在服务器部署Wordpress
用Docker部署Pantheon
Wordpress数据库
PHP/HTML/JS联动
如果不用docker 直接在服务器部署Wordpress,可以参考以下步骤。
登录到root账户:
使用su
命令切换到root账户,或者直接以root身份登录。
创建新用户:
使用useradd
命令来创建新用户,假设你想创建的用户是newuser
,可以运行以下命令:
1
useradd newuser
设置密码: 为新用户设置一个密码:
1
passwd newuser
将用户添加到管理员组: 这样新用户就可以拥有管理权限。使用以下命令将用户添加到管理员组:
1
2
usermod -aG sudo newuser # Ubuntu
usermod -aG wheel newuser # CentOS
验证: 你可以切换到新用户并验证权限:
1
2
su - newuser
sudo whoami
登录服务器
使用 SSH 登录你的服务器。
1
ssh newuser@server_ip
更新系统软件包
1
2
sudo apt update && sudo apt upgrade -y # 针对基于Debian/Ubuntu的系统
sudo yum update -y # 针对基于CentOS/RHEL的系统
安装 Apache (如果使用nginx+php则不需要)
1
2
sudo apt install apache2 # 针对Ubuntu/Debian
sudo yum install httpd # 针对CentOS/RHEL
安装 MySQL/MariaDB
1
2
sudo apt install mysql-server # Ubuntu/Debian
sudo yum install mariadb-server # CentOS/RHEL`
有必要的话需要启动MariaDB, 设置权限
1
2
3
4
5
sudo systemctl start mariadb # CentOS
sudo systemctl status mariadb
sudo chown mysql:mysql /var/lib/mysql/mysql.sock
sudo chmod 755 /var/lib/mysql
然后,启动 MySQL 并设置 root 密码
在第1处输入密码时,由于是新安装,请直接按 enter
:
1
sudo mysql_secure_installation
安装 PHP
1
2
sudo apt install php libapache2-mod-php php-mysql # Ubuntu/Debian
sudo yum install php php-mysqlnd # CentOS/RHEL
检查 php 版本
1
php -v
安装 php-fpm
1
2
3
4
5
6
7
# Ubuntu/Debian
sudo apt update
sudo apt install nginx php-fpm php-mysql
# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx php-fpm php-mysql
重启 Apache
如有需要的话 可以更换端口:
更改1 Apache监听端口:
1
2
sudo nano /etc/apache2/ports.conf # Ubuntu/Debian
sudo nano /etc/httpd/conf/httpd.conf # CentOS/RHEL
Listen 15678
更改2 Apache虚拟主机配置:
1
2
sudo nano /etc/apache2/sites-available/000-default.conf #Ubuntu
sudo nano /etc/httpd/conf.d/wordpress.conf # CentOS/RHEL 新建
<VirtualHost *:15678>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/wordpress
<Directory /var/www/html/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Ubuntu
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# CentOS
# ErrorLog /var/log/httpd/error.log
# CustomLog /var/log/httpd/access.log combined
</VirtualHost>
更改3 Nginx配置:
记得先备份
1
sudo cp /etc/nginx/nginx.conf ./nginx.backup.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name domain.com;
location / {
gzip on;
gzip_proxied any;
proxy_pass http://127.0.0.1:15678/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
重启 Apache:
1
2
sudo systemctl restart apache2 # Ubuntu/Debian
sudo systemctl restart httpd # CentOS/RHEL
重启 Nginx:
1
sudo systemctl restart nginx
###
登录 MySQL
1
sudo mysql -u root -p
创建 WordPress 数据库和用户
1
2
3
4
5
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
下载 WordPress
1
2
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
解压 WordPress
1
tar xzvf latest.tar.gz
将 WordPress 文件移动到服务器目录
1
sudo mv /tmp/wordpress /var/www/html/
设置正确的权限
1
2
3
4
5
6
# Ubuntu
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
# CentOS
sudo chown -R apache:apache /var/www/html/wordpress
创建 WordPress 配置文件
1
2
cd /var/www/html/wordpress
cp wp-config-sample.php wp-config.php
编辑配置文件,连接到数据库
1
sudo nano wp-config.php
在文件中找到以下行,并根据你的数据库信息修改:
1
2
3
4
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
保存 Ctrl+O
退出 Ctrl+X
在浏览器中访问你的服务器IP地址或域名或者绑定端口以后从localhost
访问:
按照屏幕上的提示完成安装,如设置站点名称、创建管理员账号等。
安装完成后,你可以通过 /wp-admin
进入 WordPress 后台。
按照以上的步骤,可以在localhost:15678访问wordpress, 也可以通过nginx转发到80端口,这样就可以通过域名访问了。
如果不能使用apache,或域名端口有问题,可以使用nginx+php-fpm的方式,这样就不需要apache了。
配置Nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
user www-data; ## CentOS 为 nginx
http
server {
server_name domain.com;
root /var/www/html/wordpress; # 确保这个路径正确指向你的 WordPress 文件夹
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf; # 如果没有配置文件可删除
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # 确保这个路径是 PHP 8.1 FPM 的套接字路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 以上部分 也可以使用 tcp 方式
# location ~ \.php$ {
# include fastcgi_params;
# fastcgi_pass 127.0.0.1:9000; # PHP-FPM 监听的地址
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#}
location ~ /\.ht {
deny all;
}
include mime.types;
default_type application/octet-stream;
types {
application/javascript js;
}
# SSL 配置 (如果需要) Certbot自动配置
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/xxxx/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxxx/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# HTTP 转 HTTPS, 如果没有SSL 可以删除
# 并把 ‘linsten 80;’ 移到上面
server {
if ($host = xxxx) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name xxxx;
return 404; # managed by Certbot
}
修改 php (Ubuntu, 如果需要)
1
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
1
ls -l /var/run/php/ # 查看套接字路径
1
2
3
4
listen = /var/run/php/php8.1-fpm.sock # 套接字路径
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
修改权限(CentOS)
权限从apache 改为 nginx
1
2
3
4
5
6
7
sudo chown -R nginx:nginx /var/www/html/wordpress
sudo find /var/www/html/wordpress -type d -exec chmod 755 {} \; # 设置目录权限
sudo find /var/www/html/wordpress -type f -exec chmod 644 {} \; # 设置文件权限
##改回来
sudo chown -R apache:apache /var/www/html/wordpress
sudo systemctl restart httpd
在Ubuntu中,都是www-data,所以不需要修改
打开 php-fpm
1
2
3
4
5
6
7
8
9
10
sudo systemctl stop apache2
sudo systemctl disable apache2
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
sudo systemctl reload nginx
# 改回来的话 把启动和停止的命令倒一下
Wordpress 配置
把Wordpress中的URL改为域名, 也可以在后台设置
1
nano /var/www/html/your_wordpress_directory/wp-config.php
1
2
define('WP_HOME', 'https://your_domain.com');
define('WP_SITEURL', 'https://your_domain.com');