香港服务器租用后如何安装 WordPress?

租用香港服务器后,安装 WordPress 是搭建网站的常见任务。


一、基本环境准备

在安装 WordPress 前,需要确保香港服务器具备以下基础环境支持:

  1. 操作系统:Linux 系统(如 CentOS、Ubuntu、Debian)
  2. Web 服务器:Nginx 或 Apache
  3. 数据库:MySQL 或 MariaDB
  4. PHP:支持 PHP 7.4 或更高版本
  5. 域名(可选):方便通过域名访问网站

二、安装 LAMP 或 LEMP 环境

1. 更新系统软件

确保服务器的软件包是最新的:

bash
# Ubuntu/Debian
apt update && apt upgrade -y

# CentOS
yum update -y

2. 安装 Web 服务器

(1)安装 Apache

如果使用 Apache 作为 Web 服务器:

bash
# Ubuntu/Debian
apt install apache2 -y

# CentOS
yum install httpd -y
systemctl start httpd
systemctl enable httpd

(2)安装 Nginx

如果使用 Nginx 作为 Web 服务器:

bash
# Ubuntu/Debian
apt install nginx -y

# CentOS
yum install nginx -y
systemctl start nginx
systemctl enable nginx

3. 安装数据库

(1)安装 MySQL 或 MariaDB

bash
# Ubuntu/Debian
apt install mysql-server -y

# CentOS
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb

(2)安全配置数据库

执行以下命令进行初始配置:

bash
mysql_secure_installation
  • 设置数据库 root 密码
  • 移除匿名用户
  • 禁止 root 远程登录
  • 删除测试数据库
  • 重新加载权限表

(3)创建 WordPress 数据库和用户

登录数据库并创建 WordPress 所需的数据库和用户:

bash
mysql -u root -p

# 进入 MySQL 控制台后执行以下命令
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. 安装 PHP

(1)安装 PHP 及所需扩展

bash
# Ubuntu/Debian
apt install php php-mysql php-fpm php-curl php-gd php-xml php-mbstring -y

# CentOS
yum install php php-mysqlnd php-fpm php-curl php-gd php-xml php-mbstring -y

(2)验证 PHP 版本

确保 PHP 版本为 7.4 或更高:

bash
php -v

三、安装 WordPress

1. 下载 WordPress

(1)进入网站目录

将 WordPress 安装到 Web 服务器的根目录(如 /var/www/html):

bash
cd /var/www/html

(2)下载 WordPress

使用 wget 下载最新版本的 WordPress:

bash
wget https://wordpress.org/latest.tar.gz

(3)解压 WordPress

解压 WordPress 文件到当前目录(或指定其他目录):

bash
tar -zxvf latest.tar.gz
mv wordpress/* .
rm -rf wordpress latest.tar.gz

2. 设置目录权限

确保 Web 服务器对 WordPress 文件拥有正确的权限:

bash
chown -R www-data:www-data /var/www/html   # Ubuntu/Debian
chown -R apache:apache /var/www/html       # CentOS
chmod -R 755 /var/www/html

3. 配置 WordPress

(1)创建配置文件

将默认的 WordPress 配置文件复制为正式配置文件:

bash
cp wp-config-sample.php wp-config.php

(2)编辑配置文件

使用文本编辑器修改 wp-config.php,填写数据库信息:

bash
nano wp-config.php

修改以下部分:

php
define('DB_NAME', 'wordpress'); // 数据库名称
define('DB_USER', 'wp_user');   // 数据库用户
define('DB_PASSWORD', 'strong_password'); // 数据库密码
define('DB_HOST', 'localhost'); // 数据库主机
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', '');

(3)设置安全密钥

访问 WordPress 密钥生成服务 获取一组随机密钥,并替换 wp-config.php 中的以下部分:

php
define('AUTH_KEY',         'your-random-key');
define('SECURE_AUTH_KEY',  'your-random-key');
define('LOGGED_IN_KEY',    'your-random-key');
define('NONCE_KEY',        'your-random-key');
define('AUTH_SALT',        'your-random-key');
define('SECURE_AUTH_SALT', 'your-random-key');
define('LOGGED_IN_SALT',   'your-random-key');
define('NONCE_SALT',       'your-random-key');

4. 配置 Web 服务器

(1)配置 Apache

如果使用 Apache,创建或修改虚拟主机配置文件:

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

添加以下内容:

apache
<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

启用配置并重启 Apache:

bash
a2ensite wordpress
a2enmod rewrite
systemctl restart apache2

(2)配置 Nginx

如果使用 Nginx,创建或修改 Nginx 配置文件:

bash
nano /etc/nginx/conf.d/wordpress.conf

添加以下内容:

nginx
server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    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;
    }
}

测试配置并重启 Nginx:

bash
nginx -t
systemctl restart nginx

5. 访问 WordPress 安装页面

在浏览器中访问你的服务器 IP 或域名,例如:

 
http://yourdomain.com

按照页面提示完成 WordPress 的安装:

  1. 选择语言
  2. 填写站点名称、管理员用户名、密码和邮箱
  3. 提交后登录 WordPress 控制台

四、可选优化

1. 配置 HTTPS(SSL 加密)

使用 Let’s Encrypt 为 WordPress 配置免费 SSL:

bash
apt install certbot python3-certbot-nginx  # Ubuntu/Debian
yum install certbot python3-certbot-nginx  # CentOS

certbot --nginx -d yourdomain.com -d www.yourdomain.com

设置自动续期:

bash
crontab -e
# 添加以下行
0 0 * * * certbot renew --quiet

2. 启用缓存

为 WordPress 启用缓存插件(如 W3 Total CacheWP Super Cache)以提高性能。

3. 安装防火墙

设置基本防火墙规则,保护服务器免受攻击:

bash
# Ubuntu/Debian
ufw allow 'Nginx Full'
ufw enable

# CentOS
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

五、总结

通过以上步骤,你可以在香港服务器租用上成功安装并运行 WordPress。以下是主要步骤的总结:

  1. 配置 LAMP 或 LEMP 环境
  2. 创建 WordPress 数据库和用户
  3. 下载并配置 WordPress
  4. 配置 Web 服务器(Apache 或 Nginx)
  5. 访问安装页面完成设置

 

此外,可以通过启用 HTTPS、缓存和防火墙等优化措施进一步提升网站的性能和安全性。

超过 50,000 人的信任 网硕互联期待你加入我们的会员。