上一篇文章讲到了搬家前怎样备份WordPress和数据库,接下来就要正式开始搬家前的准备工作。准备工作最重要的部分,当然就是在新的VPS服务器上安装好最新版本的WordPress和相关的依赖组件啦。
我的VPS服务器是基于CentOS 7 + nginx,所以接下来所有的安装过程都是按照这个配置来进行的。如果你使用的是其他版本的Linux或者Apache,安装的细节会有些不一样,但是大致的过程基本都一样。另外本文的安装过程不涉及到运行WordPress网站的其它工作内容,如域名申请,数字签名申请等。这部分的内容有时间后续再整理。
安装的过程主要包含下面的几个部分:
- 安装MariaDB数据库
- 安装PHP
- 安装PHPMyAdmin
- 安装WordPress
CentOS 7的软件仓库的MariaDB数据库,PHP等的版本都比较低。这里我们都会安装比较新的版本,以达到更好的性能和安全性的要求。
安装MariaDB数据库
先添加MariaDB yum仓库:
sudo vim /etc/yum.repos.d/MariaDB.repo
在MariaDB.repo文件里增加如下内容:
[mariadb]
name = MariaDB
baseurl = https://mirrors.aliyun.com/mariadb/yum/10.6/centos7-amd64
gpgkey=https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
安装MariaDB 10.6:
sudo yum install MariaDB-server
设置开机启动MariaDB:
sudo systemctl start mariadb
sudo systemctl enable mariadb
使用MariaDB安全配置向导进行安全配置:
sudo mariadb-secure-installation
按照向导配置服务器安全信息:
Enter current password for root (enter for none): # (输入数据库root密码,第一次进入还没有设置密码则直接回车)
Switch to unix_socket authentication [Y/n]: # n (不使用unix_socket)
Set root password? [Y/n]: # y(root用户设置密码)
New password: # (新密码)
Re-enter new password: # (再次输入密码)
Remove anonymous users? [Y/n]: # y (移除匿名用户)
Disallow root login remotely? [Y/n]:# y (拒绝root远程登录,不管y/n,都会拒绝root远程登录)
Remove test database and access to it? [Y/n]: # y (删除test数据库)
Reload privilege tables now? [Y/n]: # y (重新加载权限表。或者重启服务也许)
登录MariaDB:
sudo mysql -u root -p
Enter password:
显示如下登录成功信息:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3601
Server version: 10.6.16-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)]>
设置MariaDB的字符集为UTF-8:
1)配置/etc/my.cnf文件:
sudo vim /etc/my.cnf
# 在 [mysqld] 标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
2) 配置/etc/my.cnf.d/client.cnf 文件:
sudo vim /etc/my.cnf.d/client.cnf
在 [client] 标签下添加
default-character-set=utf8
3) 配置/etc/my.cnf.d/mysql-clients.cnf 文件:
sudo vim /etc/my.cnf.d/mysql-clients.cnf
在 [mysql] 标签下添加
default-character-set=utf8
MariaDB安装完成,重启MariaDB服务:
sudo systemctl restart mariadb
安装PHP
首先删除已有老版本的php:
sudo yum remove -y php*
添加remi源(可能需要提前安装epel-release):
sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
单独启用php80的源:
sudo yum-config-manager --disable 'remi-php*'
sudo yum-config-manager --enable remi-php80
安装php及其拓展:
sudo yum install -y php php-bcmath php-cli php-common php-devel php-fpm php-gd php-intl php-ldap php-mbstring php-mysqlnd php-odbc php-pdo php-pear php-pecl-xmlrpc php-pecl-zip php-process php-snmp php-soap php-sodium php-xml
默认情况下,PHP-FPM将以apache用户身份在端口9000上运行。因为我们使用nginx,所有需要将用户更改为nginx,并从TCP socket切换为Unix socket。为此需要更改/etc/php-fpm.d/www.conf文件:
sudo vim /etc/php-fpm.d/www.conf
更改如下部分的内容:
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx
配置/var/lib/php目录的权限:
sudo chown -R root:nginx /var/lib/php
重新启动PHP FPM服务:
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
验证php版本:
php -v
看到如下的版本内容代表php安装已经成功了:
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies
with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies
安装PHPMyAdmin
安装phpmyadmin:
sudo yum install phpmyadmin
将/etc/phpMyAdmin目录的所有权更改为nginx(运行PHP FPM服务的用户):
sudo chgrp -R nginx /etc/phpMyAdmin
配置nginx和phpMyAdmin:
1)创建snippets/phpMyAdmin.conf文件:
sudo mkdir -p /etc/nginx/snippets
sudo nano /etc/nginx/snippets/phpMyAdmin.conf
2)在snaippets/phpMyAdmin.conf文件中加入如下内容:
location /phpMyAdmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpMyAdmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpMyAdmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpmyadmin {
rewrite ^/* /phpMyAdmin last;
}
把如下内容增加到WordPress域对应的nginx conf文件中:
include snippets/phpMyAdmin.conf;
示例如下:
server {
# . . .
include snippets/phpMyAdmin.conf;
# . . .
}
到这里phpMyAdmin安装工作完成,浏览器访问http(s)://your_server_address/phpmyadmin应该就可以进入到phpMyAdmin的登录界面了。
安装WordPress
安装完上述的依赖软件,接下来就可以正式安装WordPress了。安装过程也相当简单。
创建一个目录,用于存储WordPress的所有文件(按自己的域名更改example.com到其它自己想要的路径名):
sudo mkdir -p /var/www/html/example.com
下载最新版本的WordPress到本地:
cd /tmp
wget https://wordpress.org/latest.tar.gz
解压并移动WordPress所有文件到/var/www/html/example.com:
tar xf latest.tar.gz
sudo mv /tmp/wordpress/* /var/www/html/example.com/
设置正确的权限,以便网络服务器可以完全访问该网站的文件和目录:
sudo chown -R nginx: /var/www/html/example.com
配置nginx:
sudo vim /etc/nginx/conf.d/example.com.conf
将如下内容加入example.com.conf文件(按照自己的实际情况调整如域名,ssl_certificate路径):
# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.example.com example.com;
include snippets/letsencrypt.conf;
return 301 https://example.com$request_uri;
}
# Redirect WWW -> NON WWW
server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/html/example.com;
index index.php;
# SSL parameters
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
# log files
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
检查nginx语法有没有错误:
sudo nginx -t
如果没有错误,则会显示如下信息:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果有错误,请检查并修改。
然后重新启动nginx:
sudo systemctl restart nginx
到这里,整个安装WordPress的工作就已经完成,可以登录WordPress页面进行安装设置。
下一篇文章我们介绍如何把前面备份的WordPress文件和数据库迁移到这个新安装的WordPress。
相关软件和资源的链接:WordPress,CentOS,MariaDB,PHP,PHPMyAdmin。
文章评论
@WordPress个人blog搬家记 – 1. 备份WordPress – HY's Blog Good