在 Ubuntu 18.04 上使用 Nginx 安装 WordPress

前言

WordPress是目前流行的开源内容管理系统(CMS)之一,WordPress可以用来开发任何类型的网站,无论是博客、小型企业还是大型企业。通过它的免费和高级插件和主题,WordPress网站可以快速扩展以提供高级功能。

前提条件

  • 准备全新的 Ubuntu 18.04 系统
  • 做好系统的备份(Check Points,Snapshots……)以防安装出错可以回滚系统
  • 域名 DNS A-记录指向你的服务器 IP 地址

检查更新Ubuntu 18.04

sudo apt-get update
  
sudo apt-get upgrade -y

检查系统是否 Ubuntu 18.04

lsb_release -a 

----------OutPut---------- 
LSB Version:  
Distributor ID: Ubuntu 
Description:    Ubuntu 18.04.4 
LTS Release:    18.04 
Codename:       bionic

安装 Nginx

安装反向代理 Nginx。

sudo apt-get install nginx -y

安装 Nginx 后,一般情况下会自动运行。可以通过以下 systemctl 命令来验证。

sudo systemctl status nginx 

----------OutPut---------- 
● nginx.service - A high performance web server and a reverse proxy server    
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)    
   Active: active (running) since Sat 2020-06-27 15:53:14 CST; 6min ago      
     Docs: man:nginx(8) 
Main PID: 5154 (nginx)    
   Tasks: 2 (limit: 1129)   
  CGroup: /system.slice/nginx.service           
          ├─5154 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;       
          └─5156 nginx: worker process 
Jun 27 15:53:14 Ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server… 
Jun 27 15:53:14 Ubuntu systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument 
Jun 27 15:53:14 Ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.

可以通过以下命令启动 Nginx 和启用开机自动运行。

sudo systemctl start nginx
# 启动 Nginx

sudo systemctl enable nginx
# 启用开机自动运行

安装数据库 MariaDB

sudo apt-get install mariadb-server -y

sudo systemctl start mariadb

sudo systemctl enable mariadb

运行以下命令提高数据库安全性。

sudo mysql_secure_installation 

----------OutPut---------- 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! 
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. 
Enter current password for root (enter for none): 
OK, successfully used password, moving on… 
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. 
You already have a root password set, so you can safely answer 'n'. 
Change the root password? [Y/n] y 
New password: 
Re-enter new password: 
Password updated successfully! 
Reloading privilege tables.. 
… Success! 
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. 
Remove anonymous users? [Y/n] y 
… Success! 
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. 
Disallow root login remotely? [Y/n] y 
… Success! 
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. 
Remove test database and access to it? [Y/n] y 
Dropping test database… 
… Success! 
Removing privileges on test database… 
… Success! 
Reloading the privilege tables will ensure that all changes made so far will take effect immediately. 
Reload privilege tables now? [Y/n] y 
… Success! 
Cleaning up… 
All done! If you've completed all of the above steps, your MariaDB installation should now be secure. 
Thanks for using MariaDB!

安装 PHP 7.4

添加ppa:ondrej/php PPA仓库,其中有PHP的最新构建包。

sudo apt-get update 
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php 
sudo apt-get update

安装 PHP 7.4

sudo apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl -y

检查 PHP 安装版本

php -verson 

----------OutPut---------- 
PHP 7.4.7 (cli) (built: Jun 12 2020 07:44:05) ( NTS ) 
Copyright (c) The PHP Group 
Zend Engine v3.4.0, Copyright (c) Zend Technologies 
with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies

创建 WordPress 数据库

sudo mysql -u root -p

CREATE DATABASE wordpress_db;

GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Password' WITH GRANT OPTION;

FLUSH PRIVILEGES;

exit

*替换 GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Password' WITH GRANT OPTION; 中的 Password 为你自己的密码

编辑 Nginx 相关文件

创建 WordPress 根目录。

sudo mkdir /var/www/html/wordpress

为 WordPress 创建 Nginx 服务器文件。

sudo nano /etc/nginx/sites-available/wordpress.conf
server {             
            listen 80;             
            root /var/www/html/wordpress;             
            index index.php index.html;             
            server_name SUBDOMAIN.DOMAIN.TLD;             
           
            access_log /var/log/nginx/www.access.log;             
            error_log /var/log/nginx/www.error.log;              

            location / {                            
                 try_files $uri $uri/ /index.php?$args;
        }
         
            location ~ \.php$ {
            include snippets/fastcgi-php.conf;                            
                 fastcgi_pass unix:/run/php/php7.4-fpm.sock;             
            }              

            location ~ /\.ht {                            
                 deny all;             
            }             

            location = /favicon.ico {                            
                 log_not_found off;                            
                 access_log off;             
            }             

            location = /robots.txt {                            
                 allow all;                            
                 log_not_found off;                            
                 access_log off;             
            }             

            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {                            
                 expires max;                            
                 log_not_found off;             
            }  
}

*替换 SUBDOMAIN.DOMAIN.TLD 为你的域名。

检查上述配置文件的正确性

nginx -t 

----------OutPut---------- 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /etc/nginx/nginx.conf test is successful

要激活 Nginx 文件,在 /etc/nginx/sites-enabled 文件夹中创建上述配置文件的符号链接。

cd /etc/nginx/sites-enabled  
sudo ln -s ../sites-available/wordpress.conf .

重新加载 Nginx 以应用更改的设置

sudo systemctl reload nginx

下载和配置 WordPress

cd /var/www/html/wordpress  
sudo wget https://wordpress.org/latest.tar.gz  
sudo tar -zxvf latest.tar.gz  
sudo mv wordpress/* .  
sudo rm -rf wordpress latest.tar.gz

更改文件所有权并且应用权限给 WordPress 所有文件。

cd /var/www/html
sudo chown -R www:www * 
sudo chmod -R 755 *

配置 wp-config.php 文件。

cd /var/www/html/wordpress  
sudo mv wp-config-sample.php wp-config.php  
sudo nano wp-config.php
/** 在wp-config.php文件中找出并更改以下内容 */ 
...  
...  
define('DB_NAME', 'wordpress_db');  
define('DB_USER', 'wpuser');  
define('DB_PASSWORD', 'Passw0rd!');  
...  
... 

/** 在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' ); 
... 
...

为了保证 WordPress 网站的安全,在上面的 WordPress 配置文件中,在数据库配置选项之后,通过这个链接生成安全密钥,粘贴在删除内容的位置中。

安装 WordPress

在浏览器中访问你的域名,根据 WordPress 设置指引完成最后的配置。

WordPress 安装完成。


发表评论须遵守中华人民共和国相关法律法规。违规评论将会被删除。

Comments must subject to the relevant laws and regulations of the People’s Republic of China. Offending comments will be deleted.

发表评论

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

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据