Tutorial

How To Install WordPress with nginx on CentOS 6

Published on July 3, 2012
How To Install WordPress with nginx on CentOS 6
Not using CentOS 6?Choose a different version or distribution.
CentOS 6

Status: Deprecated

This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.

Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.

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.

Setup

The steps in this tutorial require the user to have root privileges on your virtual private server. You can see how to set that up here in steps 3 and 4.

Before working with wordpress, you need to have LEMP installed on your VPS. If you don't have the Linux, nginx, MySQL, PHP stack on your server, you can find the tutorial for setting it up here.

Once you have the user and required software, you can start installing wordpress!

Step One—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 Two—Create the WordPress Database and User

After we unzip the WordPress files, they will be in a directory called wordpress in the home directory.

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 Three—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 Four—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/wordpress

The final move that remains is to transfer the unzipped WordPress files to the website's root directory.

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

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 nginx:nginx * -R
sudo usermod -a -G nginx username

Step Five—Set Up Nginx Server Blocks

Now we need to set up the WordPress virtual host. Although Wordpress has an extra step in its installation, the nginx website gives us an easy configuration file:

Open up the default nginx default hosts file:

sudo vi /etc/nginx/conf.d/default.conf

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

#
# The default server
#
server {
    listen       80;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/wordpress;
        index index.php  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • Here are the details of the changes—you may have some of these in effect already:
  • Add index.php within the index line.
  • Change the root to /var/www/wordpress;
  • Uncomment the section beginning with "location ~ \.php$ {",
  • Change the root to access the actual document root, /var/www/wordpress;
  • Change the fastcgi_param line to help the PHP interpreter find the PHP script that we stored in the document root home.

Save, exit, and restart nginx for the changes to take effect:

sudo service nginx restart

Step Six—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 your Virtual Private Server's IP address (eg. example.com) 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?
 
42 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!

Don’t forge to service nginx restart

After getting Wp set up with this tutorial, I can’t update plugins. I tried to set up vsftpd but can’t get this to work. So many conflicting instructions out there. Can you post a quick addition to get us past this part?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 16, 2013

What error are you getting when you attempt to update plugins and what method are you using for installing/updating plugins?

For Cent OS, Check out, http://centminmod.com/

This guide lacks informationabout how to set up folder permissions…

what about emails? with this config, no email notifications will work

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
May 12, 2013

@Binoy Install a mailserver such as postfix or sendmail, it should fix emails.

This is down right incomplete:

  1. You need to give the right writing permissions to the wp-content/ directory.

  2. For updates, I suggest that you use WP-CLI

  3. You need to add a rewrite rule in your to make WordPress support smartpermalinks.

I too think this needs to rewritten with other features like permissions, ownership etc. Plus copy paste this with required changes for LAMP.

Hahah the last step they should have put was to restart Nginx. That would have saved me 10 minutes figuring out what I did wrong :-)

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 18, 2013

@JustGiveMeTheDamnManual I’ve updated the article to include that at the bottom :] Thanks.

after manual installation

I see

http://mydomain/wp-admin/install.php?step=2

404 Not Found nginx

