Running WordPress on top of Nginx web server might improve your website speed/performance.. but that can be argued.

Nginx web server is an alternative to Apache2 web server. It is probably the second most popular web servers today in used. It’s popularity is growing among webmasters because of how it uses system memory to process web requests. There may be other reasons Nginx is popular, but I am not going to spend too much time on that.

For more information on Nginx web server, please visit this page or click the link below.  http://nginx.org/

When you’re ready to install Nginx on Ubuntu 15.04 and WordPress running on top of it, then continue below.

The first to do before going further is update your Ubuntu machine. To do that, run the commands below.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

After that, run the commands below to install Nginx on Ubuntu.

sudo apt-get install nginx

Running the commands above will install version 1.6.2 of Nginx from the stable branch. This version of Nginx will work but it’s a pretty old, but stable version.

To learn how to install the latest version of nginx on Ubuntu, read this post .

After that, run the commands to enable Nginx to automatically startup when your server starts.

sudo systemctl enable nginx

To start Nginx web server, run the commands below.

sudo systemctl start nginx

Next, browse to the server using its IP or hostname and you’ll get Nginx’s default test page.. this is how you tell Nginx is up and functioning OK.

Now that Nginx is running.. let’s go and install WordPress. By default, Nginx creates and make /var/www/html its default root directory. So content at this location will be fetched and retrieve when requested.

To access WordPress content, put it in Nginx default root directory (/var/www/html).. To do that run the commands below to download WordPress latest content.

cd /tmp/ && wget http://wordpress.org/latest.tar.gz

Then run the commands below to extract that content.

tar -xvzf latest.tar.gz

Finally, copy all WordPress content to the root directory for Nginx

sudo mv wordpress/* /var/www/html/

WordPress content is in the right place but not ready to be used. WordPress needs database servers and scripts to function. For this tutorial, we’re going to install MariaDB database server..

To do that run the commands below.

sudo apt-get install mariadb-server mariadb-client

To start MariaDB server run the commands below.

sudo systemctl start mysql

To enable MariaDB to automatically startup when your server starts, run the commands below.

sudo systemctl enable mysql

To configure MariaDB database server, run the commands below.

sudo mysql_secure_installation

When prompted, follow the options below

Next, choose Yes for the rest of the prompts until you’re done.

  • Enter current password for root (enter for none): Press Enter for none.
  • Set root password? Y
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database and access to it? Y
  • Reload privilege tables now? Y

The database server configuration is done.

The next step is to create WordPress database on the database server.. WordPress needs databases to store content. To create a database for WordPress, follow the steps below.

Run the commands below to sign onto the database. When prompted for a password, type the root password you created in the earlier steps.

mysql -u root -p

Next, run the commands below to create a new database called wpdb.

CREATE DATABASE wpdb;

Next, run the commands below to create a new database user called wpuser with a new password.

CREATE USER [email protected] IDENTIFIED BY 'new_password_here';

Then run the commands below to give the user full access to the newly created database you created earlier.

GRANT ALL ON wpdb.* to [email protected];

Finally, run the commands below to refresh the database permissions table and exit.

FLUSH PRIVILEGES;

exit.

Now the database is done, go and install PHP along with other PHP modules. WordPress needs PHP to function.

To do that, run the commands below.

sudo apt-get install php5 php5-mysql php5-fpm php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

After installing PHP, configure WordPress to use these settings. To do that run the commands below to copy WordPress sample file and create a wp-config.php file.

sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php

Then open wpo-config.php file and make the following changes to including the database info.

sudo vi /var/www/html/wp-config.php

Add the database name, username and password.

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘wpdb‘);

/** MySQL database username */
define(‘DB_USER’, ‘wpuser‘);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘password‘);

Next, change the permissions on the root folder by running the commands below.

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

Finally, open Nginx’s default site file at /etc/nginx/sites-available/default by running the commands below.

sudo vi /etc/nginx/sites-avaiable/default

Then make the follow change to enable PHP support.

Under root /var/www/html;  add

index index.php index.html index.htm;

then change the block code to enable PHP scripts to function

location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Save the file, restart Nginx and you’re done.

Next, go to /etc/php5/fpm/pool.d/www.conf

Verify the listen line is:

listen unix:/var/run/php5-fpm.sock;

Save and you’re done.  Restart Nginx and PHP5-FPM.

sudo systemctl reload nginx
sudo systemctl reload php5-fpm

Browse to the server via IP or hostname and you should see WordPress default setup page.

Enjoy!