如何在Ubuntu 20.04上使用Nginx安装WordPress
WordPress是使用最广泛的开源内容管理软件之一。 它为大约6000万个网站提供支持,其中包括前1000万个网站中的33%。 它是用PHP编写的,并使用MariaDB / MySQL作为数据库来存储信息。
在这里,我们将看到如何在Ubuntu 20.04上使用Nginx安装WordPress。 我们还将部署用于WordPress的“让我们加密SSL”,以确保网站的安全交付。
在Ubuntu 20.04上使用Nginx安装WordPress
安装LEMP堆栈
在继续之前,请在Ubuntu 20.04上设置LEMP堆栈以进行WordPress安装。
在Ubuntu 20.04上安装LEMP(Nginx,MariaDB和PHP)堆栈
安装PHP扩展
WordPress要在Ubuntu 20.04上运行,需要以下扩展。 因此,使用apt命令安装它们。
sudo apt update
sudo apt install -y php-dom php-simplexml php-ssh2 php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv php-imagick php-json php-mbstring php-posix php-sockets php-tokenizer
为WordPress创建Nginx服务器块
让我们为WordPress安装创建一个Nginx的服务器块。 该服务器块需要域名,端口号,文档根目录,日志位置,快速CGI等。
假设以下内容,
域名: www.itzgeek.net
文件根: /sites/www.itzgeek.net/public_html/
日志: /sites/www.itzgeek.net/logs/
在/etc/nginx/conf.d目录下创建服务器块配置文件。
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf
放置以下内容。
server { server_name www.itzgeek.net; root /sites/www.itzgeek.net/public_html/; index index.html index.php; access_log /sites/www.itzgeek.net/logs/access.log; error_log /sites/www.itzgeek.net/logs/error.log; # Don't allow pages to be rendered in an iframe on external domains. add_header X-Frame-Options "SAMEORIGIN"; # MIME sniffing prevention add_header X-Content-Type-Options "nosniff"; # Enable cross-site scripting filter in supported browsers. add_header X-Xss-Protection "1; mode=block"; # Prevent access to hidden files location ~* /.(?!well-known/) { deny all; } # Prevent access to certain file extensions location ~.(ini|log|conf)$ { deny all; } # Enable WordPress Permananent Links location / { try_files $uri $uri/ /index.php?$args; } location ~ .php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
创建文档根目录和日志目录。
sudo mkdir -p /sites/www.itzgeek.net/public_html/
sudo mkdir -p /sites/www.itzgeek.net/logs/
验证配置文件。
sudo nginx -t
以下消息确认Nginx的服务器块配置正确。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
重新启动服务。
sudo systemctl restart nginx
安装让我们为WordPress加密SSL(可选)
在当前情况下,几乎所有网站/博客都使用HTTPS(SSL证书)来确保真实性。 Google要求所有者切换到HTTPS,以提高安全性并改善Google页面排名。
安装Certbot
要生成SSL证书,请在系统上安装Certbot ACME客户端。 它可以处理证书的颁发和证书的安装,而无需停机。
在撰写本文时,Certbot客户端不会自动将Nginx配置为使用SSL证书。 我们需要手动安装SSL证书。
Certbot客户端现在在Ubuntu存储库中可用。 因此,使用apt命令安装它。
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository universe
sudo apt update
现在,安装certbot客户端。
sudo apt install -y certbot
更新/更改DNS记录
转到您的域名注册商,然后为您的域创建一个A / CNAME记录。
更新DNS记录
等待一段时间,让记录传播。
生成让我们加密SSL证书
使用certbot命令生成并安装“让我们加密”证书。
sudo certbot certonly --webroot
输出:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.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: Y www.itzgeek.net Obtaining a new certificate Performing the following challenges: http-01 challenge for www.itzgeek.net Input the webroot for www.itzgeek.net: (Enter 'c' to cancel): /sites/www.itzgeek.net/public_html/ Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/www.itzgeek.net/privkey.pem Your cert will expire on 2020-08-07. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
部署让我们加密SSL证书
编辑我们为WordPress安装创建的Nginx的服务器阻止文件。
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf
将SSL证书添加到服务器块中。
server { . . . # Let's Encrypt SSL certificate listen 443 ssl; ssl_certificate /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.itzgeek.net/privkey.pem; . . . }
使用Nginx将HTTP请求重定向到HTTPS
我们还将创建另外两个服务器块,以将来自HTTP站点的流量重定向到HTTPS站点。
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf
在文件末尾添加以下块。
# Redirect WWW HTTP to WWW HTTPS # http://www.itzgeek.net >> https://www.itzgeek.net server { if ($host = www.itzgeek.net) { return 301 https://$host$request_uri; } server_name www.itzgeek.net; listen 80; return 404; } # Redirect NON-WWW HTTP to WWW HTTPS # http://itzgeek.net >> https://www.itzgeek.net server { if ($host = itzgeek.net) { return 301 https://www.itzgeek.net$request_uri; } server_name itzgeek.net; listen 80; return 404; }
重新启动Nginx服务。
sudo systemctl restart nginx
为WordPress创建数据库
登录到MariaDB / MySQL。
sudo mysql -u root -p
为WordPress创建所需的数据库。
CREATE DATABASE wordpress;
创建一个用户。
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wppassword';
向创建的用户授予访问数据库的权限。
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
从MariaDB / MySQL Shell退出。
quit
下载WordPress
从WordPress.org下载最新版本的WordPress。
wget http://wordpress.org/latest.tar.gz
使用tar命令提取WordPress软件包。
tar -zxvf latest.tar.gz
将WordPress文件移动到文档根目录。
sudo mv wordpress/* /sites/www.itzgeek.net/public_html/
更改所有权,以便Nginx Web服务器可以向其中写入文件。
sudo chown -R www-data:www-data /sites/www.itzgeek.net/public_html/
sudo chown -R www-data:www-data /sites/www.itzgeek.net/logs/
安装WordPress
打开您的网络浏览器并访问:
http:// your-web-site-url
您将获得WordPress安装向导。
单击“开始吧!”。

我们走吧
在此页面上,输入数据库详细信息以使WordPress与数据库连接。

输入WordPress数据库详细信息
如果与数据库的连接成功,则将显示以下页面。 单击“运行安装”。

运行WordPress安装
在此页面上,输入网站标题,WordPress管理员和密码(您选择),然后输入电子邮件地址。 然后,点击安装WordPress。

输入网站详细信息
WordPress安装现已完成。 您可以单击“登录”以转到WordPress管理员(后端)。

WordPress安装完成
输入WordPress管理员用户及其密码以访问WordPress Admin页面。

WordPress管理员登录页面
WordPress管理员:

WordPress管理员
WordPress前端:

WordPress网站
WordPress服务器信息(YLD服务器信息插件):

服务器信息
配置最大上传文件大小
默认情况下,PHP不允许上传2MB以上的文件。 要允许通过WordPress Web界面上传较大的文件,请配置upload_max_filesize设置和post_max_sizin php.ini。
sudo nano /etc/php/7.4/fpm/php.ini
根据您的要求更改上传大小
upload_max_filesize = 256M
根据您的要求更改上传大小。
post_max_size = 256M
在Nginx服务器配置文件中添加client_max_body_size核心模块。
须藤nano /etc/nginx/nginx.conf
该指令可以添加到HTTP块(对于所有站点),特定的服务器块或位置上下文中。
我将指令添加到HTTP块,该块为该服务器上运行的所有站点设置值。
http { .... client_max_body_size 256M; .... }
重新启动服务。
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx
结论
就这样。 我希望您已了解如何在Ubuntu 20.04上使用Nginx安装WordPress。 请在评论部分分享您的反馈。