boxmoe_header_banner_img

Hello! 欢迎来到盒子萌!

文章导读

Deploying WordPress on Ubuntu 22.04


avatar
admin 2026年8月3日 26

Install LAMP

# Ubuntu/Debian 系统
sudo apt update && sudo apt upgrade -y

# 安装 Apache
sudo apt install apache2 -y

# 启动 Apache 服务
sudo systemctl start apache2

# 设置开机自启动
sudo systemctl enable apache2

# 检查 Apache 状态,确保服务正常运行
sudo systemctl status apache2

# 安装 MariaDB 服务器和客户端
sudo apt install mariadb-server mariadb-client -y

# 启动 MariaDB 服务
sudo systemctl start mariadb

# 设置开机自启动
sudo systemctl enable mariadb

# 检查 MariaDB 状态
sudo systemctl status mariadb

# 加固数据库安全
sudo mysql_secure_installation

# 安装 PHP 及 WordPress 必需的扩展(如 mysql、curl、gd 等)
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-xml php-mbstring php-zip -y

# 检查 PHP 版本,确保 ≥ 7.4
php -v
展开

Install WordPress

# 进入 /tmp 目录(临时存放压缩包)
cd /tmp
 
# 下载 WordPress 最新版(官网链接)
wget https://wordpress.org/latest.tar.gz
 
# 解压压缩包(会生成 wordpress 文件夹)
tar -zxvf latest.tar.gz
 
# 将解压后的文件移动到 Apache 默认网站目录(/var/www/html/)
sudo mv wordpress /var/www/html/

# 设置目录所有者为 www-data(Apache 用户)
sudo chown -R www-data:www-data /var/www/html/wordpress/
 
# 设置目录权限(文件夹 755,文件 644,确保安全且可访问)
sudo chmod -R 755 /var/www/html/wordpress/

Create an WordPress Database

sudo mysql -u root -p

CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY '你的强密码';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;  # 使权限生效
EXIT;             # 退出数据库命令行

Configure Web Server

sudo nano /etc/apache2/sites-available/wordpress.conf

<VirtualHost *:80>
    # 网站域名或服务器 IP(如无域名,直接填写服务器公网 IP)
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com  # 可选,域名别名
 
    # WordPress 网站根目录(指向我们移动的 wordpress 文件夹)
    DocumentRoot /var/www/html/wordpress
 
    # 管理员邮箱,用于接收服务器错误通知
    ServerAdmin admin@yourdomain.com
 
    # 日志文件路径(方便调试)
    ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log
    CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined
 
    # 允许 .htaccess 文件覆盖 Apache 配置(用于 WordPress 固定链接)
    <Directory /var/www/html/wordpress>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

sudo a2ensite wordpress.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Enable SSL(https)

生成SSL

$ sudo certbot --apache -d meow.lostyu.qzz.io

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for meow.lostyu.qzz.io

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/meow.lostyu.qzz.io/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/meow.lostyu.qzz.io/privkey.pem
This certificate expires on 2026-11-02.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for meow.lostyu.qzz.io to /etc/apache2/sites-available/wordpress-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://meow.lostyu.qzz.io

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

修改数据库端口

mysql> use wordpress_db
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from wp_options where option_name in ('siteurl','home');
+-----------+-------------+------------------------------+----------+
| option_id | option_name | option_value                 | autoload |
+-----------+-------------+------------------------------+----------+
|         3 | home        | http://meow.lostyu.qzz.io:80 | on       |
|         2 | siteurl     | http://meow.lostyu.qzz.io:80 | on       |
+-----------+-------------+------------------------------+----------+
2 rows in set (0.00 sec)

mysql> update wp_options set option_value='https://meow.lostyu.qzz.io' where option_name in ('siteurl','home');
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye


评论(0)

查看评论列表

暂无评论


发表评论

表情 颜文字
插入代码

标签云

暂无标签!