I try make install from this tutorial but after can`t login with this defaults configs for testing :( Always see incorrect password dialog

http://mydomain/wp-login.php

define(‘DB_NAME’, ‘wordpress’);

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

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

/** MySQL hostname */ define(‘DB_HOST’, ‘localhost’);

Anybody pls share worked nginx config where wordpress worked with permalinks .

Thanks !

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 6, 2013

@amnetw are you able to complete the installation process? Do you see any errors?

Thanks for you replay Kamal Nasser . Yes I do all steps from this manual . Will be great if anybody share worked nginx config where wordpress work with rewrite rules with seo friendly links . I use nginx + php-fpm (fastcgi_pass unix:/tmp/php-fpm.sock; ) without httpd .

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 7, 2013

@amnetw: Enable the nginx module from http://codex.wordpress.org/Nginx#URL_Rewrites_.2F_Permalinks

And add this before “location ~ .php$ {” in your nginx virtualhost config:

location / { try_files $uri $uri/ /index.php$is_args$args; }

Restart nginx and hopefully permalinks will work properly.

Thanks for permalinks nginx sample . You php-fpm config ( www.conf ) work with user = nginx group = nginx ?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 8, 2013

@amnetw yes, but make sure nginx can write to wp-uploads and other directories that need to be writable.

WORKED !!! chmod -R 755 /usr/share/nginx/html/mysite + chown -R nginx:nginx /usr/share/nginx/html/ + where my sites :) I am happy :) SEO Friendly permalinks work ! BIG THANKS Kamal Nasser !

Permissions has been mentioned a couple of times but is there a guide or instructions for how to set permissions correctly, not just for NGINX but Apache too?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 2, 2013

@Tyssen: It depends on your app. If it’s wordpress, it’s usually the same for nginx and apache. Just chown www-data the directories you need to write to (usually wp-content).

You can also use WP-CLI command line tool to install, configure, and update wordpress and it’s plugins i.e. http://centminmod.com/addons.html#wpcli and Nginx and WP-CLI setup http://centminmod.com/nginx_configure_wordpress_ffpc_plugin.html

when i enter my ftp info into wordpress i get this message Unable to locate WordPress Content directory (wp-content).

how do i set permissions for my ftp user

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2013

@edgarlambarena: You shouldn’t need to enter your ftp details into wordpress – where does it ask you for them?

I have tried this tutorial several times and am having a problem. my nginx installs correctly but the php does not seem to be working on info.php. I just see a blank page, blank white. I think php is not passing to the folder.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
November 10, 2013

@mcpadden.hunter: Try restarting nginx and restarting php-fpm, does that fix it?

Looks like this article is missing a section, when compared to the tail of <b>Step Five</b> of <a href=“https://www.digitalocean.com/community/articles/how-to-install-wordpress-with-nginx-on-ubuntu-12-04”>How To Install Wordpress with nginx on Ubuntu 12.04</a>:<blockquote>We can modify the permissions of <code>/var/www</code> 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.<br/><br/>First, switch in to the web directory:<br/><pre>cd /var/www/</pre>Give ownership of the directory to the nginx user, replacing the “username” with the name of your server user.<br/><pre>sudo chown www-data:www-data * -R<br/>sudo usermod -a -G www-data username</pre></blockquote>

Justin Ellingwood
DigitalOcean Employee
DigitalOcean Employee badge
November 13, 2013

Thanks Pablo. It should be fixed now.

Hey Pablo,

I’m running Apache on CentOS 6 and I tried using the command you listed but I’m getting this result:

chown: invalid user: ‘www-data:www-data’

I tried switching out www-data with my superuser but that still prompts me with “To perform the requested action…”

Any ideas?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
November 18, 2013

@Alex: On CentOS, I believe the user is called nginx instead of www-data. Try running these commands instead: <pre>sudo chown nginx:nginx * -R sudo usermod -a -G nginx username</pre>

@Kamal should update your document to reflect chown nginx:nginx since this is a CENTOS DOCUMENT. Could of saved me time.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
November 22, 2013

@adamsebolka: Thanks, updated.

Also - For the Nginx permalink issue - tried many things… all I had to do was add
try_files $uri $uri/ /index.php$is_args$args;

to the location / area so it reflects - location / { root /var/www/wordpress; index index.php index.html index.htm; try_files $uri $uri/ /index.php$is_args$args; }

Shouldn’t SET PASSWORD FOR wordpressuser@localhost= PASSWORD(“password”); be SET PASSWORD FOR wordpressuser@localhost= PASSWORD(‘password’); ?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
December 4, 2013

@sunridgewoods: Both should work. :]

If anyone on the web is reading this I ask you for the sake of humanity please create a proper wordpress tutorial with how to set up users and permissions

You can follow the above tutorial and you can’t install any plugins from the backend of wordpress which is very convenient

I shouldn’t have to chmod 777 everything just to get wordpress to work, I know it’s my own fault I’m not an advanced user with linux but if someone could guide us that would be great!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
December 5, 2013

@SaM5246: Try running these commands: <pre>sudo usermod -a -G www-data myusername sudo chmod -R g+rw /var/www sudo chown -R myusername:myusername /var/www</pre>

Hi Kamal,

I received this error:

"Can’t select database We were able to connect to the database server (which means your username and password is okay) but not able to select the wp******* database.

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

Any idea?

Fixed, my mistake.

Just my 0.5 cents: If you rename the database name and user name examples, I believe it will be more simple to understand:

CREATE DATABASE YourDataBaseName; 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 YourDataBaseUserName@localhost; Query OK, 0 rows affected (0.00 sec)

Set the password for your new user: SET PASSWORD FOR YourDataBaseUserName@localhost= PASSWORD(“YourDataBaseUserNamePassword”); 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 YourDataBaseName.* TO YourDataBaseUserName@localhost IDENTIFIED BY ‘YourDataBaseUserNamePassword’; Query OK, 0 rows affected (0.00 sec)

Thanks.

Just my 0.5 cents to solve issues when trying to crop images:

Error messages: “Image could not be processed. Please go back and try again.” “Crop error…”

Solution: yum --enablerepo=remi install php-gd.i686 yum --enablerepo=remi update

service php-fpm restart

Best regards, Tiago Lima

After Doing All that… While I trying to access, It shows a message Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

Good . Thank you.

You never covered where to download nginx. This tutorial is missing a lot of pieces and is incomplete.

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.