Skip to content
本页目录

软件版本

Nginx版本: nginx 1.12.0 Mysql版本:mysql 5.7.18 PHP版本:php 7.1.4

实现环境

Centos版本:CentOS Linux release 7.3.1611 (Core) 64位

Nginx 安装

这里将用 yum 来安装 Nginx。首先更新一下 yum repo, 以便可以安装到对应的最新版本 nginx。 http://nginx.org/packages/centos/7/noarch/RPMS/ 可以通过变换上面的地址找到和自己服务器对应版本的 repo 的 rpm。

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装好 yum repo 之后,接下来用 yum 安装 nginx

[root@localhost ~]# yum -y install nginx
[root@localhost ~]# yum -y install nginx

好了,Nginx 已经安装完成,版本是 1.12.0

[root@localhost ~]# nginx -v
nginx version: nginx/1.12.0
[root@localhost ~]# nginx -v
nginx version: nginx/1.12.0

现在设置让 Nginx 在随开机自动启动

[root@localhost ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

Centos 7启动服务命令(启动Nginx)

[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl start nginx

查看Nignx 状态

[root@localhost ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-04-17 11:48:32 CST; 6min ago
     Docs: http://nginx.org/en/docs/
  Process: 11987 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 11985 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 11989 (nginx)
   CGroup: /system.slice/nginx.service
           ├─11989 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─11990 nginx: worker process

4月 17 11:48:32 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
4月 17 11:48:32 localhost.localdomain nginx[11985]: nginx: the configuration file /etc/nginx/nginx... ok
4月 17 11:48:32 localhost.localdomain nginx[11985]: nginx: configuration file /etc/nginx/nginx.con...ful
4月 17 11:48:32 localhost.localdomain systemd[1]: Failed to read PID from file /run/nginx.pid: Inv...ent
4月 17 11:48:32 localhost.localdomain systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-04-17 11:48:32 CST; 6min ago
     Docs: http://nginx.org/en/docs/
  Process: 11987 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 11985 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 11989 (nginx)
   CGroup: /system.slice/nginx.service
           ├─11989 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─11990 nginx: worker process

4月 17 11:48:32 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
4月 17 11:48:32 localhost.localdomain nginx[11985]: nginx: the configuration file /etc/nginx/nginx... ok
4月 17 11:48:32 localhost.localdomain nginx[11985]: nginx: configuration file /etc/nginx/nginx.con...ful
4月 17 11:48:32 localhost.localdomain systemd[1]: Failed to read PID from file /run/nginx.pid: Inv...ent
4月 17 11:48:32 localhost.localdomain systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.

MySQL 安装

这里同样用 yum 安装 MySQL. 可以从以下地址中找到对应的 yum repo: http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

[root@localhost ~]# rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
[root@localhost ~]# rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

接着安装 MySQL

[root@localhost ~]# yum -y install mysql-community-server mysql-community-devel
[root@localhost ~]# yum -y install mysql-community-server mysql-community-devel

MySQL安装完成后配置文件会在这个路径 /etc/my.cnf 可以根据实际需要修改里边的选项。这里暂时不做任何修改,但有个选项是要注意的,因为后面配置 PHP 的时候讲会用到:

[root@localhost ~]# grep socket /etc/my.cnf
socket=/var/lib/mysql/mysql.sock
[root@localhost ~]# grep socket /etc/my.cnf
socket=/var/lib/mysql/mysql.sock

看看下 MySQL 的状态

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

把Mysql添加到开机启动项

[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl enable mysqld

启动 MySQL

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl start mysqld

MySQL 5.7 和之前版本很大区别是在安装后会自动为 root@localhost 用户设置一个随机初始密码,之前的版本密码为空的。那如何找到这个初始密码呢?网上很多文章说初始密码在这个文件中 /root/.mysql_secret 我不清楚早前的版本是不是这样,但 MySQL 5.7.11 并不然,而是保持到 error log 文件中。可以通过下面方法找到 MySQL 5.7 root 的初始密码

[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2017-04-17T04:09:50.383473Z 1 [Note] A temporary password is generated for root@localhost: )(9lIA*hT=q#
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2017-04-17T04:09:50.383473Z 1 [Note] A temporary password is generated for root@localhost: )(9lIA*hT=q#

其中 )(9lIA*hT=q# 就是密码。现在立即用这个密码登录 MySQL 并且修改密码(MySQL 5.7 版本对密码的安全性要求很严格,必须至少包含1个大写字母、1个小写字母、1个数字和1个特殊字符,长度不得小于8个字符)

[root@localhost ~]# mysql -uroot -p')(9lIA*hT=q#'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.18

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yasn2017.';
Query OK, 0 rows affected (0.00 sec)

mysql> update mysql.user set Host='%' where HOST='localhost' and User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+--------------------------------+
| query                          |
+--------------------------------+
| User: 'root'@'%';              |
| User: 'mysql.sys'@'localhost'; |
+--------------------------------+
2 rows in set (0.00 sec)
[root@localhost ~]# mysql -uroot -p')(9lIA*hT=q#'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.18

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yasn2017.';
Query OK, 0 rows affected (0.00 sec)

mysql> update mysql.user set Host='%' where HOST='localhost' and User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+--------------------------------+
| query                          |
+--------------------------------+
| User: 'root'@'%';              |
| User: 'mysql.sys'@'localhost'; |
+--------------------------------+
2 rows in set (0.00 sec)

其中 Yasn2017. 就是新密码。好了 MySQL 5.7 已经安装完成,退出 MySQL 命令行接着安装 PHP 7

mysql> quit
mysql> quit

ps:如果mysql授权远程访问之后,访问还是失败。可能是由于防火墙和selinux没有关闭到底。 Centos 7 关闭防火墙和selinux命令如下:

systemctl disable firewalld.service 
systemctl stop firewalld.service 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl disable firewalld.service 
systemctl stop firewalld.service 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

PHP 7 安装

PHP 7 在15年年底推出,PHP官方说的比 PHP 5 快2倍,就为这个,这个鲜必须尝。不过有个很值得注意的地方是,虽然 PHP 7 增加了不少新特性,但也很多地方是向后不兼容的,例如 mysql 扩展,在 PHP 7 中已经被删除。 这些向后不兼容导致很多程序在 PHP 7 中运行不了,例如 Discuz。但其实也不需要特别担心,因为我们可以在同一服务器上安装多个版本的 PHP。 现在最新版本是7.1.4。先把源码下载到 /software

[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/
[root@localhost software]# wget http://219.239.26.13/files/206300000A0566B7/am1.php.net/distributions/php-7.1.4.tar.gz
[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/
[root@localhost software]# wget http://219.239.26.13/files/206300000A0566B7/am1.php.net/distributions/php-7.1.4.tar.gz

接着解压

[root@localhost software]# tar zxf php-7.1.4.tar.gz
[root@localhost software]# tar zxf php-7.1.4.tar.gz

再进入解压后的文件夹

[root@localhost software]# cd php-7.1.4/
[root@localhost software]# cd php-7.1.4/

这里将只安装一些常用的扩展,大家可以根据自己的实际需要进行增减,可以通过以下命令查看PHP安装是具体有有些扩展和选项:

[root@localhost php-7.1.4]# ./configure --help
[root@localhost php-7.1.4]# ./configure --help

有接近300个选项。 安装之前要先安装那些准备装的扩展要用到的软件模块

[root@localhost php-7.1.4]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel
[root@localhost php-7.1.4]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel

接下来 configure PHP 7

[root@localhost php-7.1.4]# ./configure --prefix=/usr/local/php7 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip
[root@localhost php-7.1.4]# ./configure --prefix=/usr/local/php7 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip

上面已经提到,PHP 7 已经删除了 MySQL 扩展,所以 -with-mysql 不再是一个有效的选项。这里用 MySQLi 或 PDO 代替。 其中 --prefix 是安装目录,上面提到在同一个服务器安装多个 PHP 版本,这个 --prefix 设定是很有必要的。至于其他扩展大家按实际增减。 如果 configure 成功的话,将会看到以下类似字样:

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

编译和安装

[root@localhost php-7.1.4]# make &&  make install
[root@localhost php-7.1.4]# make &&  make install

好,PHP 7 已经安装完成,下面进行配置 先是 PHP 的配置文档

[root@localhost php-7.1.4]# cp php.ini-development /usr/local/php7/lib/php.ini
[root@localhost php-7.1.4]# cp php.ini-development /usr/local/php7/lib/php.ini

php.ini 路径应该放在 PREFIX/lib 文件夹,除非在安装的时候通过这个选项修改 --with-config-file-path=PATH 如果安装 PHP 时没有指明 --prefix ,那么就 php.ini 路径就是 /usr/local/lib/php.ini 。刚才安装时有指明 --prefix ,所以是 /usr/local/php7/lib/php.ini 然后根据实际自己需要修改 php.ini。 查找 mysqli.default_socket,修改成 mysqli.default_socket = /var/lib/mysql/mysql.sock:

[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php7/lib/php.ini
mysqli.default_socket = 
[root@localhost php-7.1.4]# sed -i 's#mysqli.default_socket =#mysqli.default_socket = /var/lib/mysql/mysql.sock#'  /usr/local/php7/lib/php.ini
[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php7/lib/php.ini
mysqli.default_socket = /var/lib/mysql/mysql.sock
[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php7/lib/php.ini
mysqli.default_socket = 
[root@localhost php-7.1.4]# sed -i 's#mysqli.default_socket =#mysqli.default_socket = /var/lib/mysql/mysql.sock#'  /usr/local/php7/lib/php.ini
[root@localhost php-7.1.4]# grep mysqli.default_socket  /usr/local/php7/lib/php.ini
mysqli.default_socket = /var/lib/mysql/mysql.sock

其中 /var/lib/mysql/mysql.sock 就是上面安装 MySQL 时提到的。这个值必须填,否则会出现如下错误: Warning: mysqli_connect(): (HY000/2002): No such file or directory

修改时区,查找 date.timezone,改成(主要将前面的 ; 去掉,这个是注释用的):

root@localhost php-7.1.4]# grep date.timezone /usr/local/php7/lib/php.ini 
; http://php.net/date.timezone
;date.timezone =
[root@localhost php-7.1.4]# sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php7/lib/php.ini 
[root@localhost php-7.1.4]# grep date.timezone /usr/local/php7/lib/php.ini 
; http://php.net/date.timezone
date.timezone = Asia/Shanghai
root@localhost php-7.1.4]# grep date.timezone /usr/local/php7/lib/php.ini 
; http://php.net/date.timezone
;date.timezone =
[root@localhost php-7.1.4]# sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php7/lib/php.ini 
[root@localhost php-7.1.4]# grep date.timezone /usr/local/php7/lib/php.ini 
; http://php.net/date.timezone
date.timezone = Asia/Shanghai

好了,PHP 7 已经安装好,下面验证一下

[root@localhost php-7.1.4]# /usr/local/php7/bin/php -v
PHP 7.1.4 (cli) (built: Apr 17 2017 14:58:11) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
[root@localhost php-7.1.4]# /usr/local/php7/bin/php -v
PHP 7.1.4 (cli) (built: Apr 17 2017 14:58:11) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

再查看下已经安装的模块

[root@localhost php-7.1.4]# /usr/local/php7/bin/php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]
[root@localhost php-7.1.4]# /usr/local/php7/bin/php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]

接下来配置 php-fpm,复制 php-fpm 的配置文档

[root@localhost php-7.1.4]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.1.4]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@localhost php-7.1.4]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.1.4]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

修改 /usr/local/php7/etc/php-fpm.d/www.conf,把启动用户改为和nginx服务同一个启动用户(这里Nginx服务使用的是nginx用户,改为nginx即可。一般都是使用www用户)

[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
group = nginx
[root@localhost php-7.1.4]# sed -i 's#user = nobody#user = nginx#' /usr/local/php7/etc/php-fpm.d/www.conf 
[root@localhost php-7.1.4]# sed -i 's#group = nobody#group = nginx#' /usr/local/php7/etc/php-fpm.d/www.conf 
[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
group = nginx
[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
group = nginx
[root@localhost php-7.1.4]# sed -i 's#user = nobody#user = nginx#' /usr/local/php7/etc/php-fpm.d/www.conf 
[root@localhost php-7.1.4]# sed -i 's#group = nobody#group = nginx#' /usr/local/php7/etc/php-fpm.d/www.conf 
[root@localhost php-7.1.4]# grep -E 'user =|group =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
group = nginx

其中 www.conf 中要留意这个值 listen = 127.0.0.1:9000

[root@localhost php-7.1.4]# grep 'listen = 127.0.0.1' /usr/local/php7/etc/php-fpm.d/www.conf
[root@localhost php-7.1.4]# grep 'listen = 127.0.0.1' /usr/local/php7/etc/php-fpm.d/www.conf

这里使用 9000 端口,这个选项在配置 Nginx 网站时要用到的。 配置 php-fpm 启动服务脚本

[root@localhost php-7.1.4]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@localhost php-7.1.4]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/

查看启动脚本中指定的程序目录和pid文件(默认已经修改过了,如果没有修改过执行下面操作)

[root@localhost php-7.1.4]# grep -E 'PIDFile|ExecStart' /usr/lib/systemd/system/php-fpm.service
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.1.4]# grep -E 'PIDFile|ExecStart' /usr/lib/systemd/system/php-fpm.service
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf

修改启动脚本,把里边 prefix 相关的内容用实际路径代替

[root@localhost php-7.1.4]# vim /usr/lib/systemd/system/php-fpm.service

PIDFile=${prefix}/var/run/php-fpm.pid
ExecStart=${exec_prefix}/sbin/php-fpm --nodaemonize --fpm-config ${prefix}/etc/php-fpm.conf
修改成
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.1.4]# vim /usr/lib/systemd/system/php-fpm.service

PIDFile=${prefix}/var/run/php-fpm.pid
ExecStart=${exec_prefix}/sbin/php-fpm --nodaemonize --fpm-config ${prefix}/etc/php-fpm.conf
修改成
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf

重新载入 systemd

[root@localhost php-7.1.4]# systemctl daemon-reload
[root@localhost php-7.1.4]# systemctl daemon-reload

让 php-fpm 随机启动

[root@localhost php-7.1.4]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost php-7.1.4]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

立即启动 php-fpm

[root@localhost php-7.1.4]# systemctl start php-fpm
[root@localhost php-7.1.4]# systemctl start php-fpm

查看状态

[root@localhost php-7.1.4]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-04-17 15:37:06 CST; 1min 9s ago
 Main PID: 55770 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─55770 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
           ├─55771 php-fpm: pool www
           └─55772 php-fpm: pool www

4月 17 15:37:06 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
4月 17 15:37:06 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...
[root@localhost php-7.1.4]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-04-17 15:37:06 CST; 1min 9s ago
 Main PID: 55770 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─55770 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
           ├─55771 php-fpm: pool www
           └─55772 php-fpm: pool www

4月 17 15:37:06 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
4月 17 15:37:06 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...

好,php-fpm 已经成功启动,那就立即建个网站看看

配置 Nginx 站点 先建立一个 lnmp 站点,路径是 /var/www/html

[root@localhost php-7.1.4]# mkdir -p /var/www/html
[root@localhost php-7.1.4]# mkdir -p /var/www/html

并准备好 phpinfo 测试文件

cat >> /var/www/html/test.php << EOF
<?php
phpinfo();
EOF
cat >> /var/www/html/test.php << EOF
<?php
phpinfo();
EOF

创建一个 Nginx 配置文件放到 /etc/nginx/conf.d/ 中

[root@localhost php-7.1.4]# cd /etc/nginx/conf.d/
[root@localhost php-7.1.4]# cd /etc/nginx/conf.d/

删除默认的default.conf

[root@localhost nginx]# rm -f /etc/nginx/conf.d/default.conf
[root@localhost nginx]# rm -f /etc/nginx/conf.d/default.conf

创建test.com.conf文件并写入以下内容

cat >> test.com.conf <<EOF
server {
    listen       80;
    server_name  localhost;
    root         /var/www/html;
    location / {
        index  index.php index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
	fastcgi_param	SCRIPT_FILENAME	$document_root$fastcgi_script_name;
	fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }
}
EOF
cat >> test.com.conf <<EOF
server {
    listen       80;
    server_name  localhost;
    root         /var/www/html;
    location / {
        index  index.php index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
	fastcgi_param	SCRIPT_FILENAME	$document_root$fastcgi_script_name;
	fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }
}
EOF

其中 server_name localhost; 中的 localhost 改成你自己的域名(例如:www.baidu.com,这里我直接使用localhost来测试) 其中 root /var/www/html; 就是刚才创建的站点目录 其中 fastcgi_pass 127.0.0.1:9000; 就是上面配置 php-fpm 提到要留意的值 修改配置后一定要记得 reload nginx 才能生效

[root@localhost conf.d]# systemctl reload nginx
[root@localhost conf.d]# systemctl reload nginx

最后的配置(nginx服务器的IP必须和域名做解析,才可以使用域名访问服务,域名购买一般在阿里云上购买) 这里我们直接使用IP访问(因为我们使用的域名是localhost,也就是nginx所在主机IP的意思)