[关闭]
@yangwenbo 2021-07-09T15:25:09.000000Z 字数 26487 阅读 442

LNMP

企业级LNMP环境应用实践

安装博客

1.LNMP介绍

LAMP(即Linux,Apache,MySQL,PHP)

2.LNMP之MySQL数据库

2.1为什么选择MySQL数据库

  • 性能卓越,服务稳定,很少出现异常宕机。
  • 开放源代码且无版权制约,自主性强,使用成本低。
  • 历史悠久,社区及用户非常活跃,遇到问题,可以很快获取到帮助。
  • 软件体积小,安装使用简单,并且易于维护,安装及维护成本低。
  • 支持多种操作系统,提供多种API接口,支持多种开发语言,特别是对流行的PHP语言无缝支持。
  • 品牌口碑效应,使得企业无需考虑就直接用之。

2.2MySQL的安装方法也有很多,常见的方法如下所示:

序号 MySQL安装方式 特点说明
1 yum/rpm包安装 特点是简单、速度快,但是没法定制安装,入门新手常用这种方式
2 二进制安装 解压软件,简单配置后就可以使用,不用安装,速度较快,专业DBA喜欢这种方式。软件名如:mysql-5.5.32-linux2.6-x86_ 64.tar.gz
3 源码编译安装 特点是可以定制安装9,但是安装时间长,例如:字符集安装路径,等等。软件名如: mysql-5.5.32.tar.gz
4 源码软件结合yum/rpm 把源码软件制作成符合要求的rpm,放到yum仓库里,然后通过yum来安装。结合了上面1和3的优点,即安装快速,可任意定制参数,但是安装者也需要具备更深能力
  • 备注:安装MySQL的注意事项如下:
  • 建议和之前介绍的Nginx服务安装在同一台机器上。
  • 重视操作过程的报错输出,有错误要解决掉再继续,不能忽略编译中的错误。

2.3安装MySQL数据库步骤介绍

2.3.1创建mysql用户的账号
  1. [root@yangwenbo ~]# groupadd mysql
  2. [root@yangwenbo ~]# useradd -s /sbin/nologin -g mysql -M mysql
  3. [root@yangwenbo ~]# tail -1 /etc/passwd
  4. mysql:x:500:500::/home/mysql:/sbin/nologin
  5. [root@yangwenbo ~]# id mysql
  6. uid=500(mysql) gid=500(mysql) groups=500(mysql)
2.3.2获取MySQL二进制软件包
  1. [root@yangwenbo yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@yangwenbo yuanmabao]# ls
  4. libiconv-1.14.tar.gz
  5. nginx-1.10.2.tar.gz
  6. wordpress-4.7.4-zh_CN.tar.gz
  7. mysql-5.5.32-linux2.6-x86_64.tar.gz #mysql二进制源码
  8. php-5.3.28.tar.gz

image_1cllbdrfc1jqp1igrsl5164019ka9.png-166.6kB

