Debian 10 全程手动部署WordPress个人博客,手动部署LNMP环境,新手小白也可以拥有自己的个人博客站。

Debian 10 全程手动部署WordPress个人博客,手动部署LNMP环境,新手小白也可以拥有自己的个人博客站。

admin
2023-10-22 / 0 评论 / 44 阅读 / 正在检测是否收录...

1. 准备工作

  • VPS RAM≥1024.00 MB
  • 域名一个

2. 部署LNMP环境

  • LNMP代表Linux、Nginx、MySQL和PHP,是一种用于构建和部署Web应用程序的技术堆栈,用于运行PHP和MySQL。

2.1 安装nginx

  • 更新软件源列表

    sudo apt update
  • 安装Nginx

    sudo apt install -y nginx
  • 防火墙放行Nginx需要用到的端口

    sudo ufw allow 80,443/tcp

2.2 安装 MariaDB

  • 添加MariaDB软件源

    sudo DEBIAN_FRONTEND=noninteractive apt install -y software-properties-common
    sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
    sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://nyc2.mirrors.digitalocean.com/mariadb/repo/10.6/debian buster main'
  • 更新软件源列表

    sudo apt update
  • 安装MariaDB

    sudo apt install -y mariadb-server
  • 配置MariaDB

    sudo mysql_secure_installation

    在运行过程中,你将需要执行以下操作:

    • 输入当前的root密码:因为我们第一次安装 MariaDB,并且尚未设置 root 密码,所以此处按 Enter 键即可。
    • 切换到unix_socket身份验证:使用基于Unix套接字的身份验证方法,如果你选择切换到unix_socket 身份验证,你不再需要输入密码来登录 MySQL,而是允许root用户使用操作系统的身份验证,unix_socket 身份验证方式可以提高安全性、简化用户管理,并避免明文密码的存储和传输问题,这里选择 "Y" 即可。
    • 更改root用户密码:由于选择了使用unix_socket身份验证,所以不需要设置密码,这里选择 "N" 即可。
    • 删除匿名用户:选择 "Y" 来删除匿名用户,这将禁止没有密码的用户访问数据库。
    • 禁止root远程登录:选择 "Y" 来禁用root用户的远程访问。
    • 删除测试数据库:选择 "Y" 来删除测试数据库,这是一个潜在的安全风险。
    • 刷新权限表:选择 "Y" 来刷新权限表,以使更改生效。
  • 创建新数据库和用户

    1. 登录数据库
sudo mariadb
  • 2.创建一个新的数据库,*your_database_name* 替换为你创建的数据库的名称

    CREATE DATABASE your_database_name;

    3. 创建一个新的数据库用户,*your_username* 替换为你创建的用户的名称,*your_password* 替换为你为该用户设置的密码

    CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';

    4. 授予用户对指定数据库的全部权限,*your_database_name* 替换为你创建的数据库的名称,*your_username* 替换为你创建的用户的名称

    GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';

    5. 刷新MariaDB的权限缓存,以使新的权限更改立即生效

    FLUSH PRIVILEGES;

    6. 退出MariaDB

    exit

2.3 安装 PHP

  • 添加 Surry PHP 存储库

    sudo apt update
    sudo apt install -y apt-transport-https lsb-release ca-certificates
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
  • 更新软件源列表

    sudo apt update
  • 安装 PHP

    sudo apt install -y php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip php-imagick

3. 配置 Nginx 来运行 PHP

  • 在 /var/www/ 目录下创建一个文件夹用于存放你的网页

    sudo mkdir /var/www/wordpress
  • 将 /var/www/wordpress 目录及其所有内容的所有者更改为当前登录用户和当前登录用户所属的用户组

    sudo chown -R $USER:$USER /var/www/wordpress
  • 新建一个 Nginx 站点配置, example.com 替换为你的域名

    sudo vim /etc/nginx/sites-available/example.com
    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/wordpress;
        index index.php index.html index.htm;
        server_name example.com www.example.com;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
            add_header X-Content-Type-Options "nosniff";
            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-XSS-Protection "1; mode=block";
        }
    
        location ~* \.(css|js|jpg|jpeg|png|gif|ico)$ {
            expires 7d;
            add_header Cache-Control "public, max-age=604800";
        }
    
        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }
    
        location = /robots.txt {
            log_not_found off;
            access_log off;
            allow all;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        }
    }
  • 启用 Nginx 站点配置,example.com 替换为你的域名

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  • 查看 Nginx 配置是否正确

    sudo nginx -t
  • 重新加载 Nginx 以使新配置生效

    sudo systemctl reload nginx

