利用LNMP实现wordpress站点搭建

环境准备

10.0.0.7:nginx+php-fpm+wordpress

10.0.0.17:mariadb

已安装的软件:MariaDB-10.4.22、Nginx-1.18.0

1 部署数据库

1.1创建wordpress数据库和用户并授权
MariaDB [mysql]> create database wordpress;
MariaDB [mysql]> GRANT ALL PRIVILEGES ON wordpress.* TO wordpress@'10.0.0.%' IDENTIFIED BY 'WordPress@2022.';
MariaDB [mysql]> FLUSH PRIVILEGES;

1.2验证MySQL账户权限
[root@mariadb ~]#mysql -uwordpress -pWordPress@2022. -h10.0.0.17
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+

2 部署PHP

在10.0.0.7主机部署php-fpm服务

2.1编译安装 php
[root@nginx ~]#yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
[root@nginx ~]#cd /usr/local/src
[root@nginx src]#wget https://www.php.net/distributions/php-7.4.11.tar.xz
[root@nginx src]#ll -h php-7.4.11.tar.xz
-rw-r--r-- 1 root root 9.9M Sep 29  2020 php-7.4.11.tar.xz
[root@nginx src]#tar xf php-7.4.11.tar.xz
[root@nginx src]#cd php-7.4.11
[root@nginx php-7.4.11]# ./configure \
> --prefix=/apps/php74 \
> --enable-mysqlnd \
> --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --with-openssl \
> --with-zlib \
> --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d \
> --enable-mbstring \
> --enable-xml \
> --enable-sockets \
> --enable-fpm \
> --enable-maintainer-zts \
> --disable-fileinfo

[root@nginx php-7.4.11]#make -j 2 && make install

2.2准备 php 配置文件
#生成配置文件
[root@nginx php-7.4.11]#cp php.ini-production /etc/php.ini
[root@nginx php-7.4.11]#cd /apps/php74/etc
[root@nginx etc]#cp php-fpm.conf.default php-fpm.conf
[root@nginx etc]#cd php-fpm.d/
[root@nginx php-fpm.d]#cp www.conf.default www.conf
[root@nginx php-fpm.d]#vim www.conf
[root@nginx php-fpm.d]#grep '^[^;]' www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000  #监听地址及IP
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /pm_status
ping.path = /ping
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow  #慢日志路径

#创建用户
[root@nginx php-fpm.d]#useradd -r -s /sbin/nologin www

#创建访问日志文件路径
[root@nginx php-fpm.d]#mkdir /apps/php74/log


[root@nginx ~]#vim /etc/php.ini
expose_php = Off  #关闭PHP版本显示
post_max_size = 30M  #设置最大上传数据大小,默认值为8M
upload_max_filesize = 20M  #设置最大上传文件,默认值为2M
2.3启动并验证 php-fpm 服务
[root@nginx ~]#/apps/php74/sbin/php-fpm -t
[root@nginx ~]#cp /usr/local/src/php-7.4.11/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@nginx ~]#systemctl daemon-reload
[root@nginx ~]#systemctl enable --now php-fpm
[root@nginx ~]#ss -ntlp
State      Recv-Q Send-Q              Local Address:Port                             Peer Address:Port
LISTEN     0      128                     127.0.0.1:9000                                        *:*users:(("php-fpm",pid=112880,fd=9),("php-fpm",pid=112879,fd=9),("php-fpm",pid=112878,fd=7))
LISTEN     0      128                             *:80                                          *:*users:(("nginx",pid=2134,fd=6),("nginx",pid=1305,fd=6))
LISTEN     0      128                             *:22                                          *:*users:(("sshd",pid=1293,fd=3))
LISTEN     0      100                     127.0.0.1:25                                          *:*users:(("master",pid=1391,fd=13))
LISTEN     0      128                          [::]:22                                       [::]:*users:(("sshd",pid=1293,fd=4))
LISTEN     0      100                         [::1]:25                                       [::]:*users:(("master",pid=1391,fd=14))
[root@nginx ~]#pstree -p |grep php
           |-php-fpm(112878)-+-php-fpm(112879)
           |                 `-php-fpm(112880)

[root@nginx ~]#ps -ef |grep php
root     112878      1  0 22:32 ?        00:00:00 php-fpm: master process (/apps/php74/etc/php-fpm.conf)
www      112879 112878  0 22:32 ?        00:00:00 php-fpm: pool www
www      112880 112878  0 22:32 ?        00:00:00 php-fpm: pool www
root     112896   3200  0 22:37 pts/0    00:00:00 grep --color=auto php

2.4准备 php 测试页
[root@nginx ~]#mkdir -p /data/nginx/wordpress
[root@nginx ~]#vim /data/nginx/wordpress/test.php
<?php
  phpinfo();
?>
2.5验证 php 测试页
[root@client ~]#curl http://www.linux2022.cn/ping
pong[root@client ~]#curl http://www.linux2022.cn/pm_status
pool:                 www
process manager:      dynamic
start time:           08/Apr/2022:08:35:11 +0800
start since:          1622
accepted conn:        7
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       1
active processes:     1
total processes:      2
max active processes: 1
max children reached: 0
slow requests:        0

在这里插入图片描述

2.6配置 php 开启 opcache 加速
#编辑php.ini配置文件
[root@nginx ~]#vim /etc/php.ini
[opcache]
; Determines if Zend OPCache is enabled
zend_extension=opcache.so
opcache.enable=1

[root@nginx ~]#systemctl restart php-fpm

2.7访问测试页确认开启opcache加速 在这里插入图片描述

3 部署 Nginx

3.1配置 Nginx 支持 fastcgi
[root@nginx ~]#vim /apps/nginx/conf/nginx.conf
[root@nginx ~]#grep -Ev '#|^$' /apps/nginx/conf/nginx.conf
worker_processes  1;
pid        /apps/nginx/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    server_tokens off;  #不在响应报文的Server首部显示nginx版本
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    client_max_body_size 100m; #设置允许客户端上传单个文件的最大值
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.linux2022.cn; #指定主机名
        location / {
            root   /data/nginx/wordpress; #指定数据目录
            index index.php index.html index.htm; #指定默认主页
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {  #实现php-fpm
            root           /data/nginx/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;  #fastcgi默认的主页资源
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  #设置脚本路径
            include        fastcgi_params; #此文件默认系统已提供,存放的相对路径为prefix/conf
            fastcgi_hide_header X-Powered-By; #隐藏响应头X-Powered-By信息
        }
        location ~ ^/(ping|pm_status)$ {  #实现状态页
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
        }
    }
    include    /apps/nginx/conf/conf.d/*.conf;
}

[root@nginx ~]#nginx -t
[root@nginx ~]#nginx -s reload

4 部署 WordPress

在10.0.0.7主机部署 wordpress

4.1准备 WordPress 文件
[root@nginx ~]#cd /usr/local/src
[root@nginx src]#tar xf latest-zh_CN.tar.gz
[root@nginx src]#cp -r wordpress/* /data/nginx/wordpress
[root@nginx src]#chown -R www.www /data/nginx/wordpress/
4.2初始化web页面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r57tZNHl-1649392634609)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\1649383196913.png)]

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.3登录后台管理界面并发表文章

在这里插入图片描述

在这里插入图片描述

4.4验证发表的文章

在这里插入图片描述

本内容为合法授权发布,文章内容为作者独立观点,不代表开发云立场,未经允许不得转载。

CSDN开发云