环境:

Linux centos7.6

Mysql 5.7.36

Nginx 1.20.2

PHP 8.1.1

Wordpress 5.8.3

基于阿里云单个ecs云服务器的搭建。

构建LNMP环境 LNMP即为Linux+Nginx+MySQL+PHP环境的简称,本节我们将使用LNMP集成脚本一键在ECS上安装Nginx、MySQL和PHP的环境。

参考文章:

CSDN博主「路飞onepiece」的原创文章,原文链接:https://blog.csdn.net/qq_27745471/article/details/121251311

CSDN博主「海蜘蛛er」的原创文章,原文链接:https://blog.csdn.net/Thorne_lu/article/details/106947587

CSDN博主「ChineseLiJie」的原创文章,原文链接:https://blog.csdn.net/ChineseLiJie/article/details/99354199

CSDN博主「BLXXQQ」的原创文章,原文链接:https://blog.csdn.net/weixin_43529253/article/details/122134710

CSDN博主「shangrila_kun」的原创文章,原文链接:https://blog.csdn.net/shangrila_kun/article/details/89501343

TIMK的小站——文章:编译安装 Nginx 1.20.2,原文链接:https://blog.kmo.ink/2021/12/29/475/

51CTO博客作者lajifeiwomoshu的原创作品——Nginx1.20.2 编译安装:https://blog.51cto.com/lajifeiwomoshu/2153598

仅供学习交流,如有错误,欢迎指正和讨论,希望能够在这条路上走得更远,感谢。

一.准备工作

1.进入设置的欢迎页面

网站:https://www.bootschool.net/ascii-art

[centos7 <sub>]# vim /etc/motd                    \\按I进入编辑模式,内容如下

,\/</sub><del>\_ _/</del><del>\
| ---, `\_ ___,-------</del>\__ /<sub>' ,,'' |
| `</sub>`, ',,\`-<del>--_____ --- - /, ,--/ '/'
`\_|\ _\` ______,---</del><sub>\ ,_ '\_/' /'
\,_| , '</sub>,/'<sub> /</sub>\ ,_ `\_\ \_ \_\'
,/ /' ,/' _,-'<del> `\ </del>\_ ,_ `\ `\
/@@ _/ /' ./',- \ `@,
@@ ' | ___/ /' / \ \ '\__ _`<sub>|, `, @@
/@@ / | | ',___ | | ` | ,,---, | | `@@,
@@@ \ | | \ \O_`\ | / / O_/' | \ \ @@@
@@@ | | `| ' </sub> / , <sub> / | @@@
`@@ | \ `\ ` | | | _/' /' | @@'
@@ | </sub>\ /--'<sub> | , | \__ | | |@@
@@, \ | ,,| | ,,| | `\ /',@@
`@@, </sub>\ \ ' | / / `' ' / ,@@
@@@, \ ~<sub>\ `\/</sub>---'<sub>/' _ /'</sub>~~~~~~<sub>--,_
`@@@_,---::::::= `-,| ,</sub> _=:::::'''''' `
<sub>` </sub><sub>_/'// _,--</sub>\_/ '~--, |\_
/' /'| `@@@@@,,,,,@@@@ | \ -LUO❥

` `@@@@@@'

完成后按ESC键结束编辑,输入:wq 保存退出。 效果如下:

,\/<sub><del>\_                            _/</del><del>\
| ---, `\_ ___,-------</del>\__ /</sub>' ,,'' |
| `<sub>`, ',,\`-<del>--_____ --- - /, ,--/ '/'
`\_|\ _\` ______,---</del></sub>\ ,_ '\_/' /'
\,_| , '<sub>,/'</sub> /<sub>\ ,_ `\_\ \_ \_'
,/ /' ,/' _,-'<del> `\ </del>\_ ,_ `\ `\
/@@ _/ /' ./',- \ `@,
@@ ' | ___/ /' / \ \ '\__ _`</sub>|, `, @@
/@@ / | | ',___ | | ` | ,,---, | | `@@,
@@@ \ | | \ \O_`\ | / / O_/' | \ \ @@@
@@@ | | `| ' <sub> / , </sub> / | @@@
`@@ | \ `\ ` | | | _/' /' | @@'
@@ | <sub>\ /--'</sub> | , | \__ | | |@@
@@, \ | ,,| | ,,| | `\ /',@@
`@@, <sub>\ \ ' | / / `' ' / ,@@
@@@, \ </sub><sub>\ `\/</sub>---'<sub>/' _ /'</sub>~~~~~~<sub>--,_
`@@@_,---::::::= `-,| ,</sub> _=:::::'''''' `
<sub>` </sub><sub>_/'// _,--</sub>\_/ '~--, |\_
/' /'| `@@@@@,,,,,@@@@ | \ -LUO❥

