如何利用 LNMP 搭建 WordPress 站点

在这个信息爆炸的时代,拥有一个能够迅速传达信息、展示个性、并能够与世界互动的在线平台,已成为企业和个人的基本需求。WordPress,以其无与伦比的易用性和强大的扩展性,成为了构建此类平台的首选工具。而LNMP,这个由Linux、Nginx、MySQL和PHP组成的强大组合,为WordPress提供了一个稳定而高效的运行环境。本文将带您走进搭建WordPress站点的奇妙旅程,从0开始,利用LNMP的强大功能,打造一个既充满个性又能满足业务需求的在线家园。

LNMP项目实战环境说明

L:Linux(CentOS7)https://mirrors.aliyun.com/centos/7/isos/x86_64/

N:Nginx(1.18.0) https://nginx.org/en/download.html

M:MySQL(8.0.19) https://dev.mysql.com/downloads/mysql/

P:PHP(7.4.10) http://php.net/downloads.php

Wordpress(5.4.2):https://cn.wordpress.org/download/

#部署规划:

192.168.26.201:Nginx php-fpm 运行web服务

192.168.26.202:运行MySQL数据库


1.1 部署数据库

在192.168.26.202主机部署MySQL服务

1.1.1 部署Mariadb数据库

1  [root@mariadb ~]# yum -y install mariadb-server

2  [root@mariadb ~]# systemctl enable --now mariadb.service

1.1.2 创建wordpress数据库和用户并授权

[root@mariadb ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> create database wordpress;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user wordpress@'192.168.26.%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.26.%';

Query OK, 0 rows affected (0.00 sec) 18

MariaDB [(none)]>

1.1.3 验证MySQL账户权限

在WordPress服务器使用授权的MySQL账户远程登录测试权限

[root@web ~]# mysql -uwordpress -p123456 -h192.168.26.202

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.+--------------------+| Database            |+--------------------+| information_schema || test                || wordpress          |+--------------------+3 rows in set (0.00 sec)

1.2 部署PHP

在192.168.26.201主机部署php-fpm服务

1.2.1 编译安装 php

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

[root@centos7 ~]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel

[root@centos7 ~]#cd /usr/local/src

[root@centos7 src]#wget https://www.php.net/distributions/php-7.4.11.tar.xz

[root@web src]# tar xf php-7.4.11.tar.xz

[root@web src]# ls

nginx-1.22.0  nginx-1.22.0.tar.gz  php-7.4.11  php-7.4.11.tar.xz

[root@web src]# cd php-7.4.11/

[root@centos7 php-7.4.11]# ./configure --prefix=/usr/local/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

......

config.status: creating main/php_config.h

config.status: executing default commands

+--------------------------------------------------------------------+

| 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@centos7 php-7.4.11]#make -j 8 && make install

1.2.2 准备PHP配置文件

#生成配置文件[root@centos7 php-7.4.11]#cp /usr/local/src/php-7.4.11/php.ini-production /etc/php.ini

[root@web php-7.4.11]# cd /usr/local/php74/etc/

[root@web etc]# cp php-fpm.conf.default php-fpm.conf

[root@web etc]# pwd

/usr/local/php74/etc

[root@web etc]# cd php-fpm.d/

[root@web php-fpm.d]# cp www.conf.defaultwww.conf

[root@web php-fpm.d]# vim www.conf

[root@centos7 php-fpm.d]#grep '^[^;]'www.conf

[www]

user = nginx

group = nginx

listen = 127.0.0.1:9000

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

1.2.3 启动并验证 php-fpm服务

[root@web php-fpm.d]# /usr/local/php74/sbin/php-fpm -t[01-Aug-2022 19:44:33] NOTICE: configuration file /usr/local/php74/etc/php- fpm.conf test is successful[root@web php-fpm.d]# /usr/local/php74/sbin/php-fpm

[root@web php-fpm.d]# ss -ntl

State Recv-Q Send-Q Local Address:Port

Peer Address:Port

LISTEN 0 100 127.0.0.1:25

*:*

LISTEN 0 128 127.0.0.1:9000

*:*

LISTEN 0 128 *:22

*:*

LISTEN 0 100 [::1]:25

[::]:*

LISTEN 0 128 [::]:22

[::]:*

[root@web php-fpm.d]#

[root@web php-fpm.d]# pstree -p |grep php

|-php-fpm(14119)-+-php-fpm(14120)

