[Ubuntu] 20.04 中部署Nginx + Wordpress

  1. 目的

    在 Ubuntu 20.04 上安装部署 Wordpress 并用 Nginx 进行反代

  2. 更新软件包

    1
    2
    sudo apt update
    sudo apt upgrade -y
  3. 下载 WordPress 并 解压

    1
    2
    3
    cd /opt/
    curl -L -O "https://wordpress.org/latest.zip"
    unzip latest.zip
  4. 安装mysql

    1
    sudo apt install mysql-server
  5. 进行初始化设置

    1
    2
    3
    4
    5
    sudo mysql

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

    sudo mysql_secure_installation
  6. 按照提示输入你的选择

    1
    2
    3
    4
    5
    Change the password for root ? ((Press yY for Yes, any other key for No) : n
    Remove anonymous users? (Press yY for Yes, any other key for No) : y
    Disallow root login remotely? (Press yY for Yes, any other key for No) : y
    Remove test database and access to it? (Press yY for Yes, any other key for No) : y
    Reload privilege tables now? (Press yY for Yes, any other key for No) : y
  7. 通过root用户登录 mysql

    1
    sudo mysql -u root -p
  8. 输入密码后,创建用户,数据库,授权,刷新权限并退出

    1
    2
    3
    4
    mysql> create user 'wordpress'@'localhost' identified by 'password';
    mysql> create database wordpress;
    mysql> grant all privileges on wordpress.* to wordpress@localhost;
    mysql> flush privileges;
  9. 复制wp-config-sample.php 到 wp-config.php,并修改里面的用户名,密码,安全认证等信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    /** WordPress 数据库名 */
    define('DB_NAME', 'database_name_here');
    /** MySQL 数据库用户名 */
    define('DB_USER', 'username_here');
    /** MySQL 数据库密码 */
    define('DB_PASSWORD', 'password_here');

    /** 通过URL <https://api.wordpress.org/secret-key/1.1/salt/> 来生成安全认证信息, 每次打开url生成的信息都是不同的,不用担心信息泄露*/
    define('AUTH_KEY', ';(jrt>#*fr(}h-yb4NoCUUp$TTS@Sg/{&#91;2!`3.aG/4)sdW)TlSnws7#-i(A3p#K');
    define('SECURE_AUTH_KEY', 'w`2nB>Lkh_,,%5CqA_->/:EFA%j(@/)T@N7%WAIE~^EX-v@+mkLWogUY~B3R=`Q');
    define('LOGGED_IN_KEY', 'KKUK?ifTs2Ktx5K7^G=,.6/Wb*2f/1=K`RK DNj+=id3j;HfY?Daa/]6l4;,-~7');
    define('NONCE_KEY', 'qD}bA#C06$3C0OaoG0&&YKBc]n<D##56j6XU CSUEQXfDZo%aE$_OTL+1r*9m=98');
    define('AUTH_SALT', 'ZEXX=tuIn0A&}cI#mfu79+&#91;P!!&9xzWI$0LRO/_:1tw&#91;.u5f=^+YT/-W4LsWiSv');
    define('SECURE_AUTH_SALT', 'u5#@;:h%gITox4d3Kbi!=AGA_%A-N-+]wW]j=BBndj7XRW.Joc{,#BT-E41}]w(E');
    define('LOGGED_IN_SALT', '7~NAs!:vB2&#91;lSu5FK7SkB0SNeqgm]+5n^@w})z&#91;%UOupLb:hS`.y>sT)Yteml^Y*');
    define('NONCE_SALT', '/33?i8m.j*p]mHc-fU7B1Ne(wYpM(tiR}&#91;9HtD&7UFWBzsKC~sc~{DwLpqSe58N');

    /** 必要时,考虑为 wordpress 配置代理*/
    define('WP_PROXY_HOST', '127.0.0.1');
    define('WP_PROXY_PORT', '10809');
  10. 安装 php(nginx):

    1
    sudo apt install php8.1-imagick php8.1-fpm php8.1-mbstring php8.1-bcmath php8.1-xml php8.1-mysql php8.1-common php8.1-gd php8.1-cli php8.1-curl php8.1-zip
  11. 修改nginx配置文件如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    server {
    listen 443;
    listen [::]:443;
    server_name www.example.com;
    root /opt/wordpress/;
    index index.php index.html index.htm index.nginx-debian.html;
    access_log /var/log/nginx/wordpress_access.log standard;
    error_log /var/log/nginx/wordpress_error.log;

    location / {
    try_files $uri $uri/ @ee;
    }

    location @ee {
    rewrite ^(.*) /index.php?$1 last;
    }

    location ~* /wp-sitemap.*\.xml {
    try_files $uri $uri/ /index.php$is_args$args;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    client_max_body_size 0;

    # location = /50x.html {
    # root /usr/share/nginx/html;
    # }

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    }

    #enable gzip compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_comp_level 5;
    gzip_types application/json
    text/css
    application/x-javascript
    application/javascript
    image/svg+xml;
    gzip_proxied any;

    # A long browser cache lifetime can speed up repeat visits to your page
    location ~* \.(jpgjpeggifpngwebpsvgwoffwoff2ttfcssjsicoxml)$ {
    access_log off;
    log_not_found off;
    expires 360d;
    }

    # disable access to hidden files
    location ~ /\.ht {
    access_log off;
    log_not_found off;
    deny all;
    }
    }
  12. 重启nginx, 然后开始进行站点配置;

  13. 后期修改 mysql 密码:

    1
    ALTER USER wordpress@localhost IDENTIFIED BY '明文密码'

[Ubuntu] 20.04 中部署Nginx + Wordpress
https://www.aojie654.com/20210828_linux_ubuntu_2004_nginx-wordpress/
Author
aojie654
Posted on
August 28, 2021
Licensed under