` `@@@@@@'
2.实用小设置
[centos7 ~]#vim /etc/profile.d/env.sh //实现即时显示功能   在shell中按shift+ins键输入下方内容后保存并重新连接

PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"

export HISTTIMEFORMAT="%F %T `who -u am i 2>/dev/null| awk '{print $NF}'|sed \-e 's/[()]//g'` `whoami` "

效果如下:

[centos7 ~]#

[centos7 ~]#history
1 2022-01-17 13:56:05 117.167.229.68 root vim /etc/motd
2 2022-01-17 13:56:05 117.167.229.68 root vim /etc/profile.d/env.sh
3 2022-01-17 13:56:05 117.167.229.68 root exit;
4 2022-01-17 13:59:28 117.167.229.68 root history
3.关闭防火墙
[centos7 ~]#systemctl stop firewalld
[centos7 ~]#systemctl disable firewalld
[centos7 ~]#setenforce 0
setenforce: SELinux is disabled
4.update 最新版本系统软件,执行后才可以安装系统软件或者一键包
[centos7 ~]#yum update -y
5.初始化安装工具 (可能有些工具系统带了,但是没关系,都先装上,以防万一,编译安装和解压等使用,关于各种包的相关用法和介绍参考官方)
[centos7 ~]#yum -y install lrzsz  yum-utils gcc gcc-c++ wget autoconf net-tools iotop lsof libaio* bash-completion curl policycoreutils openssh-server openssh-clients postfix -y
[centos7 ~]#yum -y install pcre pcre-devel openssl openssl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel -y
[centos7 ~]#yum -y install libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel -y
[centos7 ~]#yum -y install libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel -y
[centos7 ~]#yum -y install gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel -y
[centos7 ~]#yum -y install gmp gmp-devel libcurl libcurl-devel openjpeg-devel mysql-utils sqlite sqlite-devel oniguruma oniguruma-devel -y
[centos7 ~]#yum -y install tree screen psmisc lsof tcpdump ntpdate systemd-devel net-tools perl perl-ExtUtils-Embed -y
6.在安装完系统后,有需要可以将系统的语言转换成中文。(本人没有尝试,感觉系统还是原生态的好,有兴趣的可以尝试)

centos中文:

yum -y install kde-l10n-Chinese  glibc-common
localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
echo '
export LANG=zh_CN.utf8
' > /etc/profile.d/chinese.sh

ubuntu中文:

apt update
apt-get install language-pack-zh* -y
echo 'LANG="zh_CN.UTF-8"' > /etc/default/locale
dpkg-reconfigure --frontend=noninteractive locales
update-locale LANG=zh_CN.UTF-8
7.下载好mysql,php,nginx,wordpress的安装包到电脑上,供编译安装时使用。官方网站如下:

MYSQL: https://downloads.mysql.com/archives/community/

PHP: https://www.php.net/downloads

NGINX: https://nginx.org/en/download.html

WORDPRESS: https://cn.wordpress.org/download/

源码包链接:https://pan.baidu.com/s/1jS5TJzLUcurLkJesXUHR6g            提取码:xyzq

二.mysql的编译

1.创建usr/local/mysql文件
[centos7 ~]#mkdir /usr/local/mysql

在centos7中默认自带了mariadb需要卸载
[centos7 ~]#rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[centos7 ~]#rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps

