Michał Karzyński

Setting up WordPress on Nginx

WordPress is an …

Prerequisites

I assume you have a server available on which you have root privileges. I am using a server running Debian 7, so everything here should also work on an Ubuntu server or other Debian-based distribution. If you’re using an RPM-based distro (such as CentOS), you will need to replace the apt-get commands by their yum counterparts and if you’re using FreeBSD you can install the components from ports.

If you don’t have a server to play with, I would recommend the inexpensive VPS servers offered by [Digital Ocean][digital_ocean_referal]. If you click through [this link][digital_ocean_referal] when signing up, you’ll pay a bit of my server bill :)

I’m also assuming you configured your DNS to point a domain at the server’s IP. In this text, I pretend your domain is example.com

Update your system

Let’s get started by making sure our system is up to date.

$ sudo apt-get update
$ sudo apt-get upgrade

Install Nginx

Let’s install Nginx and set it to start automatically during system boot.

$ sudo apt-get install nginx
$ sudo service nginx start

You can navigate to your server (http://example.com) with your browser and Nginx should greet you with the words “Welcome to nginx!”.

Install PHP-FPM

Under Apache PHP code is executed by the web server (via mod_php). The Nginx philosophy is somewhat different. It’s a reverse proxy rather then a server, so it’s not running any code itself. Instead it can serve (proxy) data generated by CGI applications running on your system.

For PHP this is PHP-FPM (FastCGI Process Manager). This is a daemon process which waits for incoming requests to execute PHP code, runs the scripts and returns their output. More information can be found on the PGP-FPM site.

$ sudo apt-get install php5-fpm

Configure PHP-FPM

Edit the /etc/php5/fpm/php.ini and change cgi.fix_pathinfo to 0.

$ sudo vim /etc/php5/fpm/php.ini
1
2
3
4
5
6
7
8
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Now check the php5-fpm configuration file /etc/php5/fpm/pool.d/www.conf and make sure that php5-fpm communicates with the outside world through a socket file:

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

Restart the php-fpm service:

$ sudo service php5-fpm restart

Create a default settings file for FastCGI applications served through Nginx

/etc/nginx/fastcgi.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#fastcgi.conf
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

http://wiki.nginx.org/**FcgiExample**

Set up MySQL

Let’s start by installing the MySQL server package.

$ sudo apt-get install mysql-server

During the installation process you will be asked for a password for the root user. If you don’t set it during installation, you can set it later using the following command (substitute a password for NEWPASSWORD).

$ mysqladmin -u root password NEWPASSWORD

Create a MySQL database and user for WordPress

$ mysql -u root -p

mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_unicode_ci;

mysql> CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'dKtbiHTPrkAzHUUrWRcuhMDqlpcszSQY0kd6vSoh5yotkdx7gCwRkAmGKFJVotu';

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';

mysql> FLUSH PRIVILEGES;

Install the PHP MySQL extension

$ sudo apt-get install php5-mysqlnd

(Native driver over standard, use standard if native not available)

Install the PHP GD extension

$ sudo apt-get install php5-gd

Install WordPress on server

$ sudo mkdir -p /var/www/

$ cd /var/www/

$ wget -nv -O - https://wordpress.org/latest.tar.gz | sudo tar -xzv

$ sudo chown -R `whoami` /var/www/wordpress

$ mkdir /var/www/wordpress/wp-content/uploads

$ sudo chown www-data /var/www/wordpress/wp-content/uploads

Create an Nginx virtual server configuration for WordPress

$ sudo vim /etc/nginx/sites-available/example-wordpress

IN VM

$ sudo ln -s ../sites-available/example-wordpress /etc/nginx/sites-enabled/example-wordpress


└ $ sudo service nginx configtest
Testing nginx configuration: nginx.
┌[02:13:57] michal@webmin-host /etc/nginx/sites-available  [0]
└ $ sudo service nginx reload
Reloading nginx configuration: nginx.

Configure WordPress

Point your browser to example.com

Click the Create a Configuration File button.

Follow onscreen instructions

PICTURE

Save generated wp-config.php file to /var/www/wordpress/wp-config.php

Navigate to http://example.com again and complete the WordPress setup process.

Your Nginx-powered WordPress site is ready to go.

Configure WP Super Cache

316 vim example.com 332 sudo ln -s ../sites-available/example.com /etc/nginx/sites-enabled/example.com 333 sudo service nginx configtest 334 sudo service nginx reload

More info: https://rtcamp.com/wordpress-nginx/tutorials/single-site/wp-super-cache/

https://rtcamp.com/wordpress-nginx/tutorials/

Comments