php-fpm nginx_具有PHP-FPM和Nginx的闪电般的WordPress

php-fpm nginx

Managed servers are slow. They run old versions of PHP on ancient copies of Apache, and loathe the Digg effect (or any similar sudden influx of traffic). In this tutorial, I’ll show how to build a server capable of withstanding a front-page Digg placement, step by step. This will mean your business stays online when it’s most important—when everyone is looking.

受管服务器速度很慢。 他们在Apache的古代副本上运行旧版本PHP,并讨厌Digg效应(或任何类似的突然流量涌入)。 在本教程中,我将逐步展示如何构建能够承受头版Digg放置的服务器。 这意味着您的公司在最重要的时刻(每个人都在看时)保持在线。

We’ll go through the process of building a super-fast, bulletproof custom web server for WordPress. The technology stack we’ll use is Ubuntu, nginx, PHP-FPM, and MySQL. In a future article we’ll look at adding memcached to the mix to take performance even further.

我们将完成为WordPress构建超快速,防弹的自定义Web服务器的过程。 我们将使用的技术堆栈是Ubuntu,nginx,PHP-FPM和MySQL。 在以后的文章中,我们将介绍将memcached添加到组合中以进一步提高性能。

为什么选择VPS? (Why VPS?)

VPS stands for virtual private server. Basically, you receive a piece of a big, expensive machine for a low monthly price. You pay for a guaranteed amount of RAM, and have access to a certain amount of CPU power.

VPS代表虚拟专用服务器。 基本上,您会以低价获得一台昂贵的大型机器。 您需要支付一定数量的RAM,并可以使用一定数量的CPU能力。

This can be a much better deal than managed hosting once you have a few websites up and running. However, you have to manage everything yourself, and take care of your server when something goes wrong.

一旦启动并运行了一些网站,这比托管托管要好得多。 但是,您必须自己管理一切,并在出现问题时照顾服务器。

为什么选择nginx? (Why nginx?)

Nginx is a small, lightweight web server and reverse proxy. It runs on 5.2% of the top one million web servers. In particular, nginx is well-suited to a high traffic site. Its lightweight nature, compared to Apache, means an nginx server can run in a much smaller memory footprint. This makes it the web server of choice for people looking to squeeze the most performance out of a VPS solution.

Nginx是一个小型轻巧的Web服务器和反向代理。 它在前一百万个Web服务器中的5.2%上运行。 特别是,nginx非常适合高流量站点。 与Apache相比,它的轻量级特性意味着nginx服务器可以在更小的内存空间中运行。 这使得它成为希望从VPS解决方案中获得最大性能的人们的首选Web服务器。

为什么选择PHP-FPM? (Why PHP-FPM?)

Included with version 5.3.3 of PHP (released in July of this year) is a new FastCGI manager called PHP-FPM. PHP-FPM is a daemon that spawns processes to manage your online applications. So, rather than have your web server running plugins to display and process your PHP code, your PHP code is now run natively, by PHP-FPM.

PHP的5.3.3版(于今年7月发布)中包含一个称为PHP-FPM的新FastCGI管理器。 PHP-FPM是一个守护程序,它生成用于管理您的在线应用程序的进程。 因此,您的Web代码不再由运行插件的Web服务器来显示和处理PHP代码,而是由PHP-FPM在本地运行。

For our example WordPress installation, we’ll set up an nginx server to serve our static files. When a user requests a PHP page, the nginx server will forward the request to PHP itself. PHP-FPM runs its own server, waiting for users to request their pages.

对于我们的示例WordPress安装,我们将设置一个Nginx服务器来提供我们的静态文件。 当用户请求一个PHP页面时,nginx服务器会将请求转发给PHP本身。 PHP-FPM运行自己的服务器,等待用户请求其页面。

This separation of features means you can see some incredible speed gains.

功能的分离意味着您可以看到令人难以置信的速度提升。

为什么选择MySQL? (Why MySQL?)

Basically, because there’s no other choice. At the moment, WordPress only supports MySQL. I always use memcached to lighten the load on MySQL. As I mentioned, I’ll be covering the use of memcached in a future post.

基本上,因为别无选择。 目前,WordPress仅支持MySQL。 我总是使用memcached减轻MySQL的负担。 如前所述,我将在以后的文章中介绍memcached的用法。

放在一起 (Putting It All Together)

Since all this software is relatively cutting edge, we’re going to go ahead and build nearly everything from source. This means you’ll need to have build-essential or an equivalent package installed on your system. I’ll assume you have basic familiarity with Linux and SSH.

由于所有这些软件都是相对尖端的,因此我们将继续进行并从源代码构建几乎所有内容。 这意味着您需要在系统上安装必需的或等效的软件包。 我假设您对Linux和SSH有基本的了解。

If you’re stuck developing in Windows and want to give this setup a go, I’d recommend installing an Ubuntu server inside the free VirtualBox virtualization application.

如果您不愿在Windows上进行开发,并且想进行此设置,建议您在免费的VirtualBox虚拟化应用程序中安装Ubuntu服务器。

第一步:安装nginx (Step One: Installing nginx)

I recommend downloading and installing nginx from source, as the version in most Linux distributions’ package managers are older than we’d like. Nginx is actively developed, and we might as well take advantage of the developers’ hard work.

我建议从源代码下载并安装nginx,因为大多数Linux发行版的程序包管理器中的版本都比我们想要的旧。 Nginx是积极开发的,我们不妨利用开发人员的辛勤工作。

We’ll start by getting the dependencies; then we’ll grab nginx and build it (check the downloads page for the latest version available):

我们将从获取依赖关系开始; 然后我们将获取nginx并进行构建(请查看下载页面以获取可用的最新版本):

sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
cd ~/downloads
wget http://nginx.org/download/nginx-0.8.53.tar.gz
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53

Now, before we run through configure, we need to set a few preferences. Specifically, where nginx should be installed to. We’ll also set up nginx to use the SSL module, so https works for our server:

现在,在进行配置之前,我们需要设置一些首选项。 特别是应将nginx安装到的位置。 我们还将设置nginx以使用SSL模块,因此https适用于我们的服务器:

./configure --pid-path=/var/run/nginx.pid --sbin-path=/usr/local/sbin --with-http_ssl_module

Finally, we’ll do a make, and a make install:

最后,我们将进行make和make install:

make
sudo make install

We’ll make a few quick edits to the nginx configuration file, which is located at /usr/local/nginx/conf/nginx.conf. At the top of that file you’ll see these two lines:

我们将对nginx配置文件进行一些快速编辑,该文件位于/usr/local/nginx/conf/nginx.conf 。 在该文件的顶部,您将看到以下两行:

# user nobody;
worker_processes 1;

Uncomment the user line and change nobody to www-data www-data, then change worker_processes to 2 instead of 1. Have a look at the rest of the file; there are a number of settings for logs and other options, a sample server declaration, and a few commented-out examples. We’ll be coming back to this file later, but for now you can save it and close it.

取消注释用户行,将任何人都更改为www-data www-data,然后将worker_processes更改为2而不是1。 有许多日志和其他选项的设置,一个服务器声明示例,以及一些注释掉的示例。 稍后我们将返回此文件,但是现在您可以保存并关闭它。

nginx doesn’t come with an init script that we can run when the system boots, but there are plenty of good ones available online. Let’s grab one and set it up:

nginx没有附带一个我们可以在系统启动时运行的初始化脚本,但是在线上有很多不错的脚本。 让我们抓住其中一个进行设置:

cd ~/downloads
wget http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
tar xfv nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
sudo mv nginx /etc/init.d/nginx 
sudo update-rc.d -f nginx defaults

Nginx will now start when your system starts, and you can control it with these commands:

Nginx现在将在系统启动时启动,您可以使用以下命令对其进行控制:

sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart

That’s it! At this point you should be able to start up nginx, hit http://localhost/ in your browser, and see the default “Welcome to nginx!” page. Next, we’ll install PHP 5.3.3, then finally, configure everything.

而已! 此时,您应该能够启动nginx,在浏览器中点击http:// localhost /,并看到默认的“欢迎使用nginx!”。 页。 接下来,我们将安装PHP 5.3.3,然后最后配置所有内容。

第二步:安装PHP 5.3.3 (Step Two: Installing PHP 5.3.3)

Now we’ll install PHP from source. Feel free to change any of the ./configure parameters as you require—the important ones for our purposes are the --enable-fpm and --with-fpm lines:

现在,我们将从源代码安装PHP。 可以根据需要随意更改任何./configure参数-对我们而言,重要的参数是--enable-fpm--with-fpm行:

