How to install WordPress with nginx on Debian Wheezy

Install WordPress

WordPress is the most popular Content Management System (CMS) on the planet. Over 20% of websites, is powered by WordPress. It is free and open-source. WordPress is built with PHP as a server-side language and MySql as a database.

Prerequisites for WordPress Installation

To get started with installing WordPress you’ll need to make sure that your server is ready for it.
You’ll need a Debian based server with root privileges and make sure you’ve Nginx, PHP and MySql installed. If you don’t have these things installed, learn how to install nginx, PHP & MySql.

Download WordPress:

To download WordPress write the following command on your terminal:

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

Unzip the downloaded file with the following command:

tar -xzvf latest.tar.gz

This will unzip the WordPress files and you’ll get all of these files in a directory called WordPress on your home directory.

Create WordPress Database and User

As WordPress will need a MySql database, you’ll need to create one.
Let’s login to MySql now by executing the following command:

mysql -u root -p

Provide your password and you’re logged in!

Create a database for WordPress:

CREATE DATABASE yourdbname;

Replace “yourdbname” with your desired database name.

Now let’s create a user for the above database:

CREATE USER youruser@localhost;

Replace “youruser” with your preferred username.

Set password for the above user:

SET PASSWORD FOR youruser @localhost= PASSWORD("password");

Replace “youruser” & “password” with your username and password.

Now grant permission to the user for this database:

GRANT ALL PRIVILEGES ON yourdbname.* TO youruser@localhost IDENTIFIED BY 'password';

Refresh MySql:

FLUSH PRIVILEGES;

We’re done with MySql, let’s exit:

exit;

Setup the WordPress Configuration:

Create a copy of the wp-config-sample.php file as wp-config.php. This file will contain all of your configurations:

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

Now open wp-config.php file:

sudo nano ~/wordpress/wp-config.php

Find the following lines of codes and replace with your own database information:

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

/** MySQL database username */
define('DB_USER', 'youruser');

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

Now, save the file and exit.

Move the files

Let’s create a directory where all of your WordPress files will be located. Replace example.com with your domain name:

sudo mkdir -p /var/www/example.com

Move the WordPress files to WordPress root directory:

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

Give ownership to nginx user, replace username with your server username:

sudo chown -R www-data:www-data /var/www/example.com
sudo usermod -a -G www-data username

Set up nginx server blocks:

Now, you’ll need to set up your Nginx server blocks (virtual host). Create a new file for the WordPress host, copying the format from the default configuration. Replace example.com with your domain name:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Open up the newly created file:

sudo nano /etc/nginx/sites-available/example.com

Add the following codes on that file, replace example.com with your domain:

server {
  listen 80;

  root /var/www/example.com;
  index index.php;

  server_name example.com www.example.com;

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

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ .php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

Save the file and exit.

Create Symbolic link for server block:

Create symbolic link for server block with the following command:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Remove the default server block:

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

Restart nginx:

service nginx restart

WordPress Installation:

Now, access your WordPress online installation page by visiting this page: example.com/wp-admin/install.php

Provide the information that the page is asking & you’re done!

Have a happy blogging!