Search

Installing WordPress on Nginx with PHP-FPM and MySQL using Ubuntu

    Sumit Pore
Listen to this article

WordPress as a CMS is usually installed over Apache servers for most websites. However, If yours’ is one of the websites that generates a lot of traffic, chances are that an Apache server would go dead on you during peak traffic hours. This is mostly because Apache processes data based on processes rather than the latest thread based processing. A good way to prevent this from happening is to use Nginx as the Web Server with PHP-FPM (PHP – Fast Process Manager) and MySQL. This will also ensure that your website is ready for high traffic in the future.

In this post, we will show you how to install WordPress on Nginx with PHP-FPM and MySQL.

Installing WordPress on Nginx with PHP FPM and MySQL

We have done our best to keep it really simple, all you need to do is follow the steps given below :

1. Switch to Root Sudo

You must switch to root using the command given below since we are going run most of the commands as root.

sudo -i

2. Installing Packages

Once you have switched to root, you can now directly go ahead and install all the packages which we’ll need to get things running up. We’ll install PHP5-FPM, Nginx, MySQL-server and PHP5-MySQL in one single command as given below.

apt-get install php5-fpm nginx mysql-server php5-mysql

If you aren’t getting it, check the screenshot given below to understand what’s going on in the terminal. Currently we have entered the second command in terminal and we’re about to press enter.

Installing WordPress on Nginx - Screenshot 1

Hit enter and the command will be processed by the terminal.

3. Setting up MySQL Password

You will be asked to enter the MySQL password. For now We’ll just enter the password as “pass”. Note – Always use a complex password. We’re using password as “pass” to keep things as simple as possible. Below is a screenshot of the terminal asking for MySQL password.

Installing WordPress on Nginx - Screenshot 2

You’ll be asked to enter the password twice. And once you do that the process will continue to install the selected packages.

4. Starting Nginx Service

When the installation is done, we’ll get the control back in terminal. Now we’ll go ahead and start Nginx service. To do that, type the command given below –

service nginx start

This will start the Nginx service on Ubuntu. Below is a screenshot of terminal while activating the Nginx service.

Installing WordPress on Nginx - Screenshot 3

Now we’ll check if Nginx is really running or not. Open the web browser and browse to localhost. This should take you to Nginx default HTML page as shown below –

Installing WordPress on Nginx - Screenshot 4

5.Creating the Database

We log into MySQL to create a database for our WordPress installation. We can log into MySQL by typing in the command given below –

mysql -u root -p

Once we enter that command, we’ll be asked to enter the MySQL password which we have set previously. So just enter the password. (Note – It won’t be displayed as you enter it.

Installing WordPress on Nginx - Screenshot 5

After logging into MySQL, you can create the database using the following command –

create database blog;

We just name the database as blog and then exit MySQL using the command –

exit

Installing WordPress on Nginx - Screenshot 6

6. Downloading and Unzipping WordPress

Now its time for WordPress to come into picture. We’ll first switch over to the root directory where localhost points to.

cd /usr/share/nginx/html

Once we do that, we’ll download the WordPress package.

wget http://wordpress.org/latest.zip

This will download WordPress and you can see the download progress in the terminal as shown below –

Installing WordPress on Nginx - Screenshot 7

Once download is complete, just type in the following command to unzip the file.

unzip latest.zip

The terminal fills up with different file extraction details. Once files are unzipped just execute few more commands given below –

rm latest.zip

mv wordpress/* .

rmdir wordpress/

rm index.html

Note:  Make sure to enter the above commands as they are mentioned above, because during our testing, something similar to the below image took place in our office with just a little modification we happened to do with the above code 🙂

Computer Crash - Image Credit: http://www.byronbayblog.com.au/
Image Credit : BryonBayBlog.com.au

Explaining the above commands, In short: we have removed the latest.zip after extracting it. Then moved the files from WordPress folder to current directory. After moving the files, we have deleted WordPress directory and also we have deleted the useless default index.html page of Nginx.

This leaves us with fresh WordPress files within the localhost root directory.

7. Configuring Nginx

Type in the command given below to edit the default configuration file of Nginx.

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

This will open up the default configuration file. Now you’ll have to do some magical stuff here. Find the line which says –

index index.html index.htm;

And replace it with the line given below. This will help Nginx to work with PHP files.

index index.php;

Then find the following part –

Installing WordPress on Nginx - Screenshot 8

And make the following changes within that part of the file. This will help us to run Nginx with php5-fpm. We are using UNIX sockets instead of TCP ports to pass data between Nginx and PHP.

Installing WordPress on Nginx - Screenshot 9

Press Control + X and follow the instructions to save the changes to the file.

8. Change Ownership and Reload Nginx

We now change the ownership of HTML directory to www-data and reload Nginx. Setting the permissions to 777 is highly discouraged. You get errors in either Apache or your editor regarding permissions because Nginx runs under a different user (www-data) than you.

cd ..

chown www-data.www-data - R html/

service nginx reload

Installing WordPress on Nginx - Screenshot !

9. Installing WordPress

Finally we can start to install WordPress. Just visit localhost in your web browser to start the installation.  – You can ignore this if you are already familiar with the famous 5 minute installation.

  1. Visit localhost in Google Chrome or any other Web Browser

  2. WordPress will ask you to create Configuration File.

  3. Click on Create a Configuration File button

  4. Click lets go.

  5. Enter database name as blog

  6. Enter username as root

  7. Enter password as pass

  8. Keep host as localhost and table prefix as wp_

  9. Hit submit button

  10. Click run the install button

  11. Enter Site Title, Admin username and pass, Admin email

  12. Click Install WordPress button

  13. And we’re good to go.

We’ve successfully installed WordPress on Ubuntu using Nginx, PHP-FPM, MySQL. We want to know if you liked this post, and in our next post, maybe we would show how to optimize this even further by using Varnish Cache and Amazon S3.

Sumit Pore

Sumit Pore

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads