前言
本篇博客使用 yum 来搭建 lnmp 环境, 将采用动态, 静态以及数据库分开安装的方式即 nginx,PHP,MySQL. 会被分开安装在不同的服务器之上, 搭建出来一套 lnmp 环境, 并部署 WordPress 进行测试.
LNMP 准备环境
centos7
firewalld 关闭状态
selinux 关闭状态
nginx 服务器 IP:192.168.43.174
PHP,PHP-fpm,PHP-MySQL 服务器 IP: 192.168.43.175
MySQL 服务器 IP:192.168.43.176
LNMP 搭建
第一步: PHP,PHP-fpm,PHP-MySQL 服务器搭建
下载用于和数据库通信的 PHP-MySQL, 支持 PHP 文件的 PHP 以及实现 fastcgi 的 PHP-fpm
[root@server ~]# yum install PHP-MySQL PHP PHP-fpm -y
第二步: 配置 PHP-fpm 文件
主要修改 12 行处为本机的 IP 地址, 24 行处修改为 nginx 端的 IP 地址, 保证本机有 apache 用户
- ; Start a new pool named 'www'.
- [www]
- ; The address on which to accept FastCGI requests.
- ; Valid syntaxes are:
- ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
- ; a specific port;
- ; 'port' - to listen on a TCP socket to all addresses on a
- ; specific port;
- ; '/path/to/unix/socket' - to listen on a unix socket.
- ; Note: This value is mandatory.
- listen = 192.168.43.175:9000 ## 这里修改为本机的 IP 地址
- ; Set listen(2) backlog. A value of '-1' means unlimited.
- ; Default Value: -1
- ;listen.backlog = -1
- ; List of ipv4 addresses of FastCGI clients which are allowed to connect.
- ; Equivalent to the FCGI_web_SERVER_ADDRS environment variable in the original
- ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
- ; must be separated by a comma. If this value is left blank, conne; must be separated by a comma. If this value is left blank, connections will be
- ; accepted from any ip address.
- ; Default Value: any
- listen.allowed_clients = 192.168.43.174 ## 修改为 nginx 端的 IP 地址
- ; Set permissions for unix socket, if one is used. In Linux, read/write
- ; permissions must be set in order to allow connections from a Web server. Many
- ; BSD-derived systems allow connections regardless of permissions.
- ; Default Values: user and group are set as the running user
- ; mode is set to 0666
- ;listen.owner = nobody
- ;listen.group = nobody
- ;listen.mode = 0666
- ; Unix user/group of processes
- ; Note: The user is mandatory. If the group is not set, the default user's group
- ; will be used.
- ; RPM: apache Choosed to be able to access some dir as httpd
- user = apache #确保有 apache 用户
- ; RPM: Keep a group allowed to write in log dir.
- group = apache #确保有 apache 组
- ...
检查是否有 apache 用户, 如果没有需要下载 httpd 服务, 或者自建 apache 用户即可
- [root@server ~]# id apache
- uid=998(apache) gid=48(apache) groups=48(apache)
第三步: 启动 PHP-fpm 服务
监听本机的 9000 端口
- [root@server ~]# systemctl restart PHP-fpm
- [root@server ~]# ss -tnl | grep 9000
- LISTEN 0 128 192.168.43.175:9000 *:*
第四步: 下载 nginx
在 192.168.43.174 服务器上面下载 nginx
[root@proxy ~]# yum install nginx -y
第五步: 配置 nginx
添加如下一个 location,fastcgi_pass 执行刚才配置的 PHP 服务器端
- [root@proxy ~]# VIM /etc/nginx/nginx.conf
- ...
- server {
- listen 80;
- server_name _;
- root /var/www/html;
- index index.HTML index.PHP;
- # Load configuration files for the default server block.
- # include /etc/nginx/default.d/*.conf;
- location ~ \.PHP$ {
- fastcgi_pass 192.168.43.175:9000;
- include fastcgi.conf;
- }
- ....
第六步: 检查 nginx 配置
- [root@proxy ~]# nginx -t
- nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
- nginx: configuration file /etc/nginx/nginx.conf test is successful
第七步: 启动 nginx
检查无误后启动 nginx
- [root@proxy ~]# systemctl restart nginx
- [root@proxy ~]# ss -tnl | lsof -i :80
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- nginx 9433 root 6u IPv4 188674 0t0 TCP *:http (LISTEN)
- nginx 9434 nginx 6u IPv4 188674 0t0 TCP *:http (LISTEN)
第八步: 下载 MySQL
在 MySQL 服务器端下载数据库
[root@agent ~]# yum install mariadb-server -y
第九步: 启动数据库
[root@agent ~]# systemctl restart mariadb
第十步: 建立数据库及用户
创建一个 WordPress 数据库, 新建一个 WordPress 用户
- [root@agent ~]# MySQL -uroot -p123
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MySQL connection id is 116
- Server version: 5.7.23-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MySQL [(none)]> create database WordPress;
- Query OK, 1 row affected (0.06 sec)
- MySQL [(none)]> grant all on WordPress.* to WordPress@'%' identified by '123';
- Query OK, 0 rows affected, 1 warning (0.07 sec)
- MySQL [(none)]> flush privileges;
- Query OK, 0 rows affected (0.02 sec)
经过以上十步 lnmp 环境就已经搭建完成
LNMP 环境部署 WordPress 进行测试
第一步: nginx 服务器端准备 WordPress 文件
- [root@proxy ~]# cd /var/www/HTML/
- [root@proxy HTML]# ls
- [root@proxy HTML]# rz
- [root@proxy HTML]# ls
- WordPress-3.3.1-zh_CN.zip
- [root@proxy HTML]# yum install unzip -y
- [root@proxy HTML]# unzip WordPress-3.3.1-zh_CN.zip
第二步: PHP 服务器端也要准备 WordPress 文件
至于为什么也要在 PHP 服务器端准备 WordPress 文件是因为 nginx 文件里面的配置, 相当于动静分离架构, 动态文件即 PHP 文件会来 PHP 服务器端来找
- [root@server ~]# cd /var/www/HTML/
- [root@server HTML]# ls
- [root@server HTML]# rz
- [root@server HTML]# ls
- WordPress-3.3.1-zh_CN.zip
- [root@proxy HTML]# yum install unzip -y
- [root@server HTML]# unzip WordPress-3.3.1-zh_CN.zip
- [root@server HTML]# ls
- WordPress WordPress-3.3.1-zh_CN.zip
第三步: 浏览器测试
在浏览器输入 nginx 服务器的 ip 地址
点击创建一个配置文件
点击现在就开始
输入之前创建的数据库信息及用户信息, 点击提交
提示创建失败, 只能进行手工创建 (nginx 服务器端及 PHP 服务器端执行下面同样的操作)
- [root@proxy HTML]# cd WordPress
- [root@proxy WordPress]# cp wp-config-sample.PHP wp-config.PHP
- [root@proxy WordPress]# VIM wp-config.PHP
- //** MySQL 设置 - 具体信息来自您正在使用的主机 **//
- /** WordPress 数据库的名称 */
- define('DB_NAME', 'wordpress');
- /** MySQL 数据库用户名 */
- define('DB_USER', 'wordpress');
- /** MySQL 数据库密码 */
- define('DB_PASSWORD', '123');
- /** MySQL 主机 */
- define('DB_HOST', '192.168.43.176');
- /** 创建数据表时默认的文字编码 */
- define('DB_CHARSET', 'utf8');
再次打开浏览器进行测试
根据提示输入以上信息, 点击下面的安装
输入账号和密码进行登录即可
至此 LNMP 服务搭建完成
来源: https://www.cnblogs.com/kenken2018/p/9955045.html