侧边栏壁纸
博主头像
STONE

行动起来,活在当下

  • 累计撰写 70 篇文章
  • 累计创建 67 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

ubuntu宿主机直接安装wordpress并配置nginx开启https访问

STONE
2025-02-10 / 0 评论 / 1 点赞 / 36 阅读 / 0 字
温馨提示:
本文最后更新于2025-02-10,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

一、环境准备

  1. 系统更新

    sudo apt update && sudo apt upgrade -y
  2. 安装必要工具

    sudo apt install -y curl wget unzip

二、安装Nginx、MySQL、PHP

  1. 安装Nginx

    sudo apt install -y nginx
    sudo systemctl start nginx
    sudo systemctl enable nginx
  2. 安装MySQL

    sudo apt install -y mysql-server
    sudo mysql_secure_installation  # 按提示设置root密码和安全选项
  3. 安装PHP及扩展

    sudo apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip
    sudo systemctl restart php8.1-fpm  # 根据实际PHP版本调整

三、配置MySQL数据库

  1. 登录MySQL

    sudo mysql -u root -p
  2. 创建数据库和用户

    CREATE DATABASE wordpress;
    CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
    GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;

四、安装并配置WordPress

  1. 下载WordPress

    cd /tmp && wget https://wordpress.org/latest.tar.gz
    tar -xzvf latest.tar.gz
    sudo mv wordpress /var/www/html/
  2. 设置权限

    sudo chown -R www-data:www-data /var/www/html/wordpress
    sudo chmod -R 755 /var/www/html/wordpress
  3. 复制配置文件

    cd /var/www/html/wordpress
    cp wp-config-sample.php wp-config.php
  4. 编辑配置文件

    sudo nano wp-config.php

    修改以下参数:

    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', 'YourStrongPassword123!');

五、配置Nginx虚拟主机

  1. 创建配置文件

    sudo nano /etc/nginx/sites-available/wordpress.conf
  2. 粘贴以下内容

    server {
        listen 80;
        server_name your-domain.com www.your-domain.com;
        root /var/www/html/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:/run/php/php8.1-fpm.sock;  # 确认PHP版本路径
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
  3. 启用配置并测试

    sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
    sudo nginx -t  # 检查配置语法
    sudo systemctl reload nginx

六、配置HTTPS(Let's Encrypt)

  1. 安装Certbot

    sudo apt install -y certbot python3-certbot-nginx
  2. 获取SSL证书

    sudo certbot --nginx -d your-domain.com -d www.your-domain.com
    • 按提示输入邮箱并同意条款

    • 选择是否重定向HTTP到HTTPS(推荐选2)

  3. 自动续期测试

    sudo certbot renew --dry-run

七、完成安装

  1. 访问网站 打开浏览器访问 https://your-domain.com,按WordPress向导完成安装。


常见问题处理

  • 权限问题:确保/var/www/html/wordpress目录属主为www-data

  • 502 Bad Gateway:检查PHP-FPM是否运行,且Nginx配置中PHP socket路径正确。

  • SSL证书错误:确认域名解析已生效,防火墙开放80/443端口。


完成以上步骤后,您的WordPress站点将通过Nginx运行,并启用HTTPS加密访问。

1

评论区