标签: VPS

  • VPS手动搭建WordPress教程(LNMP环境)

    前期准备

    • VPS 服务(推荐Debian 13)
    • SSH连接工具(本文以Finalshell为例)

    步骤

    连接上VPS之后,输入下列命令将系统更新到最新。

    apt update -y && apt upgrade -y

    1、安装基础软件和必要软件

    • 安装PHP和相关组件
    apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache php-xml php-mysqli unzip -y
    • 安装NGINX
    apt install nginx -y
    • 安装MariaDB

    个人网站建议是用MariaDB,相比MySQL性能会更好。复制速度更快,安全措施更好,还有额外的存储引擎。

    apt install mariadb-server -y

    2、创建数据库和配置文件

    • 接下来为安装WordPress做准备,先新建一个数据库
    mysql -uroot -p
    CREATE DATABASE your_database_db;
    CREATE USER your_username_user@localhost IDENTIFIED BY 'your_database_password';
    GRANT ALL ON your_database_db.* TO your_username_user@localhost;
    FLUSH PRIVILEGES;
    • 配置NGINX
    mkdir -p /var/www/html/www.yourdomain.com/public_html
    • 创建虚拟主机配置文件和配置伪静态规则

    方法一:在Finalshell终端,用命令行编辑(输入下列命令,按i键进入Insert模式,把配置部分粘贴进去,然后按ESC键再输入:wq保存退出)

    vi /etc/nginx/sites-available/www.yourdomain.com.conf

    方法二:如果命令行不熟悉,可以在Finalshell的下部文件窗口,导航到/etc/nginx/sites-available/手动创建文件文件名为www.yourdomain.com.conf的配置文件,然后粘贴以下配置内容。

    server {
      listen 80;
      server_name yourdomain.com www.yourdomain.com;
      root /var/www/html/www.yourdomain.com/public_html;
      index index.html;
    
    location / {
        index index.php index.html index.htm;
    	try_files $uri $uri/ /index.php?$args;
    }
    
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
      location ~* \.php$ {
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        include snippets/fastcgi-php.conf;
      }
    }

    以上代码中php要改成你安装的版本号,可输入php -v查询,比如我装的是PHP 8.4.16,所以我放的就是 php8.4-fpm.sock

    • 创建软连接
    ln -s /etc/nginx/sites-available/www.yourdomain.com.conf /etc/nginx/sites-enabled/
    • 删除默认站点
    rm /etc/nginx/sites-enabled/default
    • 验证NGINX配置
    nginx -t
    • 重启相关服务
    systemctl restart nginx
    systemctl restart mariadb
    systemctl restart php8.4-fpm

    3、配置SSL安全证书

    接下来我们先安装好SSL安全证书。(如果你打不开网站,应该是你没有解析域名,请到你购买域名的网站去设置,如果使用的是Cloudflare就去它那先设置好域名解析)。

    • 首先安装snapd
    apt install snapd -y
    • 后面时间久了如果需要更新snapd,则使用下面的命令
    snap install core; snap refresh core
    • 安装 Certbot
    snap install --classic certbot
    • 运行certbot申请SSL安全证书,一般都会同时申请带www和不带www的网址的安全证书
    /snap/bin/certbot --nginx -d yourdomain.com -d www.yourdomain.com

    输入命令后,需要你输入一个邮箱地址,回车之后几个提示直接按Y回车。

    • 后面需要为SSL续签的时候,运行下面的命令
    certbot renew

    申请完毕后,打开网站就可以在地址栏看到一把小锁了。

    4、安装WordPress网站程序

    • 先切换到网站根目录
    cd /var/www/html/www.yourdomain.com/public_html/
    • wget下载wordpress安装包文件
    wget https://wordpress.org/latest.zip
    • 解压安装包
    unzip latest.zip
    • 进入解压出来的文件夹,移动所有文件到上级目录,切换到上级目录。
    cd wordpress
    mv * ..
    cd ..
    • 修改文件权限
    chmod -R 755 /var/www/html/www.yourdomain.com/public_html
    chown -R www-data:www-data /var/www/html/www.yourdomain.com/public_html

    访问你的网站就会进入WordPress安装界面。

    5、补充

    5.1 解决不带https访问404错误

    当默认设置的访问网址是https://yourdomain.com,如果直接浏览器打开www.yourdomain.com会提示404,这是因为申请ssl证书时自动加载的代码写了404,我们只需要下面这么操作。

    找到/etc/nginx/sites-available/www.yourdomain.com.conf这个配置文件,定位到下列内容。

    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 404; # managed by Certbot

    将最后404那一行内容修改为:

    return 301 https://yourdomain.com; # managed by Certbot

    然后重启nginx,这样访问不带https的网址,也会跳转到带有https的主域名。

    5.2 让WordPress网站支持Redis Object缓存

    安装 Redis 服务器和 PHP Redis 扩展

    # 安装Redis服务器
    apt install redis-server -y
    
    #安装PHP Redis扩展
    apt install php-redis -y
    
    #重启PHP-FPM使扩展生效
    systemctl restart php8.4-fpm

    验证安装情况

    # 检查Redis服务状态
    systemctl status redis-server
    
    # 验证PHP Redis扩展是否加载
    php -m | grep redis

    安装 WordPress Redis 缓存插件

    登录 WordPress 后台,进入 “插件” → “安装插件”,搜索 “Redis Object Cache”,安装并激活。

    进入插件设置页面启用缓存。

    5.3 报错:413 Request Entity Too Large

    打开 Nginx 的配置文件,路径在 /etc/nginx/nginx.conf

    在http块儿里面添加上client_max_body_size 20M;

    然后重启nginx(systemctl restart nginx)