利用LNMP实现Wordpress站点搭建
Ubuntu2204 >>> 192.168.28.61 >>> nginx1.22.1 + php-fpm8.1.2 + wordpress6.4.2
Ubuntu2204 >>> 192.168.28.62 >>> mysql 8.0.36
192.168.28.61
--------------------------------------------配置php-fpm
#安装php-fpm及相关组件
[root@nginxphp ~]#apt -y install php-fpm php-mysql php-json
[root@nginxphp ~]#php-fpm8.1 -v
PHP 8.1.2-1ubuntu2.14 (fpm-fcgi) (built: Aug 18 2023 11:41:11)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
[root@nginxphp ~]#systemctl status php8.1-fpm.service
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-02-23 02:29:13 CST; 1min 2s ago
Docs: man:php-fpm8.1(8)
...
[root@nginxphp ~]#ll /run/php/php-fpm.sock
lrwxrwxrwx 1 root root 30 Feb 23 02:29 /run/php/php-fpm.sock -> /etc/alternatives/php-fpm.sock=
#若nginx使用socket文件和php通信,需要使nginx服务和php-fpm使用相同的账户,如www-data,否则会出现502
#若nginx通过9000端口和php通信,无需修改用户身份
#默认不支持远程连接,9000端口未监听
[root@nginxphp ~]#ss -tnl | grep 9000
[root@nginxphp ~]#ps aux | grep php
root 8191 0.0 1.0 202700 20348 ? Ss 03:03 0:00 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
www-data 8193 0.0 0.3 203172 7368 ? S 03:03 0:00 php-fpm: pool www
www-data 8194 0.0 0.3 203172 7368 ? S 03:03 0:00 php-fpm: pool www
#优化php配置,修改下方三行
[root@nginxphp ~]#vim /etc/php/8.1/fpm/php.ini
date.timezone = Asia/Shanghai #设置PHP的默认时区
post_max_size = 100M #限制通过HTTP POST方法提交到服务器的数据的最大值,和上传有关,nginx中也要修改相关值
upload_max_filesize = 100M #限制通过HTTP上传的文件的最大值,即上传功能
[root@nginxphp ~]#vim /etc/php/8.1/fpm/pool.d/www.conf
;listen = /run/php/php8.1-fpm.sock
listen = 9000 #在本地监听9000端口。listen = 127.0.0.1:9000; 表示仅允许本机连接9000
pm.status_path = /pm_status #访问 PHP-FPM 的状态页面的路径。http://your-server-address/pm_status
ping.path = /ping #检查 PHP-FPM 是否还在运行。访问http://your-server-address/ping 来发送一个 PING 请求到 PHP-FPM。如果 PHP-FPM 还在运行,它将返回一个包含 "pong" 的简单响应。这个机制通常用于健康检查或监控脚本中。
[root@nginxphp ~]#systemctl restart php8.1-fpm.service
[root@nginxphp ~]#ss -tnl | grep 9000
LISTEN 0 511 *:9000 *:*
#安装php-fpm相关组件
[root@nginxphp conf.d]#apt -y install php-mysql
--------------------------------------------配置nginx
#通过脚本安装
[root@nginxphp ~]#nginx -V
nginx version: nginx/1.22.1
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginxphp ~]#tree -d /apps/nginx/
/apps/nginx/
├── client_body_temp
├── conf
├── fastcgi_temp
├── html
├── logs
├── proxy_temp
├── sbin
├── scgi_temp
└── uwsgi_temp
[root@nginxphp ~]#mkdir /apps/nginx/conf/conf.d
[root@nginxphp conf.d]#vim ../nginx.conf
...
http {
...
include /apps/nginx/conf/conf.d/*.conf;
}
[root@nginxphp conf.d]#cat www.wenzi.com.conf
server {
listen 80;
server_name www.wenzi.com;
location / {
root /apps/nginx/html/wordpress;
index index.php index.html index.htm;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location ~ \.php$|ping|pm_status {
root /apps/nginx/html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
client_max_body_size 100m;
include fastcgi_params;
}
}
[root@nginxphp conf.d]#ls /apps/nginx/html/
50x.html index.html wordpress-6.4.2-zh_CN.zip
[root@nginxphp conf.d]#cd /apps/nginx/html/
[root@nginxphp html]#unzip wordpress-6.4.2-zh_CN.zip
#设置wordpress项目权限,注意权限是给php-fpm的www-data,而不是nginx,因为nginx只是个代理,不处理wordpress文件
[root@nginxphp html]#chown -R www-data:www-data wordpress
192.168.28.62
--------------------------------------------配置MySQL
#安装MySQL
[root@mysql mysql]#apt -y install mysql-server
#修改mysql密码
[root@mysql mysql]#mysql
mysql> alter user root@'localhost' identified with mysql_native_password by 'Admin.123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
#准备wordpress数据库、创建用户、授权
[root@mysql mysql]#mysql -uroot -pAdmin.123
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> create user wp@'192.168.28.%' identified by 'Admin.123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on wordpress.* to wp@'192.168.28.%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for wp@'192.168.28.%';
+--------------------------------------------------------------+
| Grants for wp@192.168.28.% |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO `wp`@`192.168.28.%` |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO `wp`@`192.168.28.%` |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)
#修改MySQL配置,使其监听在0.0.0.0:3306上,所有远程主机都可以访问
[root@mysql mysql]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
...
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
...
[root@mysql mysql]#systemctl restart mysql.service
[root@mysql mysql]#ss -tnl | grep 3306
LISTEN 0 151 0.0.0.0:3306 0.0.0.0:*
测试访问
访问 http://www.wenzi.com/ping
访问 http://www.wenzi.com/pm_status
访问 http://www.wenzi.com ,会自动进入wordpress的安装向导,完成后发布一篇文章测试
利用LNMP实现phpmyadmin的负载均衡,并利用Redis实现会话保持
192.168.28.60 >>> nginx反向代理负载均衡
192.168.28.61 >>> nginx + phpmyadmin 192.168.28.62 >>> nginx + phpmyadmin
192.168.28.51 >>> mysql
192.168.28.52 >>> redis
192.168.28.51
--------------------------------------------配置MySQL
[root@mysql ~]#apt update && apt -y install mysql-server
[root@mysql ~]#mysql
mysql> create user pma@'192.168.28.%' identified with mysql_native_password by 'Admin.123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to pma@'192.168.28.%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@mysql ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
[root@mysql ~]#systemctl restart mysql
[root@mysql ~]#ss -tnl | grep 3306
LISTEN 0 151 *:3306 *:*
192.168.28.52
--------------------------------------------配置Redis
[root@redis ~]#apt -y install redis
[root@redis ~]#vim /etc/redis/redis.conf
bind 0.0.0.0
[root@redis ~]#systemctl restart redis
[root@redis ~]#ss -tnlp | grep redis
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=3436,fd=6))
192.168.28.61 / 192.168.28.62
--------------------------------------------配置Nginx
#通过脚本安装
[root@pma1 ~]#nginx -V
nginx version: nginx/1.22.1
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@pma1 ~]#tree -d /apps/nginx/
/apps/nginx/
├── client_body_temp
├── conf
├── fastcgi_temp
├── html
├── logs
├── proxy_temp
├── sbin
├── scgi_temp
└── uwsgi_temp
[root@pma1 ~]#mkdir /apps/nginx/conf/conf.d
[root@pma1 ~]#cd /apps/nginx/conf/conf.d
[root@pma1 conf.d]#vim ../nginx.conf
...
http {
...
include /apps/nginx/conf/conf.d/*.conf;
}
[root@pma1 ~]#cat /apps/nginx/conf/conf.d/www.wenzi.com.conf
server {
listen 80;
server_name www.wenzi.com;
root /data/www/; #php项目路径
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@pma1 conf.d]#systemctl reload nginx
[root@pma1 conf.d]#mkdir /data/www -p
[root@pma1 conf.d]#chown -R www-data:www-data /data/www
--------------------------------------------配置php-fpm
[root@pma1 conf.d]#apt -y install php-fpm php-mysql php-redis php-json php-mbstring
[root@pma1 conf.d]#vim /etc/php/8.1/fpm/php.ini
date.timezone = Asia/Shanghai #设置PHP的默认时区
[root@pma1 conf.d]#vim /etc/php/8.1/fpm/pool.d/www.conf
;listen = /run/php/php8.1-fpm.sock
listen = 9000
#下面两行加入文件最后
#设置PHP会话的保存处理器为redis。PHP将会话数据存储在Redis中,而不是默认的文件系统中
php_value[session.save_handler] = redis
#指定Redis服务器的连接信息。192.168.28.52是Redis服务器的IP地址,而6379是Redis服务器的默认端口
php_value[session.save_path] = "tcp://192.168.28.52:6379"
--------session默认保存在本地/var/lib/php/sessions/ 目录下
[root@pma1 conf.d]#systemctl restart php8.1-fpm.service
[root@pma1 conf.d]#ss -tnl | grep 9000
LISTEN 0 511 *:9000 *:*
--------------------------------------------部署phpmyadmin
[root@pma1 ~]#unzip phpMyAdmin-5.2.1-all-languages.zip -d /opt/
[root@pma1 ~]#mv /opt/phpMyAdmin-5.2.1-all-languages/* /data/www/
[root@pma1 ~]#cp -a /data/www/config.sample.inc.php /data/www/config.inc.php
[root@pma1 ~]#vim /data/www/config.inc.php
$cfg['Servers'][$i]['host'] = '192.168.28.51'; #把localhost改为数据库mysql所在主机
192.168.28.60
--------------------------------------------部署nginx反向代理负载均衡
#通过脚本安装
[root@nginx ~]#nginx -V
nginx version: nginx/1.22.1
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx ~]#tree -d /apps/nginx/
/apps/nginx/
├── client_body_temp
├── conf
├── fastcgi_temp
├── html
├── logs
├── proxy_temp
├── sbin
├── scgi_temp
└── uwsgi_temp
[root@nginx ~]#mkdir /apps/nginx/conf/conf.d
[root@nginx ~]#cd /apps/nginx/conf/conf.d
[root@nginx conf.d]#vim ../nginx.conf
...
http {
...
include /apps/nginx/conf/conf.d/*.conf;
}
[root@nginx conf.d]#cat www.wenzi.com.conf
upstream webservers {
server 192.168.28.61;
server 192.168.28.62;
}
server {
listen 80;
server_name www.wenzi.com;
location / {
proxy_pass http://webservers;
proxy_set_header Host $http_host;
}
}
测试访问
浏览器访问 www.wenzi.com
刷新界面可看到用户信息处已经改变,nginx中upstream的轮询
在redis服务器192.168.28.52上查看会话缓存
通过phpmyadmin创建一个数据库wenzi,在数据库192.168.28.52上查询验证