镜像下载、域名解析、时间同步请点击
阿里巴巴开源镜像站-opsx镜像站-阿里云开发者社区
lnmp是linux + nginx + mysql + php 四个系统的首字母缩写,相对于 lamp(linux + apache + mysql + php )来说的。曾经在虚拟主机建站界风靡一时,随着新的编程语言和容器技术、微服务等发展,慢慢没落了,尤其是php编程语言的使用量急剧下降了。
wordpress是一款能让您建立出色网站、博客或应用程序的开源软件。它具有美观的设计,强大的功能,可以助您自由发挥心中所想。wordpress既是免费的,也是无价的。
# 本实践过程中的系统及环境描述l:linux https://mirrors.aliyun.com/centos/n:nginx https://nginx.org/en/download.htmlm:mysql https://dev.mysql.com/downloads/mysql/p:php http://php.net/downloads.phpwordpress https://cn.wordpress.org/latest-zh_cn.tar.gz#部署规划:192.168.250.47:nginx php-fpm 运行web服务192.168.250.48:运行mysql数据库,redis服务
1. 架构拓扑及主机说明
# 三台主机1 1台 linux+nginx+php+wordpress (简称 lnp) 服务器 :主机名:lnp-server-ip47centos 7.9ip:192.168.250.472 1台 mysql+redis 服务器 :主机名: mysql-redis-ip48centos 8.4ip:192.168.250.48/243 1台 client主机 :win10-pc机
2. 准备 mysql 数据库
# centos系统的优化,可以查以前的文章;按照架构图修改好主机名[root@centos84-ip48 ]#hostnamectl set-hostname mysql-redis-ip48[root@centos84-ip48 ]#exit# yum 安装 mysql-server 数据库[root@mysql-redis-ip48 ]#yum info mysql-serverlast metadata expiration check: 19:31:21 ago on mon 28 mar 2022 02:34:38 am cst.available packagesname : mysql-serverversion : 8.0.26[root@mysql-redis-ip48 ]#yum -y install mysql-server# 启动服务并开启自启[root@mysql-redis-ip48 ]#systemctl enable --now mysqld# 进入数据库[root@mysql-redis-ip48 ]#mysqlwelcome to the mysql monitor. commands end with ; or g.your mysql connection id is 8server version: 8.0.26 source distributioncopyright (c) 2000, 2021, oracle and/or its affiliates.oracle is a registered trademark of oracle corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners.type 'help;' or 'h' for help. type 'c' to clear the current input statement.# 创建 wordpress 库mysql> create database wordpress;query ok, 1 row affected (0.00 sec)# 创建wordpress的数据库账户名和密码mysql> create user wordpress@'192.168.250.%' identified by '123456';query ok, 0 rows affected (0.01 sec)# 数据库授权mysql> grant all on wordpress.* to wordpress@'192.168.250.%';query ok, 0 rows affected (0.01 sec)# 本机登录并验证数据库mysql> show databases;+--------------------+| database |+--------------------+| information_schema || mysql || performance_schema || sys || wordpress |+--------------------+5 rows in set (0.01 sec)mysql> use wordpressdatabase changedmysql> show tables;empty set (0.00 sec)mysql> quitbye[root@mysql-redis-ip48 ]#
3. 网络验证mysql服务
# 通过网络在另外一台机器上登录上面建好的数据库服务器# 安装数据库客户端 mysql 包[root@centos84-ip172-48 ]#yum -y install mysql# 网络方式登录远程数据库[root@centos84-ip172-48 ]#mysql -uwordpress -p123456 -h192.168.250.48mysql: [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 9server version: 8.0.26 source distributioncopyright (c) 2000, 2021, oracle and/or its affiliates.oracle is a registered trademark of oracle corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners.type 'help;' or 'h' for help. type 'c' to clear the current input statement.mysql> show databases;+--------------------+| database |+--------------------+| information_schema || wordpress |+--------------------+2 rows in set (0.00 sec)mysql>
4. 配置 lnp 服务器
基本任务: 编译安装和部署 php 支持 redis,并准备配置和启动服务文件,启动 php-fpm; 编译安装nginx ,并准备配置和启动服务文件,启动nginx
4.1 部署php-fpm服务
# 按照架构图修改好主机名[root@centos79 <sub>]# hostnamectl set-hostname lnp-server-ip47[root@centos79 </sub>]# exit# 安装编译php需要的依赖包[root@lnp-server-ip47 ]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel# 下载 php-7.4.28.tar.xz 源码包[root@lnp-server-ip47 src]# wget https://www.php.net/distributions/php-7.4.28.tar.xz[root@lnp-server-ip47 src]# ll -h php-7.4.28.tar.xz -rw-r--r-- 1 root root 10m feb 15 21:40 php-7.4.28.tar.xz# 解压源码包,进入源码包所在目录[root@lnp-server-ip47 src]# tar xf php-7.4.28.tar.xz [root@lnp-server-ip47 src]# lltotal 11220drwxr-xr-x 9 1001 1001 186 mar 28 17:06 nginx-1.20.2-rw-r--r-- 1 root root 1062124 nov 16 22:51 nginx-1.20.2.tar.gzdrwxrwxr-x 16 root root 4096 feb 15 21:23 php-7.4.28-rw-r--r-- 1 root root 10418352 feb 15 21:40 php-7.4.28.tar.xz# 准备编译参数[root@lnp-server-ip47 src]#cd php-7.4.28/[root@lnp-server-ip47 php-7.4.28]# ./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....................................thank you for using php. # 需要看到这个信息才算成功了# 查看cpu个数,作为编译参数cpu选项输入[root@lnp-server-ip47 nginx-1.20.2]# lscpuarchitecture: x86_64cpu op-mode(s): 32-bit, 64-bitbyte order: little endiancpu(s): 8on-line cpu(s) list: 0-7[root@lnp-server-ip47 php-7.4.28]# # 编译安装[root@lnp-server-ip47 php-7.4.28]# make -j 8 && make install................... #此处删除很多屏显内容,需要看到下面成功信息再进入下一步build complete.don't forget to run 'make test'.installing shared extensions: /apps/php74/lib/php/extensions/no-debug-zts-20190902/installing php cli binary: /apps/php74/bin/installing php cli man page: /apps/php74/php/man/man1/installing php fpm binary: /apps/php74/sbin/installing php fpm defconfig: /apps/php74/etc/installing php fpm man page: /apps/php74/php/man/man8/installing php fpm status page: /apps/php74/php/php/fpm/installing phpdbg binary: /apps/php74/bin/installing phpdbg man page: /apps/php74/php/man/man1/installing php cgi binary: /apps/php74/bin/installing php cgi man page: /apps/php74/php/man/man1/installing build environment: /apps/php74/lib/php/build/installing header files: /apps/php74/include/php/installing helper programs: /apps/php74/bin/ program: phpize program: php-configinstalling man pages: /apps/php74/php/man/man1/ page: phpize.1 page: php-config.1/usr/local/src/php-7.4.28/build/shtool install -c ext/phar/phar.phar /apps/php74/bin/phar.pharln -s -f phar.phar /apps/php74/bin/pharinstalling pdo headers: /apps/php74/include/php/ext/pdo/################################################################################ 准备 php 配置文件# 从配置文件模板复制,并进行修改[root@lnp-server-ip47 php-7.4.28]# cp /usr/local/src/php-7.4.28/php.ini-production /etc/php.ini# 进入当时编译参数内定义的目录 /apps/php74/ 从模板复制创建 php-fpm.conf[root@lnp-server-ip47 php-7.4.28]# cd /apps/php74/etc[root@lnp-server-ip47 etc]# cp php-fpm.conf.default php-fpm.conf# 进入子配置文件目录,从模板 文件创建 www.conf [root@lnp-server-ip47 etc]# cd php-fpm.d/[root@lnp-server-ip47 php-fpm.d]# cp www.conf.default www.conf [root@lnp-server-ip47 php-fpm.d]# # 按照本实践的思路修改 www.conf[root@lnp-server-ip47 php-fpm.d]# vim www.conf;user = nobodyuser = www;group = nobodygroup = www;pm.status_path = /statuspm.status_path = /status;ping.path = /pingping.path = /ping;access.log = log/$pool.access.logaccess.log = log/$pool.access.log ;slowlog = log/$pool.log.slowslowlog = log/$pool.log.slow # 修改后的 www.conf 文件去除 ; 注释行的所有文件内容 供比对[root@lnp-server-ip47 php-fpm.d]# grep '^[^;]' www.conf[www]user = wwwgroup = wwwlisten = 127.0.0.1:9000pm = dynamicpm.max_children = 5pm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3pm.status_path = /statusping.path = /pingaccess.log = log/$pool.access.logslowlog = log/$pool.log.slow [root@lnp-server-ip47 php-fpm.d]# # 创建 www 用户[root@lnp-server-ip47 php-fpm.d]# useradd -r -s /sbin/nologin www# 创建访问日志文件路径[root@lnp-server-ip47 php-fpm.d]# mkdir /apps/php74/log[root@lnp-server-ip47 php-fpm.d]# ################################################################################ 启动并验证 php-fpm 服务# 检查配置文件语法等[root@lnp-server-ip47 php-fpm.d]# /apps/php74/sbin/php-fpm -t[28-mar-2022 18:05:51] notice: configuration file /apps/php74/etc/php-fpm.conf test is successful# 准备启动服务文件[root@lnp-server-ip47 php-fpm.d]# cp /usr/local/src/php-7.4.28/sapi/fpm/php-fpm.service /usr/lib/systemd/system/# 启动并开机自启动 php-fpm[root@lnp-server-ip47 php-fpm.d]# systemctl daemon-reload[root@lnp-server-ip47 php-fpm.d]# systemctl enable --now php-fpmcreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.# 验证监听端口[root@lnp-server-ip47 php-fpm.d]# ss -ltnstate recv-q send-q local address:port peer address:port listen 0 511 127.0.0.1:9000 *:* # 查看并跟踪 进程信息[root@lnp-server-ip47 php-fpm.d]# pstree -p |grep php |-php-fpm(20700)-+-php-fpm(20701) | `-php-fpm(20702)[root@lnp-server-ip47 php-fpm.d]# ps -ef |grep phproot 20700 1 0 18:06 ? 00:00:00 php-fpm: master process (/apps/php74/etc/php-fpm.conf)www 20701 20700 0 18:06 ? 00:00:00 php-fpm: pool wwwwww 20702 20700 0 18:06 ? 00:00:00 php-fpm: pool wwwroot 20707 5036 0 18:07 pts/0 00:00:00 grep --color=auto php[root@lnp-server-ip47 php-fpm.d]#
4.2 部署 nginx 服务
4.2.1 编译安装 nginx
#### 编译安装 nginx# 准备nginx编译安装的依赖包[root@lnp-server-ip47 <sub>]# yum -y install gcc pcre-devel openssl-devel zlib-devel# 下载 nginx 1.20.2 源码包 一般/usr/local/src/ 作为源码文件存放目录[root@lnp-server-ip47 </sub>]# cd /usr/local/src/[root@lnp-server-ip47 src]# wget http://nginx.org/download/nginx-1.20.2.tar.gz# 解压源码包[root@lnp-server-ip47 src]# tar xf nginx-1.20.2.tar.gz[root@lnp-server-ip47 src]# lltotal 1040drwxr-xr-x 8 1001 1001 158 nov 16 22:44 nginx-1.20.2-rw-r--r-- 1 root root 1062124 nov 16 22:51 nginx-1.20.2.tar.gz[root@lnp-server-ip47 src]# # 进入nginx-1.20.2 目录,准备编译参数[root@lnp-server-ip47 src]# cd nginx-1.20.2[root@lnp-server-ip47 nginx-1.20.2]# ./configure --prefix=/apps/nginx > --user=www > --group=www > --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@lnp-server-ip47 nginx-1.20.2]# make -j 8 && make install############################################################################### 准备服务文件并启动 nginx[root@lnp-server-ip47 nginx-1.20.2]# vim /usr/lib/systemd/system/nginx.service[root@lnp-server-ip47 nginx-1.20.2]# cat /usr/lib/systemd/system/nginx.service[unit]description=nginx - high performance web serverdocumentation=http://nginx.org/en/docs/after=network-online.target remote-fs.target nss-lookup.targetwants=network-online.target[service]type=forkingpidfile=/apps/nginx/run/nginx.pidexecstart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.confexecreload=/bin/kill -s hup $mainpidexecstop=/bin/kill -s term $mainpid[install]wantedby=multi-user.target[root@lnp-server-ip47 nginx-1.20.2]## 创建目录[root@lnp-server-ip47 nginx-1.20.2]# mkdir /apps/nginx/run/# 修改配置文件[root@lnp-server-ip47 nginx-1.20.2]# vim /apps/nginx/conf/nginx.conf# 仅修改下面这行的内容pid /apps/nginx/run/nginx.pid; # 启动并开机自启服务[root@lnp-server-ip47 wordpress]# systemctl daemon-reload[root@lnp-server-ip47 wordpress]# systemctl enable --now nginx[root@lnp-server-ip47 wordpress]# ss -tlnstate recv-q send-q local address:port peer address:port listen 0 511 127.0.0.1:9000 *:*listen 0 511 *:80 *:* [root@lnp-server-ip47 wordpress]#
4.2.2 配置 nginx 支持 fastcgi
################################################################################## 配置 nginx 支持 fastcgi[root@lnp-server-ip47 nginx-1.20.2]# vim /apps/nginx/conf/nginx.conf# 仅仅修改下面这些内容,其他都市默认值worker_processes auto;pid /apps/nginx/run/nginx.pid; server { listen 80; server_name blog.shone.cn; location / { root /data/nginx/wordpress; index index.php index.html index.htm; } location <sub> .php$ { root /data/nginx/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } location </sub> ^/(ping|pm_status)$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param path_translated $document_root$fastcgi_script_name; }# 通过命令筛选出有效的配置行[root@lnp-server-ip47 nginx-1.20.2]# grep -ev '#|^#39; /apps/nginx/conf/nginx.confworker_processes auto;pid /apps/nginx/run/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name blog.shone.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 <sub> .php$ { #实现php-fpm root /data/nginx/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } location </sub> ^/(ping|pm_status)$ { #php检测状态页 include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param path_translated $document_root$fastcgi_script_name; } }}[root@lnp-server-ip47 nginx-1.20.2]## 重新启动 nginx 让新配置文件生效[root@lnp-server-ip47 php-fpm.d]# systemctl reload nginx[root@lnp-server-ip47 php-fpm.d]# ss -ltnstate recv-q send-q local address:port peer address:port listen 0 511 127.0.0.1:9000 *:* listen 0 511 *:80 *:* [root@lnp-server-ip47 php-fpm.d]#
4.2.3 测试php工作是否正常
准备测试页面
[root@lnp-server-ip47 php-fpm.d]# mkdir -p /data/nginx/wordpress[root@lnp-server-ip47 php-fpm.d]# vim /data/nginx/wordpress/phpinfo.php[root@lnp-server-ip47 php-fpm.d]# cat /data/nginx/wordpress/phpinfo.php<?phpphpinfo();?>
测试php的ping
查看状态页
5. 部署 wordpress
5.1 准备 wordpress 文件
# 下载源文件,并复制到前面定义的网页目录下,并修改权属[root@lnp-server-ip47 <sub>]# wget https://cn.wordpress.org/latest-zh_cn.tar.gz[root@lnp-server-ip47 </sub>]# lltotal 19012-rw-r--r-- 1 root root 19462197 mar 19 00:00 latest-zh_cn.tar.gz[root@lnp-server-ip47 <sub>]# tar xf latest-zh_cn.tar.gz [root@lnp-server-ip47 </sub>]# lltotal 19016-rw-r--r-- 1 root root 19462197 mar 19 00:00 latest-zh_cn.tar.gzdrwxr-xr-x 5 1006 1006 4096 mar 19 00:00 wordpress[root@lnp-server-ip47 <sub>]# cp -r wordpress/* /data/nginx/wordpress[root@lnp-server-ip47 </sub>]# chown -r www.www /data/nginx/wordpress/[root@lnp-server-ip47 ~]#
5.2 初始化 wordpress
# 修改win10的本地hosts文件,路径为 c:windowssystem32driversetchosts 在最后添加一行192.168.250.47 blog.shone.cn在浏览器内输入 blog.shone.cn 出现可道云的初始化向导,按照向导完成初始化
在浏览器内输入 http://blog.shone.cn
6. 优化 wordpress
6.1 允许上传大文件
#注意:默认只支持1m以下文件上传,要利用php程序上传大文件,需要修改下面的配置,最大上传由下列项值的最小值决定,直接上传大于1m文件,会出现下面413错误[root@lnp-server-ip47 wordpress]# vim /apps/nginx/conf/nginx.confhttp { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; client_max_body_size 100m; #nginx上传文件大小修改成100m,默认1m ......... [root@lnp-server-ip47 wordpress]# vim /etc/php.ini; http://php.net/post-max-size;post_max_size = 8m # 默认值为8mpost_max_size = 30m;upload_max_filesize = 2m # 默认值为2mupload_max_filesize = 20m [root@lnp-server-ip47 wordpress]# systemctl restart nginx php-fpm
6.2 安全加固
# 关闭版本显示[root@lnp-server-ip47 wordpress]# grep -ev '#|^#39; /apps/nginx/conf/nginx.confworker_processes auto;pid /apps/nginx/run/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; client_max_body_size 100m; sendfile on; keepalive_timeout 65; server { listen 80; server_name blog.shone.cn; server_tokens off; # 安全加固选项 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 <sub> .php$ { root /data/nginx/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_hide_header x-powered-by; # 安全加固选项 } location </sub> ^/(ping|pm_status)$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param path_translated $document_root$fastcgi_script_name; } }}[root@lnp-server-ip47 wordpress]# # 关闭 php版本暴露[root@lnp-server-ip47 wordpress]# vim /etc/php.ini; http://php.net/expose-php;expose_php = on # 默认值为on 可以在客户端看到版本信息expose_php = off
6.3 配置 php 开启 opcache 加速
[root@lnp-server-ip47 wordpress]# vim /etc/php.ini.....................[opcache]; determines if zend opcache is enabledzend_extension=opcache.so opcache.enable=1.......................[root@lnp-server-ip47 wordpress]#systemctl restart php-fpm
本文转自:https://blog.51cto.com/shone/5165650