4. 为 WordPress 配置证书

  • 安装 Certbot

    sudo apt install -y python3-acme python3-certbot python3-mock python3-openssl python3-pkg-resources python3-pyparsing python3-zope.interface
    sudo apt install python3-certbot-nginx
  • 申请 SSL 证书,example.com 替换为你的域名

    sudo certbot -d example.com -d www.example.com --nginx
    • 输入邮箱

      Enter email address (used for urgent renewal and security notices) (Enter 'c' to
      cancel): email@gmail.com
    • 同意服务条款

      Please read the Terms of Service at
      https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
      agree in order to register with the ACME server at
      https://acme-v02.api.letsencrypt.org/directory
      (A)gree/(C)ancel: A
    • 是否分享你的邮箱地址

      Would you be willing to share your email address with the Electronic Frontier
      Foundation, a founding partner of the Let's Encrypt project and the non-profit
      organization that develops Certbot? We'd like to send you email about our work
      encrypting the web, EFF news, campaigns, and ways to support digital freedom.
      (Y)es/(N)o:N
    • 将所有请求重定向至HTTPS

      1: No redirect - Make no further changes to the webserver configuration.
      2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
      new sites, or if you're confident your site works on HTTPS. You can undo this
      change by editing your web server's configuration.
      Select the appropriate number [1-2] then [enter] (press 'c' to cancel):2

5. 安装 WordPress

  • 下载 WordPress 并将其解压至 /var/www/wordpress/ 目录下

    curl -o wordpress.tar.gz https://cn.wordpress.org/latest-zh_CN.tar.gz && sudo tar -xzvf wordpress.tar.gz -C /var/www/wordpress/ --strip-components=1 && rm wordpress.tar.gz
  • 在/var/www/wordpress/目录下找到 wp-config-sample.php 文件并将其重命名为 wp-config.php,并修改配置

    // ** Database settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define( 'DB_NAME', 'database_name_here' ); #database_name_here修改为你创建的数据库名
    /** Database username */
    define( 'DB_USER', 'username_here' );  #username_here修改为你创建的数据库用户名
    /** Database password */
    define( 'DB_PASSWORD', 'password_here' );  #password_here修改为你创建的数据库用户名对应的密码
    /** Database hostname */
    define( 'DB_HOST', 'localhost' );
    /** Database charset to use in creating database tables. */
    define( 'DB_CHARSET', 'utf8' );
    /** The database collate type. Don't change this if in doubt. */
    define( 'DB_COLLATE', '' );
  • 使用以下命令获取安装密钥

    https://api.wordpress.org/secret-key/1.1/salt/
  • 修改/var/www/wordpress/wp-config.php文件的安装密钥,用你获取到的密钥替换以下部分

    define( 'AUTH_KEY',         'put your unique phrase here' );
    define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
    define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
    define( 'NONCE_KEY',        'put your unique phrase here' );
    define( 'AUTH_SALT',        'put your unique phrase here' );
    define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
    define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
    define( 'NONCE_SALT',       'put your unique phrase here' );
  • 将/var/www/wordpress目录的所有者和所属组设置为"www-data"

    sudo chown -R www-data:www-data /var/www/wordpress

6. 优化 WordPress

  • 设置 WordPress 上传文件限制

    • 修改/etc/php/8.2/fpm/php.ini文件

      post_max_size = 8M   #大约在700行,8M修改为你希望的限制
      upload_max_filesize = 2M     #大约在850行,2M修改为你希望的限制
    • 修改 Nginx 配置文件(/etc/nginx/nginx.conf)

      http {
      ......
      client_max_body_size 8M;    #这里的8M和php.ini文件里设置的上传限制要对应
      ......
      }
    • 重新加载 Nginx

      sudo systemctl reload nginx
    • 重启 PHP

      sudo systemctl restart php8.2-fpm.service
  • 安装 WPS Hide Login 插件来更改登录 URL,防止陌生用户访问 wp-login.php 页面和 wp-admin 目录以保护您的网站。
  • 安装WP Githuber MD插件,该插件提供了Markdown编辑器,可以自动生成文章目录。
  • 其它的博客美化与一些常用的插件大家自行添加吧。

YouTube视频教程地址:https://youtu.be/6ATWa1k3lGc

0

评论 (0)

取消