拓展
怎么查看和卸载yum源下载的mysql包
[centos7 ~]#yum list installed | grep mysql
[centos7 ~]#yum -y remove mysql-libs.x86_64
2.将下载好的源码包拖入xshell中,等待文件传输完毕,解压
[centos7 ~]#cd /usr/local/mysql
[centos7 mysql]#ls
mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
[centos7 mysql]#tar zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
3.配置MySQL服务

在使用Yum软件仓库安装服务程序时,系统会自动根据RPM软件包中的指令集完整软件配置等工作。但是一旦选择使用源码包的方式来安装,这一切就需要自己来完成了。针对MySQL数据库来讲,我们需要在系统中创建一个名为mysql的用户,专门用于负责运行MySQL数据库。请记得要把这类账户的Bash终端设置成nologin解释器,避免黑客通过该用户登录到服务器中,从而提高系统安全性。

[centos7 mysql]#useradd mysql -s /sbin/nologin

重命名并移动文件

[centos7 mysql]#mv mysql-5.7.36-linux-glibc2.12-x86_64/* /usr/local/mysql
[centos7 mysql]#rm -rf mysql-5.7.36-linux-glibc2.12-x86_64 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

创建一个用于保存MySQL数据库程序和数据库文件的目录,并把该目录的所有者和所属组身份修改为mysql。其中,/usr/local/mysql是用于保存MySQL数据库服务程序的目录,/usr/local/mysql/data则是用于保存真实数据库文件的目录。

[centos7 mysql]#mkdir -p /usr/local/mysql/data  
[centos7 mysql]#chown -Rf mysql:mysql /usr/local/mysql /usr/local/mysql/data

修改相关配置文件

[centos7 mysql]#vim /etc/my.cnf
[mysqld]
port=3306
socket=/tmp/mysql.sock
#mysql-5.7.36文件的路径
basedir=/usr/local/mysql
#mysql-5.7.36数据路径
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/data/mysql.err
#最大连接数
max_connections=10000
#编码
character-set-server=utf8
#skip-grant-tables
default-storage-engine=INNODB
#不区分大小写
lower_case_table_names=1
#绑定ipv4
bind-address=0.0.0.0
#设置传输包限制,默认1M
max_allowed_packet=64M
#设置缓冲池大小:
innodb_buffer_pool_size=1024M
#设置隔离级别
transaction-isolation=read-committed
#设置时区
default-time-zone='+8:00'
#设置密码验证默认加密方式
#default-authentication-plugin=mysql_native_password
explicit_defaults_for_timestamp=true
[mysql]
#编码
default-character-set=utf8


初始化数据库
进入mysql的bin目录
[centos7 mysql]#cd /usr/local/mysql/bin/
[centos7 bin]#./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize

查看密码
[centos7 bin]#cat /usr/local/mysql/data/mysql.err ABwj)5bdw0Sv

启动mysql,并更改root 密码 先将mysql.server放置到/etc/init.d/mysql中

[centos7 bin]#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[centos7 bin]#/etc/init.d/mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/mysql.err'.
[ OK ]
[22:40:32 root@centos8 bin]#service mysql restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]

配置环境变量
[centos7 bin]#vim /etc/profile
在最后添加export PATH="$PATH:/usr/local/mysql/bin"

刷新数据
[centos7 bin]#source /etc/profile

修改密码
[centos7 bin]#mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: //输入初始密码
The existing password for the user account root has expired. Please set a new password.
New password: //输入新密码,这里不会显示
Re-enter new password: // 继续输一遍
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y //默认y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

创建wordpress数据

[centos7 ~]#mysql -uroot -p设置的密码  
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 11
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> create database wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set host = '120.79.136.172' where user = 'wordpress';
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> grant all privileges on wordpress.* to "wordpress"@"120.79.136.172" identified by "设置的密码";
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql | |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit;
Bye




拓展:如何修改mysql密码
[centos7 ~]#SET PASSWORD = PASSWORD('设置的密码');
[centos7 ~]#ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
[centos7 ~]#FLUSH PRIVILEGES;

设置开机自启动

[centos7 ~]#chkconfig --list    //查看列表

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.

If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.

aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[centos7 ~]#chkconfig add mysql //添加
chkconfig version 1.7.6 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage: chkconfig [--list] [--type <type>] [name]
chkconfig --add <name>
chkconfig --del <name>
chkconfig --override <name>
chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

[centos7 ~]#chkconfig mysql on //设置开机自启动

查看是否mysql启动
[centos7 bin]#ps -ef | grep mysql
root 87902 1 0 10:59 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/centos8.5.pid
mysql 88170 87902 0 10:59 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/mysql.err --pid-file=/usr/local/mysql/data/centos8.5.pid --port=3306
root 88365 4134 0 11:15 pts/1 00:00:00 grep --color=auto mysql

三.php的编译

1.解压php包
[centos7 bin]#mkdir /usr/local/php /usr/local/php/etc /usr/local/php/etc/php-fpm.d
[centos7 bin]#yum -y install cmake -y
设置环境变量
[centos7 bin]# echo "export CMAKE_HOME=/opt/cmake export PATH=$CMAKE_HOME/bin:$PATH" >> /etc/profile.d/cmake.sh

[centos7 bin]# source /etc/profile.d/cmake.sh #让环境变量生效

[centos7 bin]#cmake -version
cmake version 2.8.12.2

[centos7 bin]#cd /usr/local/php
[centos7 php]#tar zxvf php-8.1.1.tar.gz
[centos7 php]#cd php-8.1.1
2.预编译,编译安装
预编译
[centos7 php-8.1.1]#./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache

编译安装参数说明
--prefix=/usr/local/php # 配置安装目录
--with-config-file-path=/usr/local/php # 配置文件 php.ini 的路径
--enable-sockets # 开启 socket
--enable-fpm # 启用 fpm 扩展
--enable-cli # 启用 命令行模式 (从 php 4.3.0 之后这个模块默认开启所以可以不用再加此命令)
--enable-mbstring # 启用 mbstring 库
--enable-pcntl # 启用 pcntl (仅 CLI / CGI)
--enable-soap # 启用 soap
--enable-opcache # 开启 opcache 缓存
--disable-fileinfo # 禁用 fileinfo (由于 5.3+ 之后已经不再持续维护了,但默认是开启的,所以还是禁止了吧)(1G以下内存服务器直接关了吧)
--disable-rpath # 禁用在搜索路径中传递其他运行库。
--with-mysqli # 启用 mysqli 扩展
--with-pdo-mysql # 启用 pdo 扩展
--with-iconv-dir # 启用 XMLRPC-EPI 字符编码转换 扩展
--with-openssl # 启用 openssl 扩展 (需要 openssl openssl-devel)
--with-fpm-user=nginx # 设定 fpm 所属的用户
--with-fpm-group=nginx # 设定 fpm 所属的组别
--with-curl # 启用 curl 扩展
--with-mhash # 开启 mhash 基于离散数学原理的不可逆向的php加密方式扩展库
# GD
--with-gd # 启用 GD 图片操作 扩展
--with-jpeg-dir # 开启对 jpeg 图片的支持 (需要 libjpeg)
--with-png-dir # 开启对 png 图片支持 (需要 libpng)
--with-freetype-dir # 开启 freetype
# 压缩
--enable-zip # 启用 zip
--with-zlib # 启用对 zlib 支持
# xml
--enable-simplexml # 启用对 simplexml 支持
--with-libxml-dir # 启用对 libxml2 支持

--enable-debug # 开启 debug 模式


[centos7 php-8.1.1]#vim /etc/ld.so.conf
# 添加如下几行
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
# 使配置生效
[centos7 php-8.1.1]#ldconfig -v

再执行一遍
[centos7 php-8.1.1]#./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache

编译安装
[centos7 php-8.1.1]make -j 2 && make install
3.创建 Php.ini 配置文件
创建一个nginx用户
[centos7 php-8.1.1]#useradd -s /sbin/nologin -M nginx

[centos7 php-8.1.1]#cp php.ini-production /usr/local/php/etc/php.ini
[centos7 php-8.1.1]#vim /usr/local/php/etc/php.ini +1355 //php的Session存储目录
mysqli.default_socket = /tmp/mysql.sock //1173行修改
session.save_path = "/tmp" //1355行将注释打开


