Optimizing Your Headway Site – Installing WordPress on Nginx

Introduction

Today we are going to look at installing WordPress on nginx and Ubuntu. This is much like our part 4 in which we installed WordPress on Apache and Ubuntu. If you are going to use Apache for your server, you are off easy this week as this week focuses on nginx.

So let’s get going installing WordPress. If you have not taken a look at how to install a LEMP environment, hit out our previous tutorial on how to do just that.

Step One – Download WordPress

We can install WordPress from their website with:

wget http://wordpress.org/latest.tar.gz

Now we need to unzip the zip file we just downloaded. To do that, we’ll run this command:

tar -xzvf latest.tar.gz

This puts the WordPress package straight into your user’s home directory.

Step Two – Create Database and User

We need to change gears a second and create our user and database. You almost never want to use your root user, especially when dealing with code. So let’s create a user for WordPress now.

To open a MySQL shell you need to type in the following:

mysql -u root -p

You’re going to login to MySQL with your root user, so put that password in when it prompts you to. It’s a good time to point out that all MySQL commands must end with a semi-colon.

Once we’re in to the MySQL shell, let’s first create the database we’ll be using with our install of WordPress. *Please note that you can replace the database, user name, and password with whatever you want to call them.

CREATE DATABASE wordpress_db_name;

New we need to create our new user.

CREATE USER ajs_new_wp_user@localhost;

To set the password for our new user:

SET PASSWORD FOR ajs_new_wp_user@localhost = PASSWORD("enterpasswordHERE");

We’re almost done. We have one last step while we are in here and that’s to give privileges to our new user. Without giving them privileges, the WordPress install process will not be able to run:

GRANT ALL PRIVILEGES ON wordpress_db_name.* TO ajs_new_wp_user@localhost IDENTIFIED BY 'enterpasswordHERE';

And finally let’s refresh MySQL:

FLUSH PRIVILEGES;

Now we can exit out of the MySQL shell:

exit

Step Three – Change Settings in WordPress’ Configuration File

Since WordPress doesn’t automatically come with a wp-config.php file, we need to copy the sample file over so we can edit it and add our information to it.

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php

Next, let’s open up the config file:

sudo nano ~/wordpress/wp-config.php

Now we need to find the MySQL section in order to add our information.

// ** MySQL settings - You can get this info from your web host **//
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress_db_name');
/** MySQL database username */
define('DB_USER', 'ajs_new_wp_user');
/** MySQL database password */
define('DB_PASSWORD', 'enterpasswordHERE');

Now save and exit the file.

Step Four – Copy Files

Our final step is to transfer our unzipped WordPress folder to our website’s root directory.

sudo rsync -avP ~/wordpress/ /var/www/

We need to set the permissions on the folder as well, but we need to switch into the new directory first, so let’s do that:

cd /var/www/

We’ll give ownership of the directory to the nginx user, replacing the “username” with the name of your server user:

sudo chown www-data:www-data * -R
sudo usermod -a -G www-data username

Step Five – Setup Up the Nginx Server Blocks

Now we need to setup the WordPress virtual host. Create a new file for the WordPress host like so:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress

Open the WordPress Virtual host:

sudo nano /etc/nginx/sites-available/wordpress

We are going to make some changes that are below:

server {
 listen 80;
 root /var/www;
 index index.php index.html index.htm;
server_name 192.34.59.214;
location / {
 try_files $uri $uri/ /index.php?q=$uri&$args;
 }
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/www;
 }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9$
 location ~ .php$ {
 #fastcgi_pass 127.0.0.1:9000;
 # With php5-fpm:
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 }
}

Here are the details to help you out:

  • Change the root to /var/www/
  • Add index.php to the index line.
  • Change the server_name from local host to your domain name or IP address.
  • Change the “try_files $uri/ /index.html;” line to “try_files $uri $uri/ /index.php?q=$uri&$args;” to enable WordPress permalinks with nginx
  • Uncomment the correct lines in “location ~.php${” section

Now save and exit.

Step Six – Activate Our Server Block

Now that all the configuration for WordPress has been completed, we need to activate the server block by creating a symlink:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress

Additionaly, we need to delete the default nginx block:

sudo rm /etc/nginx/sites-enabled/default

The last thing to do is install php5-mysql:

sudo apt-get install php5-mysql

And then restart nginx and php-fpm:

sudo service nginx restart
sudo service php5-fpm restart

Step Seven – Checking Our Results!

Our WordPress installation page is up and waiting for us. Let’s access the page by adding /wp-admin/install.php to your site’s domain or IP address and fill out the short form.

And that should be it!

Your homework will be to install Headway and any plugins you want for your site.

Next week we’ll be looking at the caching level and what software we can install to help with caching. Until then!

Spread the word!

8 Responses

  1. Thanks Headway! This is a sweet and concise tut. I’m importing a database from another WP install. How easy should it be to get phpMyAdmin installed on nginx?

    • >> How easy should it be to get phpMyAdmin installed on nginx?

      Depends. With a sub-domain specific for phpMyAdmin, and if you can follow the above guidelines to make WP work with Nginx, then it is very easy. Here’s how you can make phpMyAdmin to work with Nginx…

      1. Create a sub domain for phpMyAdmin, for ex, pma.domainname.com
      2. Download phpMyAdmin, extract it and note down the path where phpMyAdmin is extracted
      3. Copy the WP-Nginx vhost entry and replace the following things in it…
      a) server_name should point to the sub domain
      b) root should point to the path of phpMyAdmin.
      4. Test the config and then reload / restart Nginx.
      5. Visit the sub-domain that should contain the working phpMyAdmin now.

      I hope that helps.

    • I would agree with @Pothi. However, for production servers, I typically do not want to install phpMyAdmin or anything else that gets into the database. I would suggest learning command-line mysql so you can be a little more secure.

      However, if you are looking for something, there’s tons of temporary solutions than installing phpMyAdmin.

Leave a reply

Copyright © 2016 Vesped Inc. All Rights Reserved. Proudly Powered by Headway and WordPress