| `-php-fpm(14121)

[root@web php-fpm.d]# ps -ef |grep php

root 14119 1 0 19:45 ? 00:00:00 php-fpm: master process

(/usr/local/php74/etc/php-fpm.conf)

nobody 14120 14119 0 19:45 ? 00:00:00 php-fpm: pool www

nobody 14121 14119 0 19:45 ? 00:00:00 php-fpm: pool www

root 14134 35079 0 19:46 pts/2 00:00:00 grep --color=auto php

1.3 部署 Nginx

在192.168.26.201主机部署nginx服务

1.3.1 编译安装 nginx

1  [root@db1 ~]# vim install_nginx.sh

#!/bin/bash

# 安装必要的编译依赖

yum install -y gcc-c++ pcre-devel zlib-devel make

# 下载Nginx源码包

wget http://nginx.org/download/nginx-1.22.0.tar.gz

tar zxvf nginx-1.22.0.tar.gz

cd nginx-1.22.0

# 配置编译选项,这里只列出了一部分常用选项

./configure --prefix=/usr/local/nginx --with-http_ssl_module

# 编译和安装

make && make install

# 启动Nginx

/usr/local/nginx/sbin/nginx

2  [root@db1 ~]# /bin/bash install_nginx.sh

3  [root@db1 ~]# ss -ntl | grep 80

LISTEN    0      128          *:80                      *:*

LISTEN    0      128          *:8080                    *:*

LISTEN    0      80        [::]:3306                  [::]:*

LISTEN    0      128      [::]:80                    [::]:*

LISTEN    0      128      [::]:8080                  [::]:*

1.3.2 配置 Nginx 支持 fastcgi

[root@web ~]#vim /usr/local/nginx/conf/nginx.conf

[root@web ~]#grep -Ev '#|^$' vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;

events {

worker_connections  1024;

}

http {

include      mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

server {

listen      80;

server_name  localhost;

location / {

root  html;

index  index.php index.html index.htm;

}

error_page  500 502 503 504  /50x.html;

location = /50x.html { root  html;

}

location ~ \.php$ {

root          html;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

include        fastcgi_params;

}

}

}

[root@web ~]# /usr/local/nginx/sbin/nginx

1.4 部署 WordPress

在192.168.26.201主机部署 wordpress

1.4.1 准备 WordPress 文件

[root@web ~]# wget https://cn.wordpress.org/wordpress-6.0.1-zh_CN.tar.gz

[root@web ~]# tar xf wordpress-6.0.1-zh_CN.tar.gz

[root@web ~]#cp -r wordpress/* /usr/local/nginx/html[root@web html]# cp wp-config-sample.php wp-config.php[root@web html]# vim wp-config.php

define( 'DB_NAME', 'wordpress' );/** Database username */define( 'DB_USER', 'wordpress' );/** Database password */define( 'DB_PASSWORD', '123456' );/** Database hostname */define( 'DB_HOST', '192.168.26.202' );

[root@web html]# chown -R nginx.nginx .

1.4.2 初始化web页面

打开浏览器访问下面链接

192.168.26.201

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

1.4.4 验证发表的文章

#可以看到上传的图片

[[root@web html]# tree wp-content/uploads/wp-content/uploads/ └── 2022└── 08└── 1.png2 directories, 1 file

1.4.5 配置允许上传大文件

#注意:默认只支持1M以下文件上传 ,要利用php程序上传大图片 ,还需要修改下面三项配置 ,最大上传由 三项值的最小值决定

#直接上传大于1M文件 ,会出现下面413错误[root@centos7 ~]#tail -f /apps/nginx/logs/access.log10.0.0.1 - - [27/Nov/2020:12:21:16 +0800] "POST /wp-admin/async-upload.php

HTTP/1.1" 413 585 "http://10.0.0.7/wp-admin/upload.php" "Mozilla/5.0(Windows NT10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67

Safari/537.36 Edg/87.0.664.47"#nginx上传文件大小限制[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf

server {client_max_body_size 10m; #默认值为1M.....#php上传文件大小限制[root@centos7 ~]#vim /etc/php.ini

post_max_size = 30M  #默认值为8Mupload_max_filesize = 20M  #默认值为2M[root@centos7 ~]#systemctl restart nginx php-fpm

以上就是本期的全部内容。大家好,我是乐乐专注IT运维技术研究与分享,更多运维技巧欢迎关注乐维社区,更多运维问题也欢迎到乐维社区留言提问。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容