搭建LAMP

学习一个服务的过程:php

一、此服务的概述:名字、功能、特色、端口号html

二、安装python

三、配置文件的位置mysql

四、服务启动关闭脚本,查看端口linux

五、此服务的使用方法c++

六、修改配置文件,实战举例正则表达式

七、排错(从上到下,从内到外)sql

LAMP=linux+apache+mysql+php数据库

安装方法:apache

rpm:安装,部署,管理比较方便,运行稳定,功能和性能较差;

源码:安装,部署,管理比较麻烦,运行相对不稳定,版本教新,功能和性能较高;

Apache官网:http://httpd.apache.org/

搭建思路:

一、服务器安装LINUX系统环境,配置网络环境,配置网络源;

二、准备软件包并上传

三、安装前先安装make,gcc,openssl等便宜工具和开发包

四、编译安装。

须要源码编译安装的软件包

httpd-2.4.25.tar.gz               #Apache主程序包

apr-1.5.2.tar.gz                           #Apache依赖包  可移植运行库

apr-util-1.5.4.tar.gz                     #Apache依赖包  可移植运行库

pcre-8.38.zip                              #Apache依赖包  是一个Perl库,包括 perl 兼容的正则表达式库

下载源码安装包

http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.25.tar.gz

http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz

http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz

https://sourceforge.net/projects/pcre/files/pcre/8.38/pcre-8.38.tar.gz

安装以前请先安装make、gcc、openssl等编译工具和开发包

[root@xuegod71]# yum install -y cmake gcc gcc-c++ bison ncurses-devel

编译安装依赖包apr-1.5.2.tar.gz

[root@xuegod71 LAMP]# tar xvf apr-1.5.2.tar.gz;cd apr-1.5.2&& ./configure -prefix=/usr/local/apr && make -j 4 && make install

编译安装依赖包apr-util-1.5.4.tar.gz

[root@xuegod71 LAMP]# tar xvf apr-util-1.5.4 && cd apr-util-1.5.4 && ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/bin/apr-1-config && make -j && make install

安装apr-util以前必须安装apr。

     如在已经安装apr的前提下安装apr-util,进入apr-util的解压目录而后执行如下命令:

     ./configure  --prefix=你要安装的目录  --with-apr=你的apr安装的目录

编译安装依赖包pcre-8.38. tar.gz

[root@xuegod71 LAMP]# tar xvf pcre-8.38.tar.gz && cd pcre-8.38 && ./configure --prefix=/usr/local/pcre && make -j 4 && make install

便宜安装apache

[root@xuegod71 LAMP]#tar zxvf httpd-2.4.25.tar.gz -C /usr/local/src/ &&  cd /usr/local/src/httpd-2.4.25/ && ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/ &&

 make -j 4 && make install

注:

--enable-so                                       #支持动态加载模块

--enable-rewrite                               #支持网站地址重写

--enable-ssl                                      #支持ssl加密

--with-apr=/usr/local/apr                 #关联apr

--with-apr-util=/usr/local/apr-util    #关联apr-util

--with-pcre=/usr/local/pcre                     #关联pcre

[root@xuegod71 LAMP]# ls /usr/local/apache/conf/httpd.conf
/usr/local/apache/conf/httpd.conf   ----配置文件

[root@xuegod71 LAMP]# ls /usr/local/apache/htdocs/
index.html  ---网站根目录

生成启动脚本:

[root@xuegod71 ~]#cp /usr/local/apache/bin/apachectl /etc/init.d/