sudo apt-get install autoconf2.13 libbz2-dev libevent-dev libxml2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libxpm-dev libfreetype6-dev libt1-dev libmcrypt-dev libmysqlclient-dev libxslt-dev mysql-common mysql-client mysql-server
cd ~/downloads
wget http://us3.php.net/get/php-5.3.3.tar.gz/from/us.php.net/mirror/
tar zxvf php-5.3.3.tar.gz
cd php-5.3.3
./buildconf --force
./configure 
	--prefix=/opt/php5 
	--with-config-file-path=/opt/php5/etc 
	--with-curl 
	--with-pear 
	--with-gd 
	--with-jpeg-dir 
	--with-png-dir 
	--with-zlib 
	--with-xpm-dir 
	--with-freetype-dir 
	--with-t1lib 
	--with-mcrypt 
	--with-mhash 
	--with-mysql 
	--with-mysqli 
	--with-pdo-mysql 
	--with-openssl 
	--with-xmlrpc 
	--with-xsl 
	--with-bz2 
	--with-gettext 
	--with-fpm-user=www-data 
	--with-fpm-group=www-data 
	--enable-fpm 
	--enable-exif 
	--enable-wddx 
	--enable-zip 
	--enable-bcmath 
	--enable-calendar 
	--enable-ftp 
	--enable-mbstring 
	--enable-soap 
	--enable-sockets 
	--enable-sqlite-utf8 
	--enable-shmop 
	--enable-dba 
	--enable-sysvmsg 
	--enable-sysvsem 
	--enable-sysvshm
 
make
sudo make install

Next, we’ll configure PHP by copying over the default php.ini and php-fpm.conf files, and setting up PHP-FPM to run when the system boots:

接下来,我们将通过复制默认的php.iniphp-fpm.conf文件并配置PHP-FPM在系统启动时运行来配置PHP:

sudo mkdir /var/log/php-fpm
sudo chown -R www-data:www-data /var/log/php-fpm
sudo cp -f php.ini-production /opt/php5/etc/php.ini
sudo chmod 644 /opt/php5/etc/php.ini
sudo cp /opt/php5/etc/php-fpm.conf.default /opt/php5/etc/php-fpm.conf
sudo cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod 755 /etc/init.d/php-fpm
sudo update-rc.d -f php-fpm defaults

Nice! Now PHP-FPM will be running as a daemon at startup.

真好! 现在,PHP-FPM将在启动时作为守护程序运行。

If you get this or similar when trying to start PHP-FPM:

如果在尝试启动PHP-FPM时遇到此问题或类似问题:

Starting pfp-fpm ................................ failed.

Make sure your /etc/init.d/php-fpm file has the right path to PHP-FPM, and also ensure you touch the .pid file so that the process can run.

确保您的/etc/init.d/php-fpm文件具有指向PHP-FPM的正确路径,并确保您触摸.pid文件,以便进程可以运行。

sudo touch /var/run/php-fpm.pid

Success! We now have a running nginx server, and a running PHP-FPM server. All we have to do is combine the two in our site configuration.

成功! 现在,我们有一个正在运行的Nginx服务器和一个正在运行PHP-FPM服务器。 我们要做的就是在我们的站点配置中将两者结合起来。

第三步:设置我们的WordPress网站 (Step Three: Setting Up Our WordPress Site)

Next, we’ll download WordPress, and install it in our localhost directory for testing. I’m assuming we already have our MySQL database set up with a user specifically for our installation; MySQL is set up exactly the same way, whether you’re using nginx and PHP-FPM or the conventional Apache and PHP stack.

接下来,我们将下载WordPress,并将其安装在我们的localhost目录中以进行测试。 我假设我们已经为用户专门安装了MySQL数据库。 无论您使用的是nginx和PHP-FPM还是常规的Apache和PHP堆栈,MySQL的设置方法都完全相同。

First, we’ll set up the directories necessary for our localhost installation:

首先,我们将设置本地主机安装所需的目录:

mkdir ~/public_html
mkdir ~/public_html/localhost/
mkdir -p ~/public_html/localhost/{public,private,logs,backup}
cd ~/downloads
wget http://wordpress.org/latest.zip
unzip latest.zip
mv wordpress/* ~/public_html/localhost/public
cd ~/public_html/localhost/public
vim wp-config-sample.php

Enter your MySQL configuration into wp-config-sample.php. Then, save the file, and rename it to wp-config.php. You now have only one step left! Making nginx and PHP-FPM recognize the new WordPress server.

在wp-config-sample.php中输入您MySQL配置。 然后,保存文件,并将其重命名为wp-config.php。 您现在只剩一步了! 使nginx和PHP-FPM识别新的WordPress服务器。

第四步:为PHP-FPM配置nginx (Step Four: Configuring nginx for PHP-FPM)

We’ll set nginx up to work with Debian/Ubuntu’s normal sites-available and sites-enabled folders for site-by-site configuration. You create site configuration files in sites-available, and then symlink those to sites-enabled to activate them. Let’s first create those two folders:

我们将nginx设置为与Debian / Ubuntu的常规sites-availablesites-enabled文件夹一起使用,以进行逐站点配置。 您可以在sites-available创建站点配置文件,然后将它们符号链接到sites-enabled了站点的文件,以将其激活。 首先创建这两个文件夹:

sudo mkdir /usr/local/nginx/sites-available
sudo mkdir /usr/local/nginx/sites-enabled

You now need to tell nginx to load all the configuration files inside sites-enabled. To do this, edit your /usr/local/nginx/conf/nginx.conf file again. Somewhere inside of the http { ... } block, add the line:

现在,您需要告诉nginx将所有配置文件加载到sites-enabled 。 为此,请再次编辑/usr/local/nginx/conf/nginx.conf文件。 在http { ... }块内的某处,添加以下行:

include   /usr/local/nginx/sites-enabled/*;

Then, cut out the entire server { ... } block in nginx.conf—that’s a default server configuration that we’ll be replacing with our virtual hosts in sites-available.

然后,切掉nginx.conf的整个server { ... }块,这是默认的服务器配置,我们将用sites-available虚拟主机替换它。

Now for the good part. We’ll create a virtual host for our WordPress site, in sites-available. Create a file called localhost (or wordpress, or whatever) inside your sites-available directory. Here’s what to put in it (replace “username” with your username):

现在大部分。 我们将在sites-available为WordPress网站创建一个虚拟主机。 在sites-available目录内创建一个名为localhost (或wordpress或其他名称)的文件。 这是要放入的内容(用您的用户名替换“用户名”):

server { 
  listen 80; 
  server_name localhost; 
	 
  access_log /home/username/public_html/localhost/logs/access.log; 
  error_log /home/username/public_html/localhost/logs/error.log; 

  location / { 
    root /home/username/public_html/localhost/public; 
    index index.php index.html index.htm; 

    if (-f $request_filename) { 
      expires 30d; 
      break; 
    } 

    if (!-e $request_filename) { 
      rewrite ^(.+)$ /index.php?q=$1 last; 
    } 
  } 
  
  location ~ .php$ { 
    fastcgi_pass   localhost:9000;  # port where FastCGI processes were spawned 
    fastcgi_index  index.php; 
    fastcgi_param  SCRIPT_FILENAME    /home/username/public_html/localhost/public/$fastcgi_script_name;  # same path as above 
    fastcgi_param PATH_INFO               $fastcgi_script_name;
    include /usr/local/nginx/conf/fastcgi_params;
  } 
}

The configuration is relatively straightforward: first up, we’re defining the web root directory, and specifying which index files the server should look for. Then we set a 30-day expires header on static files, and redirect any other requests to index.php in our WordPress directory. There are far too many available configuration settings for nginx to cover them all here, but there’s a full list on the nginx wiki.

配置相对简单:首先,我们定义Web根目录,并指定服务器应查找的索引文件。 然后,我们在静态文件上设置30天的Expire标头,并将其他任何请求重定向到WordPress目录中的index.php 。 nginx的可用配置设置太多了,无法在此处进行介绍,但nginx Wiki上完整列表

Now all that’s left is to add your new site to sites-enabled. We do this with a link:

现在剩下的就是将您的新站点添加到sites-enabled站点的sites-enabled 。 我们通过一个链接来做到这一点:

ln -s /usr/local/nginx/sites-available/localhost /usr/local/nginx/sites-enabled/localhost
sudo /etc/init.d/nginx restart

Open up a browser and go to http://locahost/ (or wherever your server is located). You should see the WordPress installation page. Install your WordPress as you usually do. When setting up clean URLs in the WordPress admin, you’ll need to remove index.php from the permalink structure. (For instance, instead of “example.com/index.php/2010/10/10/my-post/” you’d have “example.com/2010/10/10/my-post/”.)

打开浏览器,然后转到http://locahost/ (或服务器所在的位置)。 您应该会看到WordPress安装页面。 像平常一样安装WordPress。 在WordPress管理员中设置干净的URL时,您需要从永久链接结构中删除index.php 。 (例如,不是“ example.com/index.php/2010/10/10/my-post/”,而是“ example.com/2010/10/10/my-post/”。)

And that’s it! Now you have one lean, mean server for your WordPress site. In the next post, I’ll be looking at a few extra configuration tweaks to our nginx setup, and we’ll also be adding memcached to the mix. Stay tuned!

就是这样! 现在,您为您的WordPress网站提供了一台精简,卑鄙的服务器。 在下一篇文章中,我将对nginx设置进行一些额外的配置调整,并且还将在混合中添加memcached。 敬请关注!

翻译自: https://www.sitepoint.com/lightning-fast-wordpress-with-php-fpm-and-nginx/

php-fpm nginx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值