; Default host for mysqli_connect() (doesn't apply in safe mode).
; https://php.net/mysqli.default-host
mysqli.default_host = localhost //1177行修改

; Default user for mysqli_connect() (doesn't apply in safe mode).
; https://php.net/mysqli.default-user
mysqli.default_user = wordpress //1181行修改
4.设置php-fpm配置文件
[centos7 php-8.1.1]#cd /usr/local/php/etc
[centos7 etc]#cp php-fpm.conf.default php-fpm.conf
[centos7 etc]#vim php-fpm.conf +17
pid = /var/run/php-fpm.pid //将注释取消并修改

php-fpm参数优化
[centos7 etc]#cd /usr/local/php/etc/php-fpm.d/
[centos7 php-fpm.d]#cp www.conf.default www.conf
[centos7 php-fpm.d]#vim www.conf
#sed -i '/^;.*/d;/^$/d' www.conf // 删除文件中;的注释和空格(看个人习惯选择执行与否)
#sed -i '/#.*/d' www.conf // 删除文件中的 #注释
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000 //php本机不用改,分开则加
pm = dynamic //网站页面设置((static, dynamic or ondemand))
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

;listen.allowed_clients=nginx Ip地址 //nginx和php同一个机器不用加

php-fpm 进程池配置
下面4个参数的意思分别为:
pm.max_children:动态方式下他限定php-fpm的最大进程数(这里要注意pm.max_spare_servers的值只能小于等 pm.max_children)
pm.start_servers:动态方式下的起始php-fpm进程数量。
pm.min_spare_servers:动态方式空闲状态下的最小php-fpm进程数量。
pm.max_spare_servers:动态方式空闲状态下的最大php-fpm进程数量。
5.启动php-fpm
[centos7 php-fpm.d]#/usr/local/php/sbin/php-fpm

配置 php-fpm 系统环境变量
[centos7 php-fpm.d]#cd
[centos7 ~]#vim /etc/profile.d/php.sh
export PHP_HOME=/usr/local/php
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin

重载环境变量
[centos7 ~]#source /etc/profile.d/php.sh

配置 php-fpm 开机自启动
[centos7 ~]#vim /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target

php-fpm.service 文件说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

重载 systemctl 配置
[centos7 ~]#systemctl daemon-reload

停止 php-fpm
[centos7 ~]#pkill php-fpm

用 systemctl 启动 php-fpm
[centos7 ~]#systemctl start php-fpm.service

设置 php-fpm 开机启动
[centos7 ~]#systemctl enable php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

php-fpm 管理命令
systemctl stop php-fpm.service # 停止服务
systemctl restart php-fpm.service # 重新启动服务
systemctl status php-fpm.service # 查看服务当前状态
systemctl disable php-fpm.service # 停止开机自启动

检查 php-fpm 是否成功启动
[centos7 ~]#ps aux | grep php-fpm
有相关进程则成功了
root 303760 0.0 0.5 117260 10392 ? Ss 14:17 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx 303761 0.0 0.5 144824 9796 ? S 14:17 0:00 php-fpm: pool www
nginx 303762 0.0 0.5 144824 9796 ? S 14:17 0:00 php-fpm: pool www
nginx 303763 0.0 0.5 144824 9796 ? S 14:17 0:00 php-fpm: pool www

四.nginx的编译

1.解压
[centos7 ~]#mkdir /usr/local/nginx              //创建nginx文件夹存放相关数据 
[centos7 ~]#cd /usr/local/nginx //下载好的nginx包拖入
[centos7 nginx]#tar zxvf nginx-1.20.2.tar.gz //解压,工具包放在local文件夹下以便编译时调用
2.预编译,编译安装
预编译
[centos7 nginx]#cd nginx-1.20.2
[centos7 nginx-1.20.2]#./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

编译安装
[centos7 nginx-1.20.2]#make && make install
3.将 Nginx 添加到 systemctl
创建nginx用户
[centos7 nginx-1.20.2]#cd ..
[centos7 nginx]#mkdir -p /var/cache/nginx/client_temp

[centos7 nginx]#mkdir /usr/local/nginx/service

[centos7 nginx]#cat >>/usr/local/nginx/service/nginx.service <<\EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

[centos7 nginx]#link /usr/local/nginx/service/nginx.service /usr/lib/systemd/system/nginx.service

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

[centos7 nginx]#systemctl start nginx

查看是否成功
[centos7 nginx]#curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.添加 php 探测文件
[centos7 nginx]#vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
5.添加 Nginx 配置
禁止爬虫抓取数据的设置
[centos7 nginx]#touch /usr/local/nginx/agent_deny.conf
[centos7 nginx]#vim /usr/local/nginx/agent_deny.conf

#禁止Scrapy等工具的抓取
if ($http_user_agent <sub>* (Scrapy|Curl|HttpClient)) {
return 403;
}

#禁止指定UA及UA为空的访问
if ($http_user_agent </sub> "WinHttp|WebZIP|FetchURL|node-superagent|java/|
FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|
CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|
Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|
lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|
YandexBot|FlightDeckReports|Linguee Bot|^$" ) {
return 403;
}

配置如下
[centos7 nginx]#sed -i '/^#.*/d;/^$/d' nginx.conf // 删除文件中的注释
[centos7 nginx]#sed -i '/#.*/d' nginx.conf // 删除文件中的#注释

[centos7 nginx]#vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
include agent_deny.conf; #禁止爬虫抓取数据
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/html;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=/$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
location ~ \.php$ {
root /usr/local/nginx/html;
include fastcgi_params; #指定nginx连接php-fpm的常量
fastcgi_pass 127.0.0.1:9000; #连接php-fpm的地址和端口
fastcgi_index index.php; #指定默认页面
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #指定站点根目录
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
6.验证 Nginx 关联 php-fpm
[centos7 nginx]#ps -ef|grep php-fpm
root 315054 1 0 15:52 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fp.conf)
nginx 315055 315054 0 15:52 ? 00:00:00 php-fpm: pool www
nginx 315056 315054 0 15:52 ? 00:00:00 php-fpm: pool www
nginx 315057 315054 0 15:52 ? 00:00:00 php-fpm: pool www
7.重载 Nginx 配置
[centos7 nginx]#netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 332212/php-fpm: mas
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 323054/nginx: maste
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1203/sshd
tcp6 0 0 :::3306 :::* LISTEN 92362/mysqld

[centos7 nginx]#/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[centos7 nginx]#/usr/local/nginx/sbin/nginx -s reload

查看nginx是否启动
[11:02:57 root@centos8 nginx]#ps -ef|grep nginx
root 323054 1 0 Jan18 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 332213 332212 0 10:32 ? 00:00:00 php-fpm: pool www
nginx 332214 332212 0 10:32 ? 00:00:00 php-fpm: pool www
nginx 332215 332212 0 10:32 ? 00:00:00 php-fpm: pool www

浏览器输入:主机名
可查看到phpinfo的内容,版本号等

五.wordpress的安装

1.解压wordpress包
[centos7 nginx]#cd html
[centos7 html]#tar zxvf wordpress-5.8.3-zh_CN.tar.gz
[centos7 html]#mv wordpress/* .
mv: overwrite './index.php'? y
[centos7 html]#rm -rf wordpress wordpress-5.8.3-zh_CN.tar.gz
2.配置wordpress相关数据
[centos7 html]#cp wp-config-sample.php wp-config.php
[centos7 html]#vim wp-config.php

// ** MySQL settings - You can get this info from your web host </strong> //
/** The name of the database for WordPress */
define( 'WP_ALLOW_REPAIR', true );
define( 'DB_NAME', '设置的账号' );

/** MySQL database username */
define( 'DB_USER', '设置的账号' );

/** MySQL database password */
define( 'DB_PASSWORD', '设置的密码' );

/** MySQL hostname */
define( 'DB_HOST', 'ip:3306' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
3.重载nginx
[centos7 html]#chown nginx:nginx /usr/local/nginx/html /usr/local/nginx -R
[centos7 html]#/usr/local/nginx/sbin/nginx -s reload