2.3.3采用二进制方式安装MySQL
  1. [root@yangwenbo yuanmabao]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
  2. [root@yangwenbo yuanmabao]# cd /usr/local/
  3. [root@yangwenbo local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
  4. [root@yangwenbo local]# ln -s mysql-5.5.32 mysql
  5. [root@yangwenbo local]# ls
  6. bin etc games include lib lib64 libexec mysql mysql-5.5.32 sbin share src
  7. [root@yangwenbo local]# cd /usr/local/mysql
  8. [root@yangwenbo mysql]# ls
  9. bin data include lib mysql-test scripts sql-bench
  10. COPYING docs INSTALL-BINARY man README share support-files
  11. #提示:二进制安装包,仅需要解压就可以了,不需要执行cmake/configure,make,make install等过程
  • 提示:
  • 当安装LNMP一体化环境时,MySQL数据库要装在Nginx所在的机器上。如果MySQL和Nginx不在一台机器上,那么,Nginx服务器上的MySQL数据库软件包只要解压移动到/usr/local/目录,改名为mysql就可以了,不需要进行后面的初始化配置。
  • 在非一体的LNMP环境(Nginx和MySQL不在一台机器上),编译PHP环境时,也是需要MySQL数据库环境的,但是高版本的PHP,例如5.3版本以上,内置了PHP需要的MySQL程序,因此,对于此类版本就不需要在Nginx服务器上安装MySQL软件了,只需要在编译PHP时指定相关参数即可。这个PHP的编译参数为--with-mysql=mysqld,表示PHP程序在编译时会调用内置的MySQL的库。
2.3.4初始化MySQL配置文件my.cnf
  1. [root@yangwenbo mysql]# pwd
  2. /usr/local/mysql
  3. [root@yangwenbo mysql]# ls -l support-files/*.cnf
  4. -rw-r--r--. 1 7161 wheel 4691 Jun 19 2013 support-files/my-huge.cnf
  5. -rw-r--r--. 1 7161 wheel 19759 Jun 19 2013 support-files/my-innodb-heavy-4G.cnf
  6. -rw-r--r--. 1 7161 wheel 4665 Jun 19 2013 support-files/my-large.cnf
  7. -rw-r--r--. 1 7161 wheel 4676 Jun 19 2013 support-files/my-medium.cnf
  8. -rw-r--r--. 1 7161 wheel 2840 Jun 19 2013 support-files/my-small.cnf
  9. [root@yangwenbo mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
  10. #使用cp全路径/bin/cp,可实现拷贝而不出现替换提示,即如果有重名文件会直接覆盖
  1. /bin/cp support-files/my-small.cnf /etc/my.cnf
  1. my-medium.cnf (最低)
  2. my-small.cnf
  3. my-large.cnf
  4. my-huge.cnf
  5. my-innodb-heavy-4G.cnf(最高)
2.3.5初始化MySQL数据库文件
  1. [root@yangwenbo ~]# mkdir -p /usr/local/mysql/data #建立MySQL数据文件目录
  2. [root@yangwenbo ~]# chown -R mysql.mysql /usr/local/mysql #授权mysql用户管理MySQL的安装目录
  3. [root@yangwenbo ~]# yum -y install libaio #光盘源安装依赖包,否则下一步的编译会报错
  4. [root@yangwenbo ~]# rpm -q libaio
  5. libaio-0.3.107-10.el6.x86_64
  6. [root@yangwenbo ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql #数据初始化
  7. #以上省略。。。
  8. Installing MySQL system tables...
  9. OK #初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
  10. Filling help tables...
  11. OK
  12. #以下省略。。。
  1. usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared ob
  2. #错误原因是没有libaio函数库的支持。需要yum -y install libaio
  1. WARNING:The host'mysql'could not be looked up with resolveip
  2. #需要修改主机名解析,使其和uname -n一样,修改后的结果如下:
  3. [root@yangwenbo ~]# grep `uname -n` /etc/hosts
  1. ERROR:1004Can't create file '/tmp/#sql300e_1_o.frm'(errno:13)
  2. #原因是/tmp目录的权限有问题。
  3. 解决办法为处理/tmp目录,如下:
  4. [root@yangwenbo ~]# ls -ld /tmp
  5. drwxrwxrwt. 3 root root 4096 Jul 14 07:56 /tmp
  6. [root@yangwenbo ~]# chmod -R 1777 /tmp/

2.4配置并启动MySQL数据库

2.4.1设置MySQL启动脚本,命令如下:
  1. [root@yangwenbo mysql]# pwd
  2. /usr/local/mysql
  3. [root@yangwenbo mysql]# ls
  4. bin data include lib mysql-test scripts sql-bench
  5. COPYING docs INSTALL-BINARY man README share support-files
  6. [root@yangwenbo mysql]# cp support-files/mysql.server /etc/init.d/mysqld #拷贝MySQL启动脚本到MySQL的命令路径
  7. [root@yangwenbo mysql]# chmod +x /etc/init.d/mysqld #使脚本可执行
2.4.2启动MySQL数据库,命令如下:
  1. [root@yangwenbo mysql]# /etc/init.d/mysqld start
  2. Starting MySQL... SUCCESS!
2.4.3检查MySQL数据库是否启动,命令如下:
  1. [root@yangwenbo mysql]# netstat -antup | grep mysql
  2. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1664/mysqld
2.4.4查看MySQL数据库启动结果日志,命令如下:
  1. [root@yangwenbo mysql]# tail -10 /usr/local/mysql/data/yangwenbo.err
  2. InnoDB: Creating foreign key constraint system tables
  3. InnoDB: Foreign key constraint system tables created
  4. 180824 23:21:45 InnoDB: Waiting for the background threads to start
  5. 180824 23:21:46 InnoDB: 5.5.32 started; log sequence number 0
  6. 180824 23:21:46 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
  7. 180824 23:21:46 [Note] - '0.0.0.0' resolves to '0.0.0.0';
  8. 180824 23:21:46 [Note] Server socket created on IP: '0.0.0.0'.
  9. 180824 23:21:46 [Note] Event Scheduler: Loaded 0 events
  10. 180824 23:21:46 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
  11. Version: '5.5.32' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
2.4.5设置MySQL开机自启动,命令如下:
  1. [root@yangwenbo mysql]# chkconfig --add mysqld
  2. [root@yangwenbo mysql]# chkconfig mysqld on
  3. [root@yangwenbo mysql]# chkconfig --list mysqld
  4. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.4.6配置mysql命令的全局使用路径,命令如下:
  1. [root@yangwenbo mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
  2. [root@yangwenbo mysql]# which mysqladmin
  3. /usr/local/bin/mysqladmin
2.4.7登陆MySQL测试,命令如下:
  1. [root@yangwenbo mysql]# mysql #第一次测试,之间登录即可
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.5.32 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show databases; #查看当前所有的数据库
  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | performance_schema |
  17. | test |
  18. +--------------------+
  19. 4 rows in set (0.00 sec)
  20. mysql> select user(); #查看当前的登陆用户
  21. +----------------+
  22. | user() |
  23. +----------------+
  24. | root@localhost |
  25. +----------------+
  26. 1 row in set (0.00 sec)
2.4.8mysql安全配置(设置密码)
  1. [root@yangwenbo mysql]# mysqladmin -uroot password '971108' #为mysql设置密码
  2. [root@yangwenbo mysql]# mysql #已经无法直接登录
  3. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  4. [root@yangwenbo mysql]# mysql -uroot -p971108
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 4
  7. Server version: 5.5.32 MySQL Community Server (GPL)
  8. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

3.nginx的编译安装部署

3.1用本地yum仓库安装依赖包

  1. [root@yangwenbo /]# yum install -y pcre-devel openssl-devel gcc gcc-c++ make curl
  2. [root@yangwenbo /]# rpm -qa pcre-devel openssl-devel gcc gcc-c++ make
  3. openssl-devel-1.0.1e-15.el6.x86_64
  4. gcc-4.4.7-4.el6.x86_64
  5. gcc-c++-4.4.7-4.el6.x86_64
  6. pcre-devel-7.8-6.el6.x86_64
  7. make-3.81-20.el6.x86_64
  8. curl-7.19.7-37.el6_4.x86_64

3.2创建程序用户

  1. [root@yangwenbo /]# useradd -s /sbin/nologin -M www
  2. [root@yangwenbo /]# id www
  3. uid=500(www) gid=500(www) groups=500(www)

3.3#解压缩

  1. [root@yangwenbo yang]# ls
  2. nginx-1.10.2.tar.gz
  3. [root@yangwenbo yang]# tar xf nginx-1.10.2.tar.gz -C /usr/src/

3.4预配置

  1. [root@yangwenbo yang]#cd /usr/src/nginx-1.10.2
  2. [root@yangwenbo nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  3. 以下省略。。。

3.5编译和安装

  1. [root@Nginx ngyangwenboinx-1.10.2]# make && make install
  2. 以下省略。。。

3.6给命令做软连接,以便PATH能找到

  1. [root@yangwenbo nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

3.7启动nginx

  1. [root@yangwenbo nginx-1.10.2]# /usr/local/nginx/sbin/nginx

4.FastCGI

4.1FastCGI的重要特点如下:

  • HTTP服务器和动态脚本语言间通信的接口或工具。
  • 可把动态语言解析和HTTP服务器分离开。
  • Nginx,Apache,Lighttpd,以及多数动态语言都支持FastCGI。
  • FastCGI接口方式采用C/S结构,分为客户端(HTTP服务器)和服务器端(动态语言解析服务器)
  • PHP动态语言服务器端可以启动多个FastCGI的守护进程(例如php-fpm(fcgi process mangement))
  • HTTP服务器通过(例如Nginx fastcgi_pass)FastCGI客户端和动态语言FastCGI服务器端通信(例如php-fpm)

4.2Nginx FastCGI的运行原理

Nginx不支持对外部动态程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用于启动另一个程序的程序),这个wrappper绑定在某个固定的socket上,如端口或文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或外部程序处理脚本来读取返回的数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。

image_1cllf657d1mok7ip1onh1e2l1c7k9.png-461.2kB

FastCGI的主要优点是把动态语言和HTTP服务器分离开来,使Nginx专门处理静态请求及向后转发的动态请求,而PHP/PHP-FPM服务器则专门解析PHP动态请求。

4.3检查确认Nginx及MySQL的安装路径,命令如下:

  1. [root@yangwenbo ~]# ls -ld /usr/local/nginx
  2. drwxr-xr-x. 11 root root 4096 Aug 24 23:57 /usr/local/nginx
  3. [root@yangwenbo ~]# ls -ld /usr/local/mysql
  4. lrwxrwxrwx. 1 mysql mysql 12 Aug 24 22:58 /usr/local/mysql -> mysql-5.5.32

4.4检查mysql端口及启动情况,命令如下:

  1. [root@yangwenbo ~]# netstat -antup | grep -E "80|3306"
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6250/nginx
  3. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1664/mysqld

4.5安装软件包wget

  1. [root@yangwenbo ~]# yum -y install wget
  2. [root@yangwenbo ~]# rpm -qa wget
  3. wget-1.12-1.8.el6.x86_64

4.6测试访问Nginx及MySQL是否OK,命令如下:

  1. [root@yangwenbo ~]# wget 127.0.0.1 #测试Nginx
  2. --2018-08-25 00:08:52-- http://127.0.0.1/
  3. Connecting to 127.0.0.1:80... connected.
  4. HTTP request sent, awaiting response... 200 OK
  5. Length: 612 [text/html]
  6. Saving to: index.html
  7. 100%[======================================================>] 612 --.-K/s in 0s
  8. 2018-08-25 00:08:52 (106 MB/s) - index.html saved [612/612]
  9. [root@yangwenbo ~]# mysql -uroot -p971108 #测试MySQL
  10. Welcome to the MySQL monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 5
  12. Server version: 5.5.32 MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  14. Oracle is a registered trademark of Oracle Corporation and/or its
  15. affiliates. Other names may be trademarks of their respective
  16. owners.
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  18. mysql> exit
  19. Bye

4.7检查安装PHP所需的lib库

  1. [root@yangwenbo ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  2. zlib-devel-1.2.3-29.el6.x86_64
  3. [root@yangwenbo ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
  • 每个lib一般都会存在对应的以“*-devel”命名的包,安装lib对应的-devel包后,对应的lib包就会自动安装好,例如安装gd-devel时就会安装gd。
  • 这些lib库不是必须安装的,但是目前的企业环境下一般都需要安装。否则,PHP程序运行时会出现问题,例如验证码无法显示等。

4.8执行下面命令安装相关的lib软件包

  1. [root@yangwenbo ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  2. [root@yangwenbo ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  3. zlib-devel-1.2.3-29.el6.x86_64
  4. libxml2-devel-2.7.6-14.el6.x86_64
  5. libjpeg-turbo-devel-1.2.1-1.el6.x86_64
  6. #这里仅缺少libiconv-devel包
  7. ------------------------------------------------
  8. [root@yangwenbo ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
  9. [root@yangwenbo ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
  10. libcurl-devel-7.19.7-37.el6_4.x86_64
  11. freetype-devel-2.3.11-14.el6_3.1.x86_64
  12. libxslt-devel-1.1.26-2.el6_3.1.x86_64
  13. gd-2.0.35-11.el6.x86_64
  14. libpng-devel-1.2.49-1.el6_2.x86_64

4.9安装yum无法安装的libiconv库

  1. [root@yangwenbo rpm]# pwd
  2. /root/rpm
  3. [root@yangwenbo rpm]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  4. --2018-08-25 00:21:11-- http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  5. Resolving ftp.gnu.org... 208.118.235.20, 2001:4830:134:3::b
  6. Connecting to ftp.gnu.org|208.118.235.20|:80... connected.
  7. HTTP request sent, awaiting response... 200 OK
  8. Length: 4984397 (4.8M) [application/x-gzip]
  9. Saving to: libiconv-1.14.tar.gz
  10. 100%[======================================================>] 4,984,397 46.9K/s in 84s
  11. 2018-08-25 00:22:36 (58.0 KB/s) - libiconv-1.14.tar.gz saved [4984397/4984397]
  12. [root@yangwenbo rpm]# ls
  13. libiconv-1.14.tar.gz #libiconv库
  14. mcrypt-2.6.8-10.el6.x86_64.rpm
  15. libmcrypt-2.5.8-9.el6.x86_64.rpm
  16. mhash-0.9.9.9-3.el6.x86_64.rpm
  17. libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
  18. nginx-1.10.2.tar.gz

4.10编译libiconv库

  1. [root@yangwenbo rpm]# tar xf libiconv-1.14.tar.gz -C /usr/src/
  2. [root@yangwenbo rpm]# cd /usr/src/libiconv-1.14/srclib
  3. [root@yangwenbo srclib]# sed -i -e '/gets is a security/d' ./stdio.in.h
  4. [root@yangwenbo srclib]# cd /usr/src/libiconv-1.14/
  5. [root@yangwenbo libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
  6. #以下省略。。。

4.11安装libmcrypt库

  1. [root@yangwenbo libiconv-1.14]# cd /etc/yum.repos.d/
  2. [root@yangwenbo yum.repos.d]# ls
  3. bak CentOS-Media.repo
  4. [root@yangwenbo yum.repos.d]# mv * /tmp/
  5. [root@yangwenbo yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  6. --2018-08-25 00:30:19-- http://mirrors.aliyun.com/repo/epel-6.repo
  7. Resolving mirrors.aliyun.com... 42.81.12.220, 42.81.12.221, 42.81.12.217, ...
  8. Connecting to mirrors.aliyun.com|42.81.12.220|:80... connected.
  9. HTTP request sent, awaiting response... 200 OK
  10. Length: 664 [application/octet-stream]
  11. Saving to: “/etc/yum.repos.d/epel.repo
  12. 100%[======================================================>] 664 --.-K/s in 0s
  13. 2018-08-25 00:30:19 (59.9 MB/s) - “/etc/yum.repos.d/epel.repo saved [664/664]
  14. [root@yangwenbo yum.repos.d]# ls
  15. epel.repo
  16. #清除缓存,建立新缓存省略。。。
  17. [root@yangwenbo yum.repos.d]# yum -y install libmcrypt-devel
  18. [root@yangwenbo yum.repos.d]# rpm -qa libmcrypt-devel
  19. libmcrypt-devel-2.5.8-9.el6.x86_64

4.12安装mhash加密扩展库

  1. [root@yangwenbo yum.repos.d]# yum -y install mhash
  2. [root@yangwenbo yum.repos.d]# rpm -qa mhash
  3. mhash-0.9.9.9-3.el6.x86_64

4.13安装mcrvpt加密扩展库

  1. [root@yangwenbo yum.repos.d]# yum -y install mcrypt
  2. [root@yangwenbo yum.repos.d]# rpm -qa mcrypt
  3. mcrypt-2.6.8-10.el6.x86_64

5. 开始安装PHP(FastCGI方式)服务

5.1获取PHP软件包

  1. [root@yangwenbo yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@yangwenbo yuanmabao]# ls
  4. libiconv-1.14.tar.gz nginx-1.10.2.tar.gz wordpress-4.7.4-zh_CN.tar.gz
  5. mysql-5.5.32-linux2.6-x86_64.tar.gz php-5.3.28.tar.gz

5.2解压配置PHP

  1. [root@yangwenbo yuanmabao]# tar xf php-5.3.28.tar.gz -C /usr/src/
  2. [root@yangwenbo yuanmabao]# cd /usr/src/php-5.3.28/
  3. [root@yangwenbo php-5.3.28]# ./configure --prefix=/usr/local/php5.3.28 --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp

image_1cllj4mef1ql5st310kn1j8v1td19.png-9.9kB

表示指定PHP的安装路径为/usr/local/php5.3.28

表示需要指定MySQL的安装路径,安装PHP需要的MySQL相关内容。当然,如果没有MySQL软件包,也可以不单独安装,这样的情况可使用--with-mysql=mysqlnd替代--with-mysql=/usr/local/mysql,因为PHP软件里已经自带了连接MySQL的客户端工具。

nginx表示指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一

表示指定PHP-FPM进程管理的组为www,此处最好与Nginx服务用户组统一。

表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。

5.3编译PHP

  1. [root@yangwenbo php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
  2. [root@yangwenbo php-5.3.28]# touch ext/phar/phar.phar
  3. [root@yangwenbo php-5.3.28]# make
  4. #以下省略。。。

Build complete.
Don't forget to run 'make test'.

5.4安装PHP生成文件到系统

  1. [root@yangwenbo php-5.3.28]# make install
  2. #以下省略。。。

5.5配置PHP引擎配置文件php.ini

5.5.1设置软链接以方便访问,命令如下:
  1. [root@yangwenbo ~]# ln -s /usr/local/php5.3.28/ /usr/local/php
  2. [root@yangwenbo ~]# ls -l /usr/local/php
  3. lrwxrwxrwx. 1 root root 21 Aug 25 01:41 /usr/local/php -> /usr/local/php5.3.28/
5.5.2查看PHP配置默认模版文件,命令如下:
  1. [root@yangwenbo ~]# cd /usr/src/php-5.3.28/
  2. [root@yangwenbo php-5.3.28]# ls php.ini*
  3. php.ini-development php.ini-production
5.5.3拷贝PHP配置文件到PHP默认目录,并更改文件名称为php.ini,命令如下:
  1. [root@yangwenbo php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
  2. [root@yangwenbo php-5.3.28]# ls -l /usr/local/php/lib/php.ini
  3. -rw-r--r--. 1 root root 69627 Aug 25 01:43 /usr/local/php/lib/php.ini

5.6配置PHP(FastCGI方式)的配置文件php-fpm.conf

  1. [root@yangwenbo php-5.3.28]# cd /usr/local/php/etc/
  2. [root@yangwenbo etc]# ls
  3. pear.conf php-fpm.conf.default
  4. [root@yangwenbo etc]# cp php-fpm.conf.default php-fpm.conf

5.7启动PHP服务(FastCGI方式)

5.7.1启动PHP服务php-fpm,命令如下:
  1. [root@yangwenbo etc]# /usr/local/php/sbin/php-fpm
5.7.2检查PHP服务php-fpm的进程及启动端口的情况,命令如下:
  1. [root@yangwenbo etc]# ps -ef | grep php-fpm
  2. root 75930 1 0 01:45 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)
  3. www 75931 75930 0 01:45 ? 00:00:00 php-fpm: pool www
  4. www 75932 75930 0 01:45 ? 00:00:00 php-fpm: pool www
  5. root 75940 1137 0 01:48 pts/0 00:00:00 grep php-fpm

6.配置Nginx支持PHP程序请求访问

6.1修改Nginx副配置文件

  1. [root@yangwenbo extra]# pwd
  2. /usr/local/nginx/conf/extra
  3. [root@yangwenbo extra]# cat www.conf
  4. server {
  5. listen 80;
  6. server_name www.boke.com;
  7. location / {
  8. root /var/www/html/blogcom;
  9. index index.php index.html index.htm;
  10. if (-f $request_filename/index.html){
  11. rewrite (.*) $1/index.html break;
  12. }
  13. if (-f $request_filename/index.php){
  14. rewrite (.*) $1/index.php;
  15. }
  16. if (!-f $request_filename){
  17. rewrite (.*) /index.php;
  18. }
  19. }
  20. location ~ .*\.(php|php5)?$ {
  21. root /var/www/html/blogcom;
  22. fastcgi_pass 127.0.0.1:9000;
  23. fastcgi_index index.php;
  24. include fastcgi.conf;
  25. }
  26. }

6.2修改Nginx主配置文件

  1. [root@yangwenbo html]# cd /usr/local/nginx/conf/
  2. [root@yangwenbo conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
  3. [root@yangwenbo conf]# pwd
  4. /usr/local/nginx/conf
  5. [root@yangwenbo conf]# cat nginx.conf
  6. worker_processes 1;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. sendfile on;
  14. keepalive_timeout 65;
  15. include extra/www.conf;
  16. }

6.3检查并平滑重启Nginx(记得关闭防火墙哟)

  1. [root@yangwenbo extra]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@yangwenbo extra]# /usr/local/nginx/sbin/nginx -s reload
  5. [root@yangwenbo extra]# service iptables stop
  6. iptables: Setting chains to policy ACCEPT: filter [ OK ]
  7. iptables: Flushing firewall rules: [ OK ]
  8. iptables: Unloading modules: [ OK ]

6.4测试PHP解析请求是否OK

6.4.1进入指定的默认站点目录后,编辑index.php,添加如下内容:
  1. [root@yangwenbo var]# mkdir -p /var/www/html/blogcom/
  2. [root@yangwenbo var]# cd /var/www/html/blogcom/
  3. [root@yangwenbo blogcom]# echo "<?php phpinfo(); ?>" >test_info.php
  4. [root@yangwenbo blogcom]# cat test_info.php
  5. <?php phpinfo(); ?>
6.4.2调整Windows下的host解析(192.168.43.228为当前的机器IP),命令如下:
  1. 192.168.43.228 www.boke.com
6.4.3打开浏览器输入http://www.boke.com/test_info.php即可打开如下图所示界面:

image_1clo6qgfr1cpc1335q8u1ivh1qn69.png-64.1kB

6.4.4针对Nginx请求访问PHP,然后对PHP连接MySQL的情况进行测试
  1. [root@yangwenbo blogcom]# pwd
  2. /var/www/html/blogcom
  3. [root@yangwenbo blogcom]# cat test_mysql.php
  4. <?php
  5. //$link_id=mysql_connect('主机名','用户','密码');
  6. $link_id=mysql_connect('localhost','root','971108');
  7. if($link_id){
  8. echo "MySQL welcomes Yang wenbo";
  9. }else{
  10. echo mysql_error();
  11. }
  12. ?>

image_1clo8a4kn1pevh6i10v7c4v10ks1g.png-15.6kB

6.4.5把以下内容加入开机自启动
  1. /etc/init.d/mysqld start
  2. /usr/local/nginx/sbin/nginx
  3. /usr/local/php/sbin/php-fpm
  4. service iptables stop

7.部署一个blog程序服务

7.1MySQL数据库配置准备

7.1.1登陆MySQL数据库,创建一个专用的数据库WordPress,用于存放blog数据
  1. [root@yangwenbo conf]# mysql -uroot -p971108 #登录Mysql数据库
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 6
  4. Server version: 5.5.32 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> create database wordpress; #创建一个数据库,名字为wordpress
  11. Query OK, 1 row affected (0.02 sec)
  12. mysql> show databases like 'wordpress'; #查看wordpress数据库
  13. +----------------------+
  14. | Database (wordpress) |
  15. +----------------------+
  16. | wordpress |
  17. +----------------------+
  18. 1 row in set (0.01 sec)
7.1.2创建一个专用的WordPress blog管理用户
  1. mysql> grant all on wordpress.* to wordpress@'localhost' identified by '971108'; #localhost为客户端地址
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> flush privileges; #刷新权限,使得创建用户生效
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> show grants for wordpress@'localhost'; #查看用户对应权限
  6. +-----------------------------------------------------------------------------------------------+
  7. | Grants for wordpress@localhost |
  8. +-----------------------------------------------------------------------------------------------+
  9. | GRANT USAGE ON *.* TO 'wordpress'@'localhost' IDENTIFIED BY PASSWORD '*2E086B4AB841306370F090F3973AC88BDAA569D3' |
  10. | GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpress'@'localhost'
  11. +-----------------------------------------------------------------------------------------------+
  12. 2 rows in set (0.00 sec)
  13. mysql> select user,host from mysql.user; #查看数据库里创建的wordpress用户
  14. +-----------+-----------+
  15. | user | host |
  16. +-----------+-----------+
  17. | root | 127.0.0.1 |
  18. | root | localhost |
  19. | wordpress | localhost | #只允许本机通过wordpress用户访问数据库
  20. +-----------+-----------+
  21. 7 rows in set (0.00 sec)

7.2Nginx及PHP环境配置准备

7.2.1选择之前配置好的支持LNMP的www域名对应的虚拟主机
  1. [root@yangwenbo extra]# pwd
  2. /usr/local/nginx/conf/extra
  3. [root@yangwenbo extra]# cat www.conf
  4. server {
  5. listen 80;
  6. server_name www.boke.com;
  7. location / {
  8. root /var/www/html/blogcom;
  9. index install.php index.html index.htm;
  10. } #补充一个首页文件install.php
  11. location ~ .*\.(php|php5)?$ {
  12. root /var/www/html/blogcom;
  13. fastcgi_pass 127.0.0.1:9000;
  14. fastcgi_index index.php;
  15. include fastcgi.conf;
  16. }
  17. }
  18. [root@yangwenbo extra]# /usr/local/nginx/sbin/nginx -s reload
  19. #平滑重启nginx
7.2.2获取WordPress博客程序,并放置到blog域名对应虚拟主机的站点目录下,即/var/www/html/blogcom,操作命令如下:
  1. [root@yangwenbo yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@yangwenbo yuanmabao]# ls
  4. wordpress-4.7.4-zh_CN.tar.gz #浏览www.wordpress.org下载博客程序
  5. [root@yangwenbo yuanmabao]# tar xf wordpress-4.7.4-zh_CN.tar.gz #解压缩
  6. [root@yangwenbo yuanmabao]# ls
  7. wordpress wordpress-4.7.4-zh_CN.tar.gz
  8. [root@yangwenbo yuanmabao]# cd /var/www/html/blogcom/
  9. [root@yangwenbo blogcom]# ls
  10. test_info.php test_mysql.php
  11. [root@yangwenbo blogcom]# rm -rf * #删除无用文件(可删可不删)
  12. [root@yangwenbo blogcom]# cd ~/yuanmabao/
  13. [root@yangwenbo yuanmabao]# mv wordpress/* /var/www/html/blogcom/
  14. #把wordpress目录里的内容移动到blogcom根目录下
  15. [root@yangwenbo yuanmabao]# cd /var/www/html/blogcom/
  16. [root@yangwenbo blogcom]# ls -l
  17. #完整的blogcom程序内容(这时可以查看所有blogcom程序内容的属主与属组)
  18. total 188
  19. -rw-r--r--. 1 nobody 65534 418 Sep 25 2013 index.php
  20. -rw-r--r--. 1 nobody 65534 19935 Jan 3 2017 license.txt
  21. -rw-r--r--. 1 nobody 65534 6956 Apr 23 2017 readme.html
  22. -rw-r--r--. 1 nobody 65534 5447 Sep 28 2016 wp-activate.php
  23. drwxr-xr-x. 9 nobody 65534 4096 Apr 23 2017 wp-admin
  24. -rw-r--r--. 1 nobody 65534 364 Dec 19 2015 wp-blog-header.php
  25. -rw-r--r--. 1 nobody 65534 1627 Aug 29 2016 wp-comments-post.php
  26. -rw-r--r--. 1 nobody 65534 2930 Apr 23 2017 wp-config-sample.php
  27. drwxr-xr-x. 5 nobody 65534 4096 Apr 23 2017 wp-content
  28. -rw-r--r--. 1 nobody 65534 3286 May 25 2015 wp-cron.php
  29. drwxr-xr-x. 18 nobody 65534 12288 Apr 23 2017 wp-includes
  30. -rw-r--r--. 1 nobody 65534 2422 Nov 21 2016 wp-links-opml.php
  31. -rw-r--r--. 1 nobody 65534 3301 Oct 25 2016 wp-load.php
  32. -rw-r--r--. 1 nobody 65534 33939 Nov 21 2016 wp-login.php
  33. -rw-r--r--. 1 nobody 65534 8048 Jan 11 2017 wp-mail.php
  34. -rw-r--r--. 1 nobody 65534 16255 Apr 7 2017 wp-settings.php
  35. -rw-r--r--. 1 nobody 65534 29896 Oct 19 2016 wp-signup.php
  36. -rw-r--r--. 1 nobody 65534 4513 Oct 15 2016 wp-trackback.php
  37. -rw-r--r--. 1 nobody 65534 3065 Sep 1 2016 xmlrpc.php
  38. [root@yangwenbo blogcom]# chown -R www.www /var/www/html/blogcom
  39. #授权用户访问(WWW为程序用户)
  40. [root@yangwenbo blogcom]# ls -l #最终博客目录和权限
  41. total 188
  42. -rw-r--r--. 1 www www 418 Sep 25 2013 index.php
  43. -rw-r--r--. 1 www www 19935 Jan 3 2017 license.txt
  44. -rw-r--r--. 1 www www 6956 Apr 23 2017 readme.html
  45. -rw-r--r--. 1 www www 5447 Sep 28 2016 wp-activate.php
  46. drwxr-xr-x. 9 www www 4096 Apr 23 2017 wp-admin
  47. -rw-r--r--. 1 www www 364 Dec 19 2015 wp-blog-header.php
  48. -rw-r--r--. 1 www www 1627 Aug 29 2016 wp-comments-post.php
  49. -rw-r--r--. 1 www www 2930 Apr 23 2017 wp-config-sample.php
  50. drwxr-xr-x. 5 www www 4096 Apr 23 2017 wp-content
  51. -rw-r--r--. 1 www www 3286 May 25 2015 wp-cron.php
  52. drwxr-xr-x. 18 www www 12288 Apr 23 2017 wp-includes
  53. -rw-r--r--. 1 www www 2422 Nov 21 2016 wp-links-opml.php
  54. -rw-r--r--. 1 www www 3301 Oct 25 2016 wp-load.php
  55. -rw-r--r--. 1 www www 33939 Nov 21 2016 wp-login.php
  56. -rw-r--r--. 1 www www 8048 Jan 11 2017 wp-mail.php
  57. -rw-r--r--. 1 www www 16255 Apr 7 2017 wp-settings.php
  58. -rw-r--r--. 1 www www 29896 Oct 19 2016 wp-signup.php
  59. -rw-r--r--. 1 www www 4513 Oct 15 2016 wp-trackback.php
  60. -rw-r--r--. 1 www www 3065 Sep 1 2016 xmlrpc.php

7.3开始安装blog博客程序(在浏览器上面操作,首次进入比较慢,请耐心等待)

7.3.1打开浏览器输入www.boke.com(提前做好hosts或DNS解析),回车后出现下图所示:

image_1cloao17l1uauddrv7l1fgd18332a.png-59.6kB

7.3.2仔细阅读页面的文字信息后,单击“现在就开始”按钮继续,然后在出现的页面表单上填写相应的内容,如下图所示:

image_1clob81b71i0916hh9441sp3ms347.png-72.9kB

7.3.3在页面表单里填好内容后,单击结尾的“提交”按钮继续,如下图所示:

image_1clob9pp91cipodt95lnlacp44k.png-22.6kB

7.3.4单击“进行安装”按钮继续,进入下图:

image_1clobddjkh7uni04jr1b671so451.png-29.3kB
image_1clobrnb6ltrkp92e13ef12t05e.png-59.9kB

7.3.5查看安装结果(出现下图所示的信息就表明已经成功安装了WordPress博客)

image_1clobvkfr12fl145j12qc1ijm1brj5r.png-22.1kB

7.3.6登录WordPress博客

image_1cloc55ch17bl1gc28sq1oum1ehc68.png-27.9kB

image_1clocdbklb3io3u1tit1anf43p7l.png-83.9kB

7.4实现WordPress博客程序URL静态化

7.4.1已知初始WordPress博客程序URL

image_1clodb78q11r21u821tk7onh1bb0as.png-6.2kB

image_1clod8h01lj22kt159p1vem1bfa2.png-27.2kB

7.4.2实现此功能时,首先要在WordPress后台依次单击设置--->固定链接--->自定义结构,然后输入下面的代码,并保存更改
  1. /archives/%post_id%.html
  2. #说明:%post_id%是数据库对应博文内容的唯一ID,例如423

image_1clodbpsd1ulrhi516of1je71ab9.png-34.4kB

7.4.3在Nginx配置文件的server容器中添加下面的代码:(个人认为这步可有可无,仅供参考)
  1. [root@yangwenbo extra]# cat www.conf
  2. server {
  3. listen 80;
  4. server_name www.boke.com;
  5. location / {
  6. root /var/www/html/blogcom;
  7. index index.php index.html index.htm;
  8. if (-f $request_filename/index.html){ #代码
  9. rewrite (.*) $1/index.html break;
  10. }
  11. if (-f $request_filename/index.php){ #代码
  12. rewrite (.*) $1/index.php; #代码
  13. }
  14. if (!-f $request_filename){
  15. rewrite (.*) /index.php;
  16. }
  17. }
  18. location ~ .*\.(php|php5)?$ {
  19. root /var/www/html/blogcom;
  20. fastcgi_pass 127.0.0.1:9000;
  21. fastcgi_index index.php;
  22. include fastcgi.conf;
  23. }
  24. }
  25. [root@yangwenbo extra]# /usr/local/nginx/sbin/nginx -t #检查语法
  26. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  27. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  28. [root@yangwenbo extra]# /usr/local/nginx/sbin/nginx -s reload #平滑重启nginx
7.4.4查看修改WordPress博客程序URL的结果

image_1clodd6g0160f1bh01esk11vcqbhbm.png-24kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注