Tutorial

How To Install Wordpress with nginx on Ubuntu 12.04

Published on June 30, 2012
How To Install Wordpress with nginx on Ubuntu 12.04
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Wordpress

Wordpress is a free and open source website and blogging tool that uses php and MySQL. It was created in 2003 and has since then expanded to manage 22% of all the new websites created and has over 20,000 plugins to customize its functionality.

Step One—Prerequisites!

This tutorial covers installing Wordpress. Before you go through it, make sure your server is ready for Wordpress. You need root privileges (check out steps 3 and 4 for details): Initial Server Setup

You need to have nginx, MySQL, and PHP-FPM installed on your server: LEMP tutorial

Only once you have the user and required software should you proceed to install wordpress!

Step Two—Download WordPress

We can download Wordpress straight from their website:

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

This command will download the zipped wordpress package straight to your user's home directory. You can unzip it the the next line:

tar -xzvf latest.tar.gz 

Step Three—Create the WordPress Database and User

After we unzip the wordpress files, they will be in a directory called wordpress in the home directory on the virtual private server.

Now we need to switch gears for a moment and create a new MySQL directory for wordpress.

Go ahead and log into the MySQL Shell:

mysql -u root -p

Login using your MySQL root password, and then we need to create a wordpress database, a user in that database, and give that user a new password. Keep in mind that all MySQL commands must end with semi-colon.

First, let's make the database (I'm calling mine wordpress for simplicity's sake; feel free to give it whatever name you choose):

CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

Then we need to create the new user. You can replace the database, name, and password, with whatever you prefer:

CREATE USER wordpressuser@localhost;
Query OK, 0 rows affected (0.00 sec)

Set the password for your new user:

SET PASSWORD FOR wordpressuser@localhost= PASSWORD("password");
Query OK, 0 rows affected (0.00 sec)

Finish up by granting all privileges to the new user. Without this command, the wordpress installer will not be able to start up:

GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

Then refresh MySQL:

FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Exit out of the MySQL shell:

exit

Step Four—Setup the WordPress Configuration

The first step to is to copy the sample WordPress configuration file, located in the WordPress directory, into a new file which we will edit, creating a new usable WordPress config:

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

Then open the wordpress config:

sudo nano ~/wordpress/wp-config.php

Find the section that contains the field below and substitute in the correct name for your database, username, and password:

// ** 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', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

Save and Exit.

Step Five—Copy the Files

We are almost done uploading Wordpress to the server. We need to create the directory where we will keep the wordpress files:

sudo mkdir -p /var/www

Transfer the unzipped WordPress files to the website's root directory.

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

We can modify the permissions of /var/www to allow future automatic updating of Wordpress plugins and file editing with SFTP. If these steps aren't taken, you may get a "To perform the requested action, connection information is required" error message when attempting either task.

First, switch in to the web directory:

cd /var/www/

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 Six—Set Up Nginx Server Blocks

Now we need to set up the WordPress virtual host.

Create a new file for the for WordPress host, copying the format from the default configuration:

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

Open the WordPress virtual host:

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

The configuration should include the changes below (the details of the changes are under the config information):

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;
                 }
        

}

