LNMP + WordPress 搭建个人博客

Linux

活动买的云服务器,2核4G5M带宽40G硬盘,CentOS7.6

[root@instance-djx5qyum ~]# cat /etc/redhat-release
System_Version

Nginx

编译安装 Nginx1.14.1 版本

[root@instance-djx5qyum ~]# /usr/local/nginx/sbin/nginx -v
Nginx_Version

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
tar -zxf nginx-1.14.1.tar.gz
cd nginx-1.14.1/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module
make && make install

/usr/local/nginx/sbin/nginx

Mysql

快速安装Mysql 5.7.22社区版

[root@instance-djx5qyum ~]# mysql --help | grep Distrib
Mysql_Version

#需将mysql社区版压缩包和配置文件my.cnf上传至root家目录下
#社区版:https://dev.mysql.com/downloads/file/?id=476857
#卸载自带的maridb

rpm -qa | grep mariadb | xargs rpm -e --nodeps

#提供server perl支持 依赖net-tools 最小化安装的系统是没有自带的
yum install perl net-tools numactl -y

#解压rpm包
tar -xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar 
rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm 
rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm 
rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm 

#清理安装包
rm -rf mysql-community-*
rm -rf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar

#启动一次mysql生成初始文件目录
systemctl start mysqld
systemctl stop mysqld

#新建mysqldb用户
useradd mysqldb

#将初始日志目录权限给mysqldb用户以免初始化出错
chown mysqldb:mysqldb /var/log/mysqld.log 

#-c<指令>或--command=<指令>:执行完指定的指令后,即恢复原来的身份;
#新建mysql所需目录
su - mysqldb -c 'mkdir db0';
su - mysqldb -c 'cd db0/;mkdir etc data log';

#移动mysql配置文件到用户目录下并修改权限
mv /root/my.cnf /home/mysqldb/db0/etc/
chown mysqldb:mysqldb /home/mysqldb/db0/etc/my.cnf

#新增启动脚本
su - mysqldb -c 'cat > ~/db0/startupmysql.sh << EOF
nohup mysqld --defaults-file=/home/mysqldb/db0/etc/my.cnf --datadir=/home/mysqldb/db0/data --socket=/home/mysqldb/db0/mysql.sock --pid-file=/home/mysqldb/db0/mysqld.pid --user=mysqldb &
EOF';

su - mysqldb -c 'chmod +x ~/db0/startupmysql.sh';

#新增关闭脚本
su - mysqldb -c 'cat > ~/db0/shutdownmysql.sh << EOF
mysqladmin -uroot -p --socket=/home/mysqldb/db0/mysql.sock shutdown
EOF';

su - mysqldb -c 'chmod +x ~/db0/shutdownmysql.sh';

#初始化mysql
su - mysqldb -c 'mysqld --initialize --user=mysqldb --datadir=/home/mysqldb/db0/data';

#启动mysql
su - mysqldb -c '/bin/sh /home/mysqldb/db0/startupmysql.sh';

#查看初始root密码
cat /var/log/mysqld.log | grep password

#用户和数据库初始化
mysql -u root -p --socket=/home/mysqldb/db0/mysql.sock

alter user root@localhost identified by '{password}';

create database wordpress_db;

grant all privileges on wordpress_db.* to wordpress@'%' identified by '{password}';

flush privileges;

PHP7

编译安装php-7.3.8

yum install -y freetype-devel libevent libevent-devel libxml2 libxml2-devel ncurses ncurses-devel  openssl openssl-devel  libjpeg libjpeg-devel libpng libpng–devel zlib-devel  bzip2 bzip2-devel libmcrypt libmcrypt-devel curl-devel libxslt libxslt-devel
#yum安装libzip版本低,需要手动编译安装;又依赖于cmake,同样版本低需要手动编译安装
##cmake
yum remove cmake
cd /opt
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5-Linux-x86_64.tar.gz
tar zxvf cmake-3.14.5-Linux-x86_64.tar.gz
vi /etc/profile.d/cmake.sh
###添加
export CMAKE_HOME=/opt/cmake-3.14.5-Linux-x86_64
export PATH=$PATH:$CMAKE_HOME/bin
###
source /etc/profile
cmake -version