[[root@xuegod71 ~]#chmod +x /etc/init.d/apachectl

[root@xuegod71 ~]#vi !$

#!/bin/sh
#chkconfig:345 64 36

345是启动级别;64是启动优先级;36是关闭的优先级;

两行是必不可少的,后面是描述信息

[root@xuegod71 ~]#chkconfig --add apachectl

[root@xuegod71 ~]#chkconfig apachectl on

[root@xuegod71 ~]#chkconfig --list apachectl

apachectl           0:off  1:off    2:on   3:on 4:on    5:on   6:off

启动服务

[root@xuegod71 ~]#service apachectl start

测试访问

 

源码编译mysql

下载地址:

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz

MySQL从5.7版本以后,boost是必须的,建议把系统自带的boost库卸载,源码编译安装高版本

Boost库是为C++语言标准库提供扩展的一些C++程序库的总称

[root@xuegod71 ~]# yum -y remove boost-*

卸载系统自带的mysql

[root@xuegod71 ~]# yum -y remove mysql

[root@xuegod71 ~]# yum -y remove boost-*

卸载系统自带的mysql

[root@xuegod71 ~]#yum -y remove mysql

安装必要的资源包

建议使用网络yum源,RHEL6.5光盘中自带的软件包版本不够,mysql-boost-5.7.18.tar.gz的编译对软件包的版本要求比较高,其中cmake的版本要不低于2.8

安装依赖包

yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

添加用户和组

[root@xuegod71 ~]# groupadd mysql

[root@xuegod71 ~]# useradd -M -s /sbin/nologin -r -g mysql mysql

 

建立安装目录和数据存放目录

[root@xuegod71 ~]# mkdir -p /server/mysql/

生产环境添加一块新的硬盘,建立分区sdb1并分配全部的空间

[root@xuegod71 ~]# mkdir /server

[root@xuegod71 ~]# mount /dev/sdb1 /server/

开机自动挂载

[root@xuegod71 ~]# echo "UUID=a582d669-4b00-44b5-a820-4980574f1ac7 /server ext4 defaults 0 0"  >> /etc/fstab

上传源码包 并解压源码包

[root@xuegod71 /]# tar zxf mysql-boost-5.7.18.tar.gz

将boost移到/server/mysql

[root@xuegod71 /]# mv boost/ /server/mysql/

[root@xuegod71 /]# cd mysql-5.7.18

[root@xuegod71 mysql-5.7.18]# cmake -DCMAKE_INSTALL_PREFIX=/server/mysql -DMYSQL_UNIX_ADDR=/server/mysql/mysql.sock -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/server/mysql/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=l -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST= -DWITH_BOOST=/server/mysql/boost

这些编译参数的帮助寻找方法:

http://www.mysql.com→→Documentation→→选择对应的版本(5.7)Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options

最终的URL https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html               

DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否容许本地执行LOAD DATA INFILE,默认OFF
WITH_COMMENT:指定编译备注信息
WITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不须要特别指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
SYSCONFDIR:初始化参数文件目录
MYSQL_DATADIR:数据文件目录
MYSQL_TCP_PORT:服务端口号,默认3306
MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock

编译

mysql-5.7.18.tar.gz编译时会占用大量的系统资源,建议使用多个核心同时进行编译,不然可能会编译失败

[root@xuegod71 mysql-5.7.18]# make -j 4

[ 96%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_thd_manager.cc
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/protocol_callback.cc.
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/signal_handler.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/sql_audit.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/sql_client.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/srv_session.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_info_serv
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_service.c
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_daemon.cc.o
Linking CXX static library libsql.a
[100%] Built target sql
Scanning dependencies of target mysqld
Scanning dependencies of target pfs_connect_attr-t
[100%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
Linking CXX executable mysqld
[100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_c
[100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_c
[100%] Building C object storage/perfschema/unittest/CMakeFiles/pfs_con
Linking CXX executable pfs_connect_attr-t
[100%] Built target pfs_connect_attr-t
[100%] Built target mysqld
Scanning dependencies of target udf_example
[100%] Building CXX object sql/CMakeFiles/udf_example.dir/udf_example.c
Linking CXX shared module udf_example.so
[100%] Built target udf_example

make -j 4   #注:-j 用来指定CPU核心数,可加快编译速度。 加 -j 4我用4核心来编译的,能够提升编译速度;固然你要肯定CPU多少核心

查看服务器CPU核心数

[root@xuegod71 mysql-5.7.18]# grep processor /proc/cpuinfo | wc -l

4

[root@xuegod71 mysql-5.7.18]#make install

-- Installing: /server/mysql/mysql-test/./README
-- Installing: /server/mysql/mysql-test/./mysql-test-run.pl
-- Up-to-date: /server/mysql/mysql-test/mtr
-- Up-to-date: /server/mysql/mysql-test/mysql-test-run
-- Installing: /server/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /server/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /server/mysql/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /server/mysql/support-files/mysqld_multi.server
-- Installing: /server/mysql/support-files/mysql-log-rotate
-- Installing: /server/mysql/support-files/magic
-- Installing: /server/mysql/share/aclocal/mysql.m4
-- Installing: /server/mysql/support-files/mysql.server
[root@xuegod71 mysql-5.7.18]#

修改目录权限

[root@xuegod71 mysql-5.7.18]# chown -R mysql:mysql /server/mysql/
[root@xuegod71 mysql-5.7.18]# ll /server/mysql/
总用量 60
drwxr-xr-x  2 mysql mysql  4096 4月  23 12:38 bin
drwxr-xr-x  3 mysql mysql  4096 3月  18 15:57 boost
-rw-r--r--  1 mysql mysql 17987 3月  18 15:45 COPYING
drwxr-xr-x  2 mysql mysql  4096 4月  23 12:37 docs
drwxr-xr-x  3 mysql mysql  4096 4月  23 12:37 include
drwxr-xr-x  4 mysql mysql  4096 4月  23 12:38 lib
drwxr-xr-x  4 mysql mysql  4096 4月  23 12:37 man
drwxr-xr-x 10 mysql mysql  4096 4月  23 12:39 mysql-test
-rw-r--r--  1 mysql mysql  2478 3月  18 15:45 README
drwxr-xr-x 28 mysql mysql  4096 4月  23 12:39 share
drwxr-xr-x  2 mysql mysql  4096 4月  23 12:39 support-files

生成配置文件

[root@xuegod71 mysql-5.7.18]# mv /etc/my.cnf{,.bak}

[root@xuegod71 etc]# ll my.cnf.bak
-rw-r--r--. 1 root root 251 6月  22 2015 my.cnf.bak

[root@xuegod71 etc]# vi /etc/my.cnf

[mysql]
sql_mode=NO_ENGINE_SUBSTITUTION.STRICT_TRANS_TABLES
#default
user = mysql
basedir = /server/mysql
datadir = /server/mysql/data
port = 3306
pid-file = /server/mysql/data/mysql.pid
socket = /server/mysql/mysql.sock
character-set-server = utf8
[client]
socket = /server/mysql/mysql.sock

生成服务启动脚本

[root@xuegod71 etc]# cp /server/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@xuegod71 etc]# chmod +x /etc/init.d/mysqld
[root@xuegod71 etc]# chkconfig --add mysqld
[root@xuegod71 etc]# chkconfig mysqld on
[root@xuegod71 etc]# chkconfig --list mysqld
mysqld          0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@xuegod71 etc]#

初始化数据库

[root@xuegod71 etc]# /server/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/server/mysql --datadir=/server/mysql/data
2017-04-23T04:58:23.534832Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-23T04:58:23.969872Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-04-23T04:58:24.065194Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-04-23T04:58:24.221551Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7af6e4bd-27e1-11e7-82a4-000c297e35d2.
2017-04-23T04:58:24.224774Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-04-23T04:58:24.247060Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@xuegod71 etc]#

启动服务

[root@xuegod71 ~]# service mysqld start
Starting MySQL SUCCESS!

添加path路径: vim /etc/profile 添加下面2行 在文件的结尾

export MYSQL_HOME=/server/mysql

export PATH=$PATH:$MYSQL_HOME/bin

使修改生效

source /etc/profile

[root@xuegod71 ~]# ln -s /server/mysql/bin/* /usr/local/bin/

源码编译安装php7.0.5

官网地址:http://php.net/

源码包下载地址

http://219.239.26.3/files/4255000007F687B2/cn2.php.net/distributions/php-7.0.5.tar.gz

安装epel扩展yum源

[root@xuegod71 ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.BHPeyO: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
 package epel-release-6-8.noarch is already installed
[root@xuegod71 ~]# yum clean all

[root@xuegod71 ~]# yum list

注:RHEL光盘和CentOS源缺乏不少软件包,建议使用扩展源进行安装依赖包

若是扩展源不能用,用本地yum源

安装所需依赖包

[root@xuegod71 ~]#

yum -y install php-mcrypt libmcrypt libmcrypt-devel  autoconf  freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel

  Verifying  : 2:libpng-1.2.49-1.el6_2.x86_64                                          26/26

已安装:
  gd.x86_64 0:2.0.35-11.el6                       libmcrypt.x86_64 0:2.5.8-9.el6           
  libmcrypt-devel.x86_64 0:2.5.8-9.el6            php-mcrypt.x86_64 0:5.3.3-5.el6          

做为依赖被安装:
  libXpm.x86_64 0:3.5.10-2.el6                php-common.x86_64 0:5.3.3-49.el6              

更新完毕:
  curl.x86_64 0:7.19.7-53.el6_9                    freetype.x86_64 0:2.3.11-17.el6          
  libcurl-devel.x86_64 0:7.19.7-53.el6_9           libpng.x86_64 2:1.2.49-2.el6_7           
  libpng-devel.x86_64 2:1.2.49-2.el6_7             libxml2.x86_64 0:2.7.6-21.el6_8.1        
  libxml2-devel.x86_64 0:2.7.6-21.el6_8.1        

做为依赖被升级:
  freetype-devel.x86_64 0:2.3.11-17.el6             libcurl.x86_64 0:7.19.7-53.el6_9        
  libxml2-python.x86_64 0:2.7.6-21.el6_8.1        

完毕!

[root@xuegod71 ~]# tar xf php-7.0.18.tar.gz
[root@xuegod71 ~]# ls
anaconda-ks.cfg        install.log.syslog           mysql-boost-5.7.18.tar.gz.4
apr-1.5.2              mysql-5.7.18                 pcre-8.38
apr-1.5.2.tar.gz       MySQL5.7自动安装脚本.sh      pcre-8.38.tar.gz
apr-util-1.5.4         mysql-boost-5.7.18.tar.gz    php-7.0.18
apr-util-1.5.4.tar.gz  mysql-boost-5.7.18.tar.gz.1  php-7.0.18.tar.gz
httpd-2.4.25.tar.gz    mysql-boost-5.7.18.tar.gz.2
install.log            mysql-boost-5.7.18.tar.gz.3
[root@xuegod71 ~]# cd php-7.0.18

[root@xuegod71 php-7.0.18]# ./configure --prefix=/server/php7/ \
> --with-apxs2=/usr/local/apache/bin/apxs \
> --enable-mbstring \
> --with-curl \
> --with-gd \
> --enable-fpm \
> --enable-mysqlnd  \
> --with-pdo-mysql=mysqlnd \
> --with-config-file-path=/server/php7/etc/ \
> --with-mysqli=mysqlnd \
> --with-mysql-sock=/server/mysql/mysql.sock

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
[root@xuegod71 php-7.0.18]#

注:

--with-apxs2                                                   #将php编译为Apache的一个模块进行使用

--enable-mbstring                                                 #多字节字符串的支持

--with-curl                                                      #支持cURL

--with-gd                                                       #支持gd库

--enable-fpm                                                  #支持构建fpm

--enable-mysqlnd                                                               #启用mysqlnd

--with-pdo-mysql                                           #支持pdo:MySQL支持

--with-config-file-path                                    #设置配置文件路径

--with-mysqli                                                  #支持MySQLi

--with-mysql-sock                                           #关联mysql的socket文件

[root@xuegod71 php-7.0.18]# make -j 2 && make install

Installing PEAR environment:      /server/php7/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.2
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.3
Wrote PEAR system config file at: /server/php7/etc/pear.conf
You may want to add: /server/php7/lib/php to your php.ini include_path
/root/php-7.0.18/build/shtool install -c ext/phar/phar.phar /server/php7/bin
ln -s -f phar.phar /server/php7/bin/phar
Installing PDO headers:           /server/php7/include/php/ext/pdo/

生成配置文件

[root@xuegod71 php-7.0.18]# cp php.ini-production /server/php7/etc/php.ini

添加Apache支持

[root@xuegod71 htdocs]# vi /usr/local/apache/conf/httpd.conf

Addtype application/x-httpd-php .php .phtml

建立测试页面

[root@xuegod71 htdocs]# vi a.php

<?php

        phpinfo();

?>