Here are the details of the changes:

  • 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 (replace the example.com 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
  • Uncomment the correct lines in “location ~ \.php$ {“ section

Save and Exit that file.

Step Seven—Activate the Server Block

Although all the configuration for worpress has been completed, we still need to activate the server block by creating a symbolic link:

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

Additionally, delete the default nginx server block.

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

Install php5-mysql:

sudo apt-get install php5-mysql

Then, as always, restart nginx and php-fpm:

sudo service nginx restart
sudo service php5-fpm restart

Step Eight—RESULTS: Access the WordPress Installation

Once that is all done, the wordpress online installation page is up and waiting for you:

Access the page by visiting your site's domain or IP address (eg. example.com/wp-admin/install.php) and fill out the short online form (it should look like this).

See More

Once Wordpress is installed, you have a strong base for building your site.

If you want to encrypt the information on your site, you can Install an SSL Certificate

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I am getting this error while making databse user ERROR 1819 (HY000): Your password does not satisfy the current policy requirements I am logged in using the password i set earlier.

I followed all steps but my css and javascript aren’t loading here is the console log

 Uncaught SyntaxError: Unexpected token <
install.php:114 Uncaught ReferenceError: jQuery is not defined

Thanks for the guide - works nicely. I ave a problem similar to one mentioned before with permalinks - for some reason Nginx is only showing the root and the admin panel - anything else gets the 404… Instaling nginx helper plugin did not resolve this

Thanks, Shai

I have followed Step 5 “to allow future automatic updating of Wordpress plugins” with

cd /var/www sudo chown www-data:www-data * -R sudo usermod -a -G www-data myusername

but I still get the “To perform the requested action, connection information is required” error message when I click the Update button from within Wordpress. I then tried

sudo chmod -R g+w *

to attempt to give myusername group write permission. But that failed. I temporarily had to

sudo chmod -R go+w

which finally allowed the automatic update to occur without error.

But I don’t want to expose the Wordpress files to be world writable just to permit automatic updating. What am I missing?

You failed to mention to install php5-gd. WordPress depends on either the GD library or imagemagick to do photo editing and manipulation. You should include this in the tutorial.

We can modify the permissions of /var/www to allow future automatic updating of Wordpress plugins and file editing with SFTP

This is exactly what I want. But does it have to be in /var/www/? My understanding is that for multi-site hosting on Debian, so presumably Ubuntu too, the default directory for web site files and directories is /srv/.

I would like to retain the ability to host non-WP sites on the same droplet, besides which any WP sites I host are likely to be so different as to make a multi-site WP installation impractical. Is this tutorial for single site droplets or do we simply treat WP sites differently?

So can I set a WP site up in the standard way, e.g. /srv/mydomain.tld/public/ where htdocs is the name of the WP installation and the permissions there will affect whether or not updates will be automatic?

Alternatively, can simlinks be used to create the same effect? i.e. put the site in /srv and simlink to it from /var/www

Thank you for sharing this information. I have a question. i purchased a theme but there is no style sheet. Now, there is a any way to fix this problem.

Hi,

I was trying to set-up 3 separate WordPress instances on a Single VPS (Not to get confused with MultiSite)

Each Instance will have their own domain name.

After following all the steps in this tutorial (I didn’t receive any error message) and successfully completed them, now when I try to navigate to my server i.e. http://128.199.143.37/

I see "Welcome to nginx ! " page, instead of WordPress Install Page.

Below are the steps which I have performed. Please do let me know what I have done wrong or is anything missing.

Appreciate all your help !

Thanks in Advance !

==================================================================================

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

==================================================================================

mysql -u root -p

CREATE DATABASE FirstSite; CREATE DATABASE SecondSite; CREATE DATABASE ThirdSite;

CREATE USER firstadmin@localhost; CREATE USER secondadmin@localhost; CREATE USER thirdadmin@localhost;

SET PASSWORD FOR firstadmin@localhost= PASSWORD(“password”); SET PASSWORD FOR secondadmin@localhost= PASSWORD(“password”); SET PASSWORD FOR thirdadmin@localhost= PASSWORD(“password”);

GRANT ALL PRIVILEGES ON FirstSite.* TO firstadmin@localhost IDENTIFIED BY ‘password’; GRANT ALL PRIVILEGES ON SecondSite.* TO secondadmin@localhost IDENTIFIED BY ‘password’; GRANT ALL PRIVILEGES ON ThirdSite.* TO thirdadmin@localhost IDENTIFIED BY ‘password’;

FLUSH PRIVILEGES;

exit

==================================================================================

sudo mkdir -p /var/www cd /var/www

sudo mkdir FirstSite sudo mkdir SecondSite sudo mkdir ThirdSite

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

sudo rsync -avP ~/wordpress/ /var/www/FirstSite/ sudo rsync -avP ~/wordpress/ /var/www/SecondSite/ sudo rsync -avP ~/wordpress/ /var/www/ThirdSite/

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

==================================================================================

cd /var/www/FirstSite

sudo nano wp-config.php

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

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

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

==================================================================================

cd /var/www/SecondSite

sudo nano wp-config.php

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

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

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

==================================================================================

cd /var/www/ThirdSite

sudo nano wp-config.php

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

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

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

==================================================================================

sudo cd /etc/nginx/sites-available

sudo cp default FirstSite sudo cp default SecondSite sudo cp default ThirdSite

edit each of the above file and change the following information as listed below

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 $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.

Save and Exit that file.


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

server { listen 80;

    root /var/www;
    index index.php index.html index.htm;

    server_name 128.199.143.37;

    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;
             }

}


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

server { listen 80;

    root /var/www;
    index index.php index.html index.htm;

    server_name 128.199.143.37;

    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;
             }

}


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

server { listen 80;

    root /var/www;
    index index.php index.html index.htm;

    server_name 128.199.143.37;

    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;
             }

}

==================================================================================

sudo ln -s /etc/nginx/sites-available/FirstSite /etc/nginx/sites-enabled/FirstSite sudo ln -s /etc/nginx/sites-available/SecondSite /etc/nginx/sites-enabled/SecondSite sudo ln -s /etc/nginx/sites-available/ThirdSite /etc/nginx/sites-enabled/ThirdSite

sudo mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak

sudo apt-get install php5-mysql

sudo service nginx restart

sudo service php5-fpm restart

==================================================================================

http://128.199.143.37

BACKGROUND So I’ve spent the better part of the past few weeks creating and destroying and creating and destroying droplets trying to follow DO’s LEMP tutorials in hopes that changing servers from a managed wordpress one to this one would get rid of a Bad Request error my client’s website has been getting.

THE PROBLEM I’ve gotten Wordpress installed on the new LEMP droplet according to the files list (ls) but trying to go to any wordpress pages give a 404. I went back and installed PHPMyAdmin and it shows that there’s a database called ‘wordpress’ and it says “No tables found in database”. Help?

I’ve tried restarting php5-fpm and nginx. Going to the IP address (which is what I’ve put instead of www.example.com in the config files) alone gives me the “Welcome to nginx” page. phpinfo finally works and gives me a bunch of info but I’m not sure how helpful that is.

Any help would be greatly appreciated! I really need to get this working… Thanks!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 29, 2014

@mattlucas: You need to run it as root:

sudo visudo

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.