Nginx功能强大-从Apache迁移WordPress的简单步骤

时间:2020-03-05 15:30:28  来源:igfitidea点击:

WordPress在许多Web服务器上都能很好地运行,大多数人已经在Apache Web服务器上创建了教程 ,因为它是一种非常流行且成熟的解决方案。
Apache提供了许多功能,良好的性能,强大的安全性以及对许多编程语言(如PHP,Perl,Python,Tcl等)的支持。
尽管Apache提供了全面而良好的体验,但由于对高性能的关注,在最近几年中,nginx在WordPress运行中变得非常流行。
NGINX紧跟当前趋势,并已计划在2014年底之前包括对HTTP/2的支持。

这使许多WordPress教程 用户承担着将其教程 从Apache迁移到Nginx的任务。
任务不是很复杂,但是有很多陷阱和可能出现的问题。
对于我们的文章,我们将在Ubuntu服务器上将WordPress教程 从Apache迁移到Nginx。

安装Nginx

我们的第一个任务是在服务器上安装Nginx服务器,由于它很受欢迎,因此该软件包已包含在Ubuntu存储库中并得到很好的维护,要安装它,请运行以下命令:

sudo apt-get update
sudo apt-get install nginx

安装php5-fpm

Web服务器不附带对运行PHP的内置支持,而是可以使用称为PHP处理程序的专用软件来运行PHP内容,最受欢迎的选择是php5-fpm,要安装它,请运行以下命令:

sudo apt-get install php5-fpm

默认情况下,php-fpm使用端口进行连接,因为我们将在本地使用它,因此建议改用套接字,为此,请使用我们喜欢的文本编辑器打开文件/etc/php5/fpm/pool.d/www.conf并确保侦听线如下所示:

listen = /var/run/php5-fpm.sock

如果它指向一个IP地址和端口,请更改它,使其看起来像上面的一样。

完成后,重新启动php5-fpm服务器

service php5-fpm restart

配置nginx

现在,我们必须在nginx中配置站点块,因此打开名为/etc/nginx/sites-enabled/default的配置文件,如下所示:

server {
root /var/www/html;
index index.php;
server_name www.theitroad.com;
location/{
try_files $uri $uri//index.php?q=$request_uri;
}
rewrite /wp-admin$$scheme://$host$uri/permanent;
location ~ \.php${
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)${
expires max; log_not_found off; access_log off;
}
}

这是没有注释和一些添加内容的默认服务器块的样子,让我解释每一行:

  • root-指向文件的位置,默认情况下为/var/www/html,但可以为/home/username/public_html
  • index-是Web服务器将自动为客户端提供服务的文件,对于WordPress和大多数php,它是index.php
  • server_name-要访问的的域名syou
  • 位置和重写-它们特定于wordpress并有助于seo url的
  • 第二个位置-指向php5-fpm文件,因此网络服务器可以处理php文件
  • 最后一个位置-将为静态文件添加最大过期时间,这有助于我们利用浏览器缓存

完成配置后,我们需要执行的最后一步是停止apache并运行新的nginx服务器,我们可以通过运行以下命令来做到这一点:

service apache2 stop
service nginx start

现在,我们可以继续检查新的,如果一切都很好,则可以最终删除apache2项目,因此可以确保不会错误启动它:

apt-get remove apache2

(可选)W3 Total Cache支持

如果我们使用流行的W3 Total Cache插件来加快站点速度,并且应该这样做,则需要向nginx配置中添加以下规则(由WordPress Codex提供):

#W3 TOTAL CACHE CHECK
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
# Use cached or actual file if they exists, otherwise pass request to WordPress
location/{
try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri//index.php?$args ;
}