#libzip
wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build
cd build
cmake ..
make -j4 && make install

wget https://www.php.net/distributions/php-7.3.8.tar.gz
tar -xvf php-7.3.8.tar.gz
cd php-7.3.8
mkdir /usr/local/php7
./configure --prefix=/usr/local/php7 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
make && make install
groupadd php
useradd -g php php
cp php-7.3.8/php.ini-development /usr/local/php7/etc/php.ini
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
vim /usr/local/php7/etc/php-fpm.d/www.conf
###添加
user = php
group = php
###
cd /usr/local/php7/sbin
./php-fpm
vi /etc/profile.d/php.sh

PATH=$PATH:/usr/local/php7/bin
export PATH

source /etc/profile
php -v
vi /usr/local/php7/etc/php-fpm.conf

pid = /var/run/php-fpm.pid
vim /usr/lib/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload
# 如果之前有php-fpm进程,需要先全部kill掉
systemctl start php-fpm

WordPress

中文最新版 wordpress-5.3.2-zh_CN.tar.gz, 需要依赖于高版本PHP

# 创建软链接
ln -s /usr/local/nginx/html/wordpress/ /root/wordpress_ln

# 配置mysql连接信息
cd /usr/local/nginx/html/wordpress/
mv wp-config-sample.php wp-config.php
vi wp-config.php

# 初始化wordpress
http://{Public IP}/wp-admin/install.php

wordpress主题优化

  1. 显示无文章的分类
    在当前应用主题下的functions.php里添加
    add_filter( 'widget_categories_args', 'wpdx_show_empty_cats' );
    function wpdx_show_empty_cats($cat_args) {
      $cat_args['hide_empty'] = 0;
      return $cat_args;
    }

迁移

注意事项

# 更改mysql连接信息
vi /usr/local/nginx/html/wordpress/wp-config.php
###重点修改
define( 'DB_HOST', '{Private IP}:{Database port}' );
###

#解压带绝对路径的Nginx压缩包
tar -zxPf {nginx}.tar.gz

#自行切换nossl和ssl配置文件
cd /usr/local/nginx/conf/conf.d/
###nossl需要修改server_name
server_name {Public IP}
###

#导入mysql dump数据
mysql -uroot -p wordpress_db --socket=/home/mysqldb/db0/mysql.sock < {mysql}.dump

# mysql常用命令
show databases;
use wordpress_db;
show tables;
select * from {table_name} limit 0,10;

PHP8升级

编译安装php-8.2.0

# 如果之前有php-fpm进程,需要先全部kill掉
systemctl stop php-fpm

# sqlite-devel
yum -y install sqlite-devel

# oniguruma
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
./autogen.sh && ./configure --prefix=/usr
make && make install

# php8
wget https://www.php.net/distributions/php-8.2.0.tar.gz
tar -zxf php-8.2.0.tar.gz
cd php-8.2.0/
mkdir /usr/local/php8

./configure --prefix=/usr/local/php8 --with-curl --with-gettext --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-gd

make && make install

cp php-8.2.0/php.ini-development /usr/local/php8/etc/php.ini
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
vim /usr/local/php8/etc/php-fpm.d/www.conf
###添加
user = php
group = php
###
vi /etc/profile.d/php.sh

PATH=$PATH:/usr/local/php8/bin
export PATH

source /etc/profile
# 需要重新打开终端
php -v
vi /usr/local/php8/etc/php-fpm.conf

pid = /var/run/php-fpm.pid
vim /usr/lib/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/php8/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload
# 如果之前有php-fpm进程,需要先全部kill掉
systemctl start php-fpm

1 comment

  1. This article offers a fascinating perspective on the subject. The depth of research and clarity in presentation make it a valuable read for anyone interested in this topic. It’s refreshing to see such well-articulated insights that not only inform but also provoke thoughtful discussion. I particularly appreciated the way the author connected various aspects to provide a comprehensive understanding. It’s clear that a lot of effort went into compiling this piece, and it certainly pays off. Looking forward to reading more from this author and hearing other readers’ thoughts. Keep up the excellent work!

Leave a Reply

Your email address will not be published. Required fields are marked *