VPS搭建WordPress完整教程:从购买到上线全流程详解

WordPress是全球最流行的内容管理系统,使用VPS搭建WordPress网站是许多站长的首选方案。本文详细讲解从VPS购买到WordPress上线的完整流程,帮助新手快速搭建自己的网站。

一、VPS选择建议

选择合适的VPS配置:

  • 配置选择:入门网站建议1核2GB内存起
  • 操作系统:推荐CentOS 7/8或Ubuntu 22.04
  • 机房位置:面向国内用户选择国内或香港节点
  • 带宽要求:至少100Mbps带宽

二、服务器环境配置

安装Web服务器、数据库和PHP环境:

# 更新系统
yum update -y

# 安装Apache/Nginx、MySQL/MariaDB、PHP
yum install nginx mariadb-server php php-fpm php-mysqlnd php-gd php-xml php-mbstring -y

# 启动服务
systemctl enable --now nginx
systemctl enable --now mariadb
systemctl enable --now php-fpm

# 配置MySQL
mysql_secure_installation

三、创建数据库

为WordPress创建数据库和用户:

# 登录MySQL
mysql -u root -p

# 创建数据库
CREATE DATABASE wordpress;

# 创建用户并授权
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

四、安装WordPress

下载并配置WordPress:

# 下载WordPress
cd /var/www/html
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
chown -R nginx:nginx wordpress

# 配置WordPress
cp wordpress/wp-config-sample.php wordpress/wp-config.php

# 修改wp-config.php配置数据库信息
sed -i "s/database_name_here/wordpress/" wordpress/wp-config.php
sed -i "s/username_here/wpuser/" wordpress/wp-config.php
sed -i "s/password_here/your_password/" wordpress/wp-config.php

五、配置Nginx

配置Nginx虚拟主机:

cat > /etc/nginx/conf.d/yourdomain.conf << EOF
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/html/wordpress;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
EOF

# 重启Nginx
systemctl restart nginx

六、域名解析与SSL配置

配置域名解析和SSL证书:

  1. 在域名注册商添加A记录指向VPS IP
  2. 使用Let's Encrypt申请免费SSL证书
  3. 配置Nginx支持HTTPS
# 安装Certbot
yum install certbot python3-certbot-nginx -y

# 获取SSL证书
certbot --nginx -d yourdomain.com -d www.yourdomain.com

# 自动续期配置
certbot renew --dry-run

七、WordPress优化建议

  • 安装缓存插件(如WP Rocket、WP Super Cache)
  • 配置CDN加速
  • 优化图片大小
  • 定期备份数据

如需进一步了解相关内容,可通过联系我们咨询。