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 author(s)

Etel Sverdlov
Etel Sverdlov
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
50 Comments
Leave a comment...

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!

Hi, I followed all steps, but when i do sudo service nginx restart, warn conflicting server name xxx.xxx.xx.xx on 0.0.0.0:80 ignored nginx. I am not able to access wordpress by keying in IP address on my browser. Any ideas?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
August 15, 2012

This is because you have two server {} blocks that are both listening on the same IP and port.

In your case I’m guessing its either 0.0.0.0 and port 80 or you used the same servername twice which was your public facing IP.

If you remove one server block it will work.

Or if you want both active make sure they have different servernames and/or different ports.

After following these steps, I see “unable to connect to database” when viewing my WordPress site index. Could this be a permissions issue? (I installed WordPress not as root but as another super user.)

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
August 15, 2012

The error unable to connect to database indicates that a connection can not be established to the MySQL server which means it has not yet even had a chance to check if the provided credentials, user, pass, and db are valid.

The most common causes of this error are that MySQL is in fact not running, or there is a misconfiguration between the IP address you provided to wordpress to connect to and what IP/port MySQL is actually listening on.

By default MySQL will listen on all IPs and port 3306 so connecting to localhost and the default port in the wordpress configuration should be enough.

You can check to see if MySQL is runnning by either doing:

ps auxw | grep -i mysql

Or

telnet localhost 3306

The telnet command will attempt to connect to the IP and port that MySQL should be listening on. If the connection is established then its working, if it doesn’t then MySQL is most likely not started.

Thanks, raiyu. That’s been very helpful. I’ve rebooted the server and all’s well now. Very impressed with Digital Ocean and will be signing up.

I also had to tweak the permissions of /var/www in order to get WordPress automatic updates and plugin installs to work, using this line:

sudo chown -R www-data:www-data /var/www

Secondly, I’ve made this change to allow editing of files directly via SFTP if required:

sudo usermod -a -G www-data myusername sudo chmod -R g+rw /var/www

Are these worthwhile additions to the tutorial, or are there likely to be any problems caused by these changes?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
August 15, 2012

Both changes are more or less ok, in terms of security you want to avoid having too many directories that are writeable by the user that the web server runs as because wordpress has had exploits in the past which would allow users to upload files directly to your system.

Keeping wordpress running on the latest version avoids the majority of those issues, however it is still something to keep in mind.

I’m going to have Etel run through this tutorial and see about getting the specified changes added to allow for automatic updates and plugin installs - its a good suggestion and should be noted.

We do go back through the tutorials as we get comments on them to make updates, so keep them coming!

Thanks

The only other tweak I’d suggest is to change this line in the location{} block of the virtual host file:

try_files $uri $uri/ /index.html;

to this instead, which enables WordPress’ pretty permalinks under Nginx:

try_files $uri $uri/ /index.php?q=$uri&$args;

You then just need to set the Permalink Settings in the WordPress dashboard to use the “Custom Structure” (the last option), with the relevant tags, for example:

/%postname%/

Thank you for the suggestion!

The configuration has been changed to facilitate Permalinks. :)

At Step Six i have this error: Sudo: In: command not found

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 14, 2012

Is it possible that you typed in In with “i” as the first letter of the command?

The command is actually ln with “L” as the first letter.

Let me know if the issue continues.

Thank you Etel. That solution solved the problem (me). One sugestion: i’ve got the same error as zhangwl87 reported. The solution was remove the default nginx configuration because nginx detects two configs with the same domain name.

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 15, 2012

Thanks for the update! I’m glad that resolved the issue.

I went ahead and added a step to the tutorial to delete the default server block which conflicted with the wordpress one.

Very nice tutorial. But after going through all the steps mentioned, when I go to ‘localhost/wordpress’ the error i get is “File Not Found”. Please help!

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
September 20, 2012

You should be running it from a public IP/domain to make it accessible on the internet.

Localhost is a definition which allows a computer or server to access itself for testing.

If you are still having issues you can paste us the servername line from your nginx config.

I did everything thats written here but I am getting this error Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

any suggestions please

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
October 23, 2012

That error means that your PHP was installed without mysql support so you want to do:

apt-get install php5-mysql

Thanks for a great guide.

Just a question regarding step 4,

sudo usermod -a -G www-data username

Should the username not be in the sudoer list.

Thanks again

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
November 5, 2012

No— that user does not need to be in the sudoers file as they will not be running any sudo commands.

Hello, thank you for the information it was very helpful… I do have one issue I was hoping someone could help me with. When I go to my domain I am getting…

<?php /**

  • Front to the WordPress application. This file doesn’t do anything, but loads
  • wp-blog-header.php which does and tells WordPress to load the theme.
  • @package WordPress */

/**

  • Tells WordPress to load the WordPress theme and output it.
  • @var bool */ define(‘WP_USE_THEMES’, true);

/** Loads the WordPress Environment and Template */ require(‘./wp-blog-header.php’);

Any suggestions would be greatly appreciated…

Front to the wordpress application. This file doesn’t do anything but loads wp-blog-head.php which does and tells wordpress to load the theme.

Sorry tried to copy and paste up there, didnt work.

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 18, 2012

What is the error that you are getting? Perhaps it may be easier to show it through a screenshot.

Error: PHP is not running

Wordpress requires that your web server is running PHP. Your server does not have PHP installed, or PHP is turned off.

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 18, 2012

Make sure that your server block configuration file matches the one in the tutorial. I would imagine the most issues would arise with an error there.

Additionally, take care that you followed all of the steps in the preliminary LEMP tutorial here: https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04

Ok, I’ll check out my server block configuration file. I definitely followed all the steps in the LEMP tutorial. Thank You.

I was able to fix my server block config file, and I installed WordPress. I also used your tutorial to setup Nginx as a front end web server and apache at the back end, all went well. I can now get to my domain.com/wp-admin page but if I try to access just my domain.com I get an error from FireFox: “The page isn’t redirecting properly. Firefox has detected that server is redirecting the request for this address in a way that will never complete.”

Any insight to this?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
December 20, 2012

Wordpress works by redirecting all requests back to domain.com/index.php which does all of the processing of the homepage and blogs that you write.

It sounds like there is a misconfig there. Are you running wordpress straight through nginx or you also using a reverse proxy back to Apache?

Also if you paste your server { } and/or VirtualHost configs we could see if there’s anything that may be causing an issue there.

Sorry tried copying and pasting my config and puts an unhappy smily face lol Let me try a screenshot instead…

php

Front to the WordPress application. This file doesn’t do anything, but loads wp-blog-header.php which does and tells WordPress to load the theme.

@package WordPress

Tells WordPress to load the WordPress theme and output it.

@var bool

define(‘WP_USE_THEMES’, true);

Loads the WordPress Environment and Template require(‘./wp-blog-header.php’);

This is my virtual host config file for nginx…

server { listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www/; #the root is changed to the website directory.
    #Be sure to replace this with the appropriate extension.
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name sniffyourkid.com; #change the server name to reflect yours

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            try_files $uri $uri/ /index.php?q=$uri&$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            deny all;
    }



    location ~ \.php$ {
    #this block is responsible for processing php requests
    #these lines pass on the IP address of the site visitors to the backend server
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;


    #this line changes the default configuration which usually doesn’t usually
    #forward host header information
    proxy_set_header Host $host;

    #this command lets nginx the address of the proxied server.
    proxy_pass http://127.0.0.1:8080;

     }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
     location ~ /\.ht {
            deny all;
    }

}

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 20, 2012

We’ve cleaned up that article and configuration yesterday (https://www.digitalocean.com/community/articles/how-to-configure-nginx-as-a-front-end-proxy-for-apache). I would recommend using the new version that we have up. In the meantime, open up a ticket with us and we’ll take a look at the error.

Thank you.

ok thank you.

Hey, I followed Step 4 but when I try to add a plugin/theme in wordpress it always asks for my FTP information. I proceed to enter the information I use to login in to SSH and it does not work. Any ideas ?

When I visit my sites IP in the browser I am downloading the index.php instead of seeing it.

Any ideas where I went wrong?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
December 27, 2012

In regards to the FTP issue make sure that you are using a user other than root as root is usually restricted from logging in.

Also make sure that the user you are logging in as has permissions to write to the directory where plugins and themes are stored and that the user is allowed to navigate to that directory.

Alternatively you can upload themes directly through the web but you will need to set the plugins directory to permissions of 777 and then return them back to 755 after you have installed your themes and plugins as a security precaution.

As for the issue with downloading the index.php that happens because PHP isn’t being parsed/executed and instead it’s being treated as a regular file. Since it isn’t mime-typed to be interpreted anyway by the browser it ends up being downloaded so that would indicate an issue with the configuration/setup. In the above example fastcgi is used to parse PHP so you have to make sure that is install and processing in order to render PHP scripts.

We are going to update the documentation for that to make it clearer.

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
December 27, 2012

We setup the LEMP stack in another tutorial which would gets fastcgi setup to process your PHP files: https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04

Run through that first then this tutorial on installing and configuring wordpress for LEMP and you should be good to go.

I was using another user to log in and I also tried chmod to 777 but didn’t work.

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 27, 2012

You can try granting permissions to the nginx user:

From within the web directory, run the following commands, replacing username with the name of your specific user:

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

what part of that should i replace with my username?

Im getting “cannot access `www-data:www-data’: No such file or directory” after i type in the first command

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 28, 2012

There was a typo in the command that I have now corrected. The username in the command should be replaced with the user that you are logged in as.

OK I got it working. Quick question, when I type in "sudo chown www-data:www-data * -R " it says cannot access ‘*’: No such file or directory

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
December 28, 2012

It’s because of the order of the steps above. The directory was empty when you ran that command (because the step to copy the files came after); with the steps rearranged the command should complete without issue.

I was getting 403 Forbidden at the IP address after i followed the steps. When i thought that default dir /var/www doesnt have an index file, i changed the root to “root /var/www/wordpress;”. But still the same issue.

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 18, 2013

A 403 forbidden usually indicates a permissions problem so check the ownership of your /var/www and /var/www/wordpress directories to ensure that the user as which you have setup your nginx to run has access to read those directories.

“Your PHP installation appears to be missing the MySQL extension which is required by WordPress.”

At the end of the tutorial I have the same problem as ‘saad.18’ above.

I tried the suggestion from ‘raiyu’.

"That error means that your PHP was installed without mysql support so you want to do:

apt-get install php5-mysql" (I tried running this code on a clean LEMP and after the installation of wordpress tutorial.)

What’s next? :)

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 18, 2013

Thats correct you need to get php5-mysql installed as wel, after you did that did you get any new errors?

Okay! Last night I installed php5-mysql and after it still had the error.

Although I looked at it today and it works.

It must have taken a little time for some of the changes to come through.

Cheers

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 19, 2013

After you install php5-mysql you will need to restart nginx, that’s probably the step you were missing.

CAN’T select database ERROR :

I’m getting the following message on the wp-admin/install.php page :

We were able to connect to the database server (which means your username and password is okay) but not able to select the wordpress database.

Are you sure it exists? Does the user wordpressuser have permission to use the wordpress database? On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 30, 2013

Looks like an issue with granting permissions to the MySQL user to access the DB that was setup for wordpress.

Rerun this line in the MySQL command line and be sure to update it for your wordpress DB, wordpress User, and set a strong password, then try to connect:

GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY ‘password’;

Also you can test MySQL access by doing the following:

mysql -u wordpressuser -p

Enter your password and if it connects that means your User and Pass are in the MySQL DB.

Then try to switch to using your wordpress DB:

use wordpress_database;

If it connects then you are all set, if it doesn’t that means you grant statement above had an issue.

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.