Install Linux NGINX, PHP-FPM, MySQL (LEMP) & WordPress on Ubuntu

A LEMP stack is a group of open source software that is used to get webservers running. LEMP is different from your traditional LAMP (Linux Apache2, PHP, MySQL) stack as it uses NGINX (pronounced Engine X) and PHP-FPM which are both lightweight and optimized for performance without a lot of fine-tuning.

There is a lot of information out there on configuring WordPress with a lemp stack. Please note that not all information out there is good, and there is a lot of inherent security risks attributed with hosting your own websites. WP Jedi has a really good article on WordPress & Nginx Hosting Resources for those who want to learn more. There is also a really good repository on GitHub for Nginx WordPress Configurations.

Please be aware that this tutorial is for advanced users only. You must have a pretty good understanding of how Linux works as well as familiarity with general web server concepts. If you are looking for a simple way to host your website, please visit Who Is Hosting This for reviews of top web hosts.

Getting Started

There are a few requirements to take care of before beginning this tutorial. First, you must have a server that is running Ubuntu. I have tested this on the 12.04 LTS, 12.10, and 13.04 releases of Ubuntu, which are the three most recent releases. Next you must have ports 22 (for SSH/SFTP) and 80 (for web access) opened and SSH configured.

Step 1: Update

This tutorial will be using Ubuntu’s package manager apt-get to install all of your software. Running updates will allow you to use the latest version of the software you download, as well as any security patches. For any tasks requiring elevated privileges you must either prefix these commands with ‘sudo’ or run them as root. Run these commands:

# apt-get update
# apt-get dist-upgrade

Step 2: MySQL

Now that your operating system is up to date, it is time to install the server components. Let’s start with the database server, MySQL.

# apt-get install mysql-server

You’ll need to enter a root password. There aren’t any requirements, but I recommend making it as secure as you can.

Step 3: Nginx

Now that MySQL is set up, it’s time to install your web server to serve static files.

# apt-get install nginx

Now to get nginx up and running:

# service nginx start

Now to confirm that nginx is running:

# ifconfig eth0 | grep 'inet addr' | sed 's/.*inet addr:\([0-9\.]*\).*/\1/g'

Enter the resulting IP address into a web browser. If you see the “Welcome to nginx” screen, then it is working.

Step 4: PHP-FPM

Now it is time to install PHP so your server can serve dynamic files

# apt-get install php5-fpm

Step 5: Download WordPress

Now that we have our basic LEMP stack set up, it is time to download WordPress and set up its dependencies.

# cd /tmp
# wget
# tar xvf latest.tar.gz

Step 6: Set up MySQL

Now we need to set up a database and a MySQL user for WordPress to use. First we login as root:

# mysql -u root -p

Enter the previously set up password.

mysql> CREATE DATABASE wordpress;

Now we create the user:

mysql> CREATE USER username@localhost;

Next, we will allow this user to access our database:

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO username@localhost IDENTIFIED BY 'password';

Refresh MySQL:

mysql> quit

Configure WordPress:

# cp wordpress/wp-config-sample.php wordpress/wp-config.php
# nano wordpress/wp-config.php

Edit the wp-config.php to contain the following information:

// ** 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', 'username');
/** MySQL database password */
define('DB_PASSWORD', 'password');

Move the WordPress installation to the right place and fix permissions:

# mkdir -p /var/www
# cp -r wordpress/* /var/www
# cd /var/www
# chown -R www-data:www-data . 
# usermod -a -G www-data <your username>

Prepare the virtual host:

# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress
# nano /etc/nginx/sites-available/wordpress

Modify the contents of this file to contain the following:

server {
        listen   80;
        root /var/www;
        index index.php index.html index.htm;
        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 a UNIX socket
        location ~ \.php$ {
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

What was done:

  • Changed the server root to /var/www/
  • Added index.php to the index line.
  • Changed the server_name from localhost to your domain name or IP address (replace the in the configuration)
  • Change the “try_files $uri $uri/ /index.html;” line to “try_files $uri $uri/ /index.php?q=$uri&$args;” to enable WordPress Permalinks with nginx
  • Uncommented the correct lines in “location ~ \.php$ {“ section

Enable the vhost:

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

Remove the default vhost:

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

Install PHP MySQL

# apt-get install php5-mysql

Restart Nginx and PHP:

# service nginx restart
# service php5-fpm restart


Now if you go to the IP address of your server from any web browser you will now see a working version of WordPress! Now that you truly have complete control over your website, the possibilities are endless.

This is a guest post written by Zach Russell

Zach Russell is a Philadelphia based WordPress and SEO who runs ProTech Internet Group. @protechig

Zach Russell's Website

Submit a Guest Post

« Back to Blog