WordPress 是使用最广泛的开源网络博客和内容管理软件,为大约 6000 万个网站提供支持。 它是用 PHP 编写的,并使用 MariaDB / MySQL 作为数据库。
本教程将帮助您在 Ubuntu 18.04 / Ubuntu 16.04 上使用 Nginx 安装 WordPress 5.x。
我们还将看到如何安装 让我们加密 SSL 用于 WordPress 以确保网站的安全交付。
安装 LEMP 堆栈
在继续之前,您可能需要查看如何安装 LEMP 堆栈。
在 Ubuntu 18.04 上安装 LEMP(Nginx、MariaDB 和 PHP)堆栈
在 Ubuntu 16.04 上安装 LEMP(Nginx、MariaDB 和 PHP)堆栈
安装 PHP 扩展
在 Ubuntu 操作系统上安装和运行 WordPress 需要以下扩展。
sudo apt install -y php-mysql 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 服务器块。 虚拟主机配置文件可以在 /etc/nginx/conf.d 目录。
服务器块需要域名、端口号、文档根目录、日志位置、快速 CGI 等。
假设以下,
域名: www.itzgeek.net
文件根: /sites/www.itzgeek.net/public_html/
日志: /sites/www.itzgeek.net/logs/
创建虚拟主机。
如果你已经从 Ubuntu 仓库安装了 Nginx,虚拟主机配置文件可以在 /etc/nginx/sites-enabled,文件路径将类似于 /etc/nginx/sites-enabled/www.itzgeek.net.conf.
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf
放置以下内容。
server { listen 80; 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: 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 安装 Let’s Encrypt SSL(可选)
在目前的情况下,几乎所有网站都使用 HTTPS(SSL 证书)来确保真实性、安全帐户,以保持用户通信的私密性。 谷歌要求所有者 切换到 HTTPS 以获得更好的安全性并提高排名。
安装证书机器人
要生成 SSL 证书,请安装 Certbot ACME 客户端 在你的系统上。 它无需停机即可处理证书颁发和证书安装。
Certbot 客户端在 Ubuntu 存储库中不可用。 因此,我们需要在 Ubuntu 上配置 Certbot PPA。
sudo apt update sudo apt install -y software-properties-common sudo add-apt-repository universe sudo add-apt-repository ppa:certbot/certbot sudo apt update
现在,安装 certbot 客户端。
sudo apt install -y certbot python-certbot-nginx
更新/更改 DNS 记录
访问您的域注册商并为您的域创建 A/CNAME 记录。
等待一段时间让记录传播。
安装 Let’s Encrypt SSL 证书
使用 certbot 命令生成并安装 Let’s Encrypt 证书。
sudo certbot --nginx
输出:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] << Enter email id to receive notification - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 << Accept Terms of Service - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 << Subscribe to newsletter Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: www.itzgeek.net - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1 << Install SSL certificate for www.itzgeek.net Deploying Certificate to VirtualHost /etc/nginx/conf.d/www.itzgeek.net.conf Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 < Redirect HTTP traffic to HTTPS site Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/www.itzgeek.net.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://www.itzgeek.net You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 2019-12-12. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. 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
使用 Nginx 将非 www HTTP 请求重定向到 www HTTPS
我们现在将创建一个新的服务器块,以将来自非 www HTTP 站点的流量重定向到 www HTTPS 站点。 IE, https://itzgeek.net >> https://www.itzgeek.net.
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf
在文件末尾添加以下块。
# Redirect NON-WWW HTTP to WWW HTTPS
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 创建数据库
登录到 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';
Exit 来自 MariaDB 外壳。
quit
下载 WordPress
下载最新的 WordPress 包。
wget https://wordpress.org/latest.tar.gz
提取 WordPress 包。
tar -zxvf latest.tar.gz
将 WordPress 文件移动到文档根目录。
sudo mv wordpress/* /sites/www.itzgeek.net/public_html/
使 Apache (www-data) 用户作为 WordPress 网站的所有者。
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
打开浏览器并访问:
https://your-web-site-url
您将获得 WordPress 安装向导。
选择 WordPress 安装的语言。
确保您拥有 WordPress 数据库名称、数据库用户和数据库用户的密码。 如果您准备好了,请单击 Let’s go!。
在这个页面输入数据库信息,让WordPress连接数据库。
如果连接到数据库成功,您将看到以下页面。 单击运行安装。
在此页面中,输入站点标题,WordPress admin, 和密码(您选择的),然后是电子邮件地址。 然后,单击安装 WordPress。
WordPress 安装现已完成。 您可以单击登录以转到 WordPress 管理页面。
Enter WordPress admin 用户及其密码以访问 WordPress 管理页面。
WordPress 管理员:
WordPress 前端:
WordPress 服务器信息(YLD 服务器信息插件):
很少有额外的配置
为 WordPress 配置最大文件大小上传
默认情况下,PHP 不允许超过 2MB 的网络上传。 要允许通过 WordPress Web 界面上传更大的文件,请配置 上传最大文件大小 设置在 配置文件.
sudo nano /etc/php/7.2/fpm/php.ini
根据您的要求更改上传大小
; Maximum allowed size for uploaded files. ; https://php.net/upload-max-filesize upload_max_filesize = 256M
重新启动 php7.2-fpm 服务。
sudo systemctl restart php7.2-fpm
在 Nginx 上为 WordPress 配置 client_max_body_size
即使您在上一步中更改了最大上传文件大小,您也可能会遇到以下错误。
2019/09/13 04:40:08 [error] 3803#3803: *517 client intended to send too large body: 9872781 bytes, client: 49.207.143.143, server: www.itzgeek.net, request: "POST /wp-admin/update.php?action=upload-theme HTTP/1.1", host: "www.itzgeek.net", referrer: "https://www.itzgeek.net/wp-admin/theme-install.php?browse=featured"
添加 client_max_body_size Nginx 服务器配置文件中的核心模块。
sudo nano /etc/nginx/nginx.conf
该指令可以添加到 http 块(对于所有站点)、特定服务器块或位置上下文中。 我正在将指令添加到 http 块,该块设置此服务器上运行的所有站点的值。
http { .... client_max_body_size 256M; .... }
此外,我们必须更改的值 post_max_size 在 php7.2-fpm 中。
sudo nano /etc/php/7.2/fpm/php.ini
根据您的要求更改上传大小。
post_max_size = 256M
重新启动服务。
sudo systemctl restart php7.2-fpm sudo systemctl restart nginx
结论
就这样。 我希望您已经学会了如何在 Ubuntu 18.04 / Ubuntu 16.04 上使用 Nginx 安装 WordPress 5.x。 请在评论部分分享您的反馈。