[Linux] 在 Ubuntu 20.04 中部署Nginx + WordPress

0. 目的

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

1. 更新软件包:

sudo apt update
sudo apt upgrade -y

2. 下载 WordPress 并 解压:

cd /opt/
curl -L -O "https://wordpress.org/latest.zip"
unzip latest.zip

3. 安装mysql:

sudo apt install mysql-server

4. 进行初始化设置:

sudo mysql

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

sudo mysql_secure_installation

5. 按照提示输入你的选择:

Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

6. 通过root用户登录 mysql:

sudo mysql -u root -p

7. 输入密码后,创建用户,数据库,授权,刷新权限并退出:

mysql> create user 'wordpress'@'localhost' identified by 'password';
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to wordpress@localhost;
mysql> flush privileges;

8. 复制wp-config-sample.php 到 wp-config.php,并修改里面的用户名,密码,安全认证等信息:

    /** 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(A|3p#K');
    define('SECURE_AUTH_KEY',  'w`2nB>Lkh_,,%5CqA_->/:EFA%j(@/)T@N7%WAIE~^EX-v@+m|kLWogUY~B3R=`Q');
    define('LOGGED_IN_KEY',    'KKUK?ifTs2Ktx5K7^G=,.6/Wb*2f/1=K`R|K 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;9H|tD&7UFWBzsKC~sc~{DwLpqSe58N');

    /** 必要时,考虑为 wordpress 配置代理*/
    define('WP_PROXY_HOST', '127.0.0.1');
    define('WP_PROXY_PORT', '10809');

9. 安装 php(nginx):

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

10. 修改nginx配置文件如下:

    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 ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
            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;
        }
    }

11. 重启nginx, 然后开始进行站点配置;

12. 后期修改 mysql 密码:

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

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注