How to Install WordPress with Nginx on Ubuntu 12.04

This tutorial guides you to install WordPress with Nginx on Ubuntu 12.04.

WordPress is a free and open source Content Management System, built on PHP and MySQL. It provides over 30,000 plugins for easy customization of appearance and functionality. WordPress is the most popular blogging platform, estimated to be running on 60 million sites and seen by tens of millions of people every day.

 

Prerequisites

  • This tutorial assumes that you already have LEMP (Linux, Nginx, MySQL and PHP) installed in your server. If you don’t have LEMP stack on your sever, you can find the steps for doing the set up here.
  • The user should have root privileges so as to have administrative capabilities and permissions. You can check out how to grant root privileges for user here.

Now that you have the user with root privileges and mandatory software, let’s get down to installing WordPress.

 

Download WordPress

 

You can download the WordPress package directly from wordpress.org.

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

Before executing the download, verify whether wget is already installed on your server. You will have the WordPress package in zipped format inside user’s home directory. Unzip it using the command given below.

tar -xzvf latest.tar.gz

 

Create a Database and a User for WordPress

 

Once the WordPress files are unzipped, you can locate them under wordpress directory within home directory.

Now, log into MySQL shell using MySQL root password.

mysql -u root -p

Once logged in, you have to create a wordpress database and a user in that database. You can set a password for the new user. Find the MySQL commands for setting up the database and the user given below.

For creating the database:

CREATE DATABASE wordpress;

Query OK, 1 row affected (0.00 sec)

Note: Here, I have given the name wordpress for the database. You can give any name you wish. However, a unique name is recommended.

 

For creating the user:

CREATE USER [email protected];

Query OK, 0 rows affected (0.00 sec)

 

For setting the password for the user:

SET PASSWORD FOR [email protected]= PASSWORD("yourpassword");

Query OK, 0 rows affected (0.00 sec)

Note: Here, I have given the name wordpressuser1 for the newly created user. You can set password as you prefer.

Now, you can grant all the privileges for the new user. Without proper privileges, you may end up with installation and database permission issues.

GRANT ALL PRIVILEGES ON wordpress.* TO [email protected] IDENTIFIED BY 'yourpassword';

Query OK, 0 rows affected (0.00 sec)

 

Finish up this step by refreshing and exit out of the shell.

FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)
Exit

 

Configure the WordPress Installation

 

Here we are going to set up WordPress and link it to the database that we created for it.

For that, first you need to locate the sample wordpress configuration file from the wordpress directory and copy it to a new file which you can edit later. Thus a new usable wordpress config will be created.

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

Open the wordpress config for editing.

sudo nano ~/wordpress/wp-config.php

Search for the code snippet that contains MySQL settings and update the name of database, username and password correctly.

// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */

define('DB_NAME', 'wordpress');

 

/** MySQL database username */

define('DB_USER', 'wordpressuser1');

 

/** MySQL database password */

define('DB_PASSWORD', 'yourpassword');

After updating the fields, save and exit.
 

Copy the WordPress Files

 

Create a directory to keep the wordpress files:

sudo mkdir -p /var/www

We need to move the unzipped wordpress files to website’s root directory.

sudo cp -r ~/wordpress/* /var/www

It is advisable to modify the permissions of /var/www for automatic updating of WordPress plugins and SFTP file editing in the future.

For granting permissions, switch to the web directory:

cd /var/www/

Give ownership of the directory to the Nginx user.

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

You should replace the username with the actual user of your server.

 

Setting Up Nginx Server Blocks

 

In this step, we will set up the WordPress virtual host. Create a new file for WordPress host by copying the format of default configuration:

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

Now, open up the WordPress virtual host:

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

Here is a list of changes that needs to be made:

  1. Change the root to /var/www
  2. Add index.php to index line.
  3. Replace local host (server_name) to your domain name or IP.
  4. Enable WordPress permalinks with nginx by updating “try_files $uri $uri/ /index.html;” as  “try_files $uri $uri/ /index.php?q=$uri&$args;”
  5. Uncomment the lines in “location ~ \.php$ {“ section

Once you are done with the changes, the file should look like this:
 

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:9000

        location ~ \.php$ {
                try_files $uri =404;
                #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;
                 }
         }

After making the changes, save and exit the file.

 

Activating the Server Block

 

We need to activate the server block by creating a symbolic link.

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

You can delete the default nginx server block.

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

Install php5-mysql using the command:

sudo apt-get install php5-mysql

Restart nginx and php-fpm:

sudo service nginx restartsudo service php5-fpm restart

 

Access and Finalize the WordPress Installation

 

This is the last stage of your WordPress installation. Open your browser and access the page by tailoring your site’s IP address or domain with /wp-admin/install.php. The link may look like this:

www.example.com/wp-admin/install.php

The WordPress online installation page will be opened and it should look similar to this:

Wordpress on ArchLinux

 

Fill out the required fields in the form and finalize the WordPress installation.

support2 has written 111 articles

Leave a Reply