Contents
Need an expert to help you with
" How To Install WordPress with Nginx on Ubuntu 14.04" ?

Get Help

How To Install WordPress with Nginx on Ubuntu 14.04

How To Install WordPress with Nginx on Ubuntu 14.04
How To Install WordPress with Nginx on Ubuntu 14.04

Tutorial Introduction

WordPress is a very popular CMS (content management system) which grants you the ability to easily get your site or blog up and running. Once you have installed WordPress, you can manage just about anything in an easy web interface. In this tutorial we will teach you how to install WordPress on your Ubuntu 14.04 server. We’ll be using nginx for the web server component as it is quite powerful and efficient making it one of the most popular web server components due to its performance capabilities.

Requirements

Before starting with the tutorial, there are a couple things that you are required to have.
  1. You’ll require a non-root user with sudo privileges.
  2. You’re also going to need a LEMP (Linux operating system, Nginx web server, MySQL database, and PHP processing) stack installed and configured on your server.
Once you have the requirements, you may proceed with the tutorial.

Create a MySQL Database and User for WordPress

Get started with WordPress by first preparing the database. You have got the MySQL database software installed, but you have not made a database for our WordPress information. You must create an account which WordPress will use in order to access the database you will be making. Start by logging into an interactive session using your MySQL administrative account by using the following command below.
mysql -u root -p
You are going to be asked for the password you have chosen for the MySQL root account after you installed the software. You should be receiving a MySQL command prompt. Then, you will create a separate database which is going to be used only by your WordPress application. The name should not matter, just make sure it is descriptive so that you can recognize it easily. In this tutorial, we are going to call our database ‘wordpress’.
CREATE DATABASE wordpress;
Keep in mind that the semi-colon (;) ends the MySQL statement and every MySQL statement has to end with one, so make sure of that in case you ever run into any issues. Now you’ve got a database, you can create a user account. Afterwards you will hand over control of the database to this new user so that your application may interact with the database. That system of creating a separate database and user for every application helps ensure your data is separate from other data being stored by MySQL, this is great for security and data management. For this tutorial, we are using ‘wordpressuser’ as an account name. We will give it a password of ‘password’ to authenticate with. Once you’ve set up your own configuration, you will need a more secure password.
CREATE USER [email protected] IDENTIFIED BY 'password';
Now that you have a database and a user, you can now establish a relationship between the two. Start by telling MySQL that your new user is allowed to access and control the database. You can do this using the following command.
GRANT ALL PRIVILEGES ON wordpress.* TO [email protected];
Now everything should be configured and ready. We will also have to flush the privileges to disk so that our current instance of MySQL knows about the privilege changes we’ve done:
FLUSH PRIVILEGES;
Next, quit the MySQL prompt.
exit
You will now be back in your command prompt and ready to proceed.

Download WordPress to your Server

Next, you will want to download the actual WordPress content onto your server. This will be available on the WordPress website. The latest stable version of the application is always provided with the same URL, which should make this part easy. You should download the file to our user’s home directory.
cd ~

wget http://wordpress.org/latest.tar.gz
The application files are usually downloaded as a compressed, archived directory structure stored in a file named ‘latest.tar.gz’. You may extract the contents by executing the following.
tar xzvf latest.tar.gz
This will make a directory named ‘wordpress’ which contains the site files. You will want to take this chance to download some more components that your WordPress instance is going to require. You can get those directly from Ubuntu’s software repositories using apt.
sudo apt-get update
sudo apt-get install php5-gd libssh2-php
Those two packages will grant you the ability to work with images and be able to install/update plugins and components using SSH respectively.

Configure WordPress

Since you now have the files, you may start configuring your WordPress instance. You have to edit the main configuration file located in your new directory. Go into the directory which you extracted to in the last step.
cd ~/wordpress
Within this directory you have a sample configuration file named ‘wp-config-sample.php’ which will have most of the configuration details right. You may copy that to use as the base of your config file.
cp wp-config-sample.php wp-config.php
Once you have opened the file, the first thing you will want to do is adjust some secret keys to provide some security for your installation. WordPress gives a secure generator for those values, so you won’t have to try and come up with good values on your own and they should only be used internally, so it isn’t going to hurt usability to have complex and secure values here. To get secure values from the WordPress secret key generator, use the below.
curl -s https://api.wordpress.org/secret-key/1.1/salt/
You will receive unique values that look like the below.
Output

define('AUTH_KEY',         '1jl/vqfs<XhdXoAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<[email protected]');

define('SECURE_AUTH_KEY',  'E2N-h2]Dcvp+aS/p7X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');

define('LOGGED_IN_KEY',    'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');

define('NONCE_KEY',        'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');

define('AUTH_SALT',        'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES  07VC*Lj*lD&?3w!BT#-');

define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');

define('LOGGED_IN_SALT',   'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');

define('NONCE_SALT',       'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');
You may now paste these configuration lines directly into your configuration file to set secure keys. Copy the output you have received. Next, click the WordPress configuration file.
nano wp-config.php
Look for the section which contains the dummy values for these settings. It should look like the below.
. . .



define('AUTH_KEY',         'put your unique phrase here');

define('SECURE_AUTH_KEY',  'put your unique phrase here');

define('LOGGED_IN_KEY',    'put your unique phrase here');

define('NONCE_KEY',        'put your unique phrase here');

define('AUTH_SALT',        'put your unique phrase here');

define('SECURE_AUTH_SALT', 'put your unique phrase here');

define('LOGGED_IN_SALT',   'put your unique phrase here');

define('NONCE_SALT',       'put your unique phrase here');



. . .
Remove these lines and paste in the values you have copied from the command line:
. . .



define('AUTH_KEY',         'VALUES COPIED FROM THE COMMAND LINE');

define('SECURE_AUTH_KEY',  'VALUES COPIED FROM THE COMMAND LINE');

define('LOGGED_IN_KEY',    'VALUES COPIED FROM THE COMMAND LINE');

define('NONCE_KEY',        'VALUES COPIED FROM THE COMMAND LINE');

define('AUTH_SALT',        'VALUES COPIED FROM THE COMMAND LINE');

define('SECURE_AUTH_SALT', 'VALUES COPIED FROM THE COMMAND LINE');

define('LOGGED_IN_SALT',   'VALUES COPIED FROM THE COMMAND LINE');

define('NONCE_SALT',       'VALUES COPIED FROM THE COMMAND LINE');



. . .
The file is now suitable for your needs, all it requires now is the information to connect to the database you have made. The parameters you have to set are ‘DB_NAME, DB_USER’ and ‘DB_PASSWORD’. You can find those parameters in the following file and set them up to use the database and user details that you have made. Below is an example of what we have in the file.
. . .

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

. . .
After you have made the changes above, save and exit the file.

Copy the Files to the Document Root

Now that you have done the changes to the config files, the next thing you need to do is copy them over to your document root so that your web server will find them and serve them. You are going to use the rsync utility to do the transfer; it will have the advantage of preserving permissions, ownership, and ensuring data integrity. The location of the default document root of nginx on Ubuntu14.04 is located in ‘/usr/share/nginx/html/‘. However, you will be setting up your document root in ‘/var/www/html/‘ to avoid editing a directory location which is controlled by the nginx package. You are going to change this in your nginx configuration a little later. You may create the new document root directory by using the below.
sudo mkdir -p /var/www/html
Next, you may copy the files to this location by using the next command.
sudo rsync -avP ~/wordpress/ /var/www/html/
This should recursively copy the contents of your ‘~/wordpress’ directory into your document root. You will now move over to the document root so that you can adjust certain permissions.
cd /var/www/html/
The problem with the directory structure as it stands currently is that all of Ur files have user and group ownership attached to your regular user. This is OK, except that your web server has to be able to edit some directories and files. You can grant this permission without exposing too much of your system by providing the group that your web server runs under group ownership of the files. Afterwards you may open up group permissions slightly as required. The group which nginx operates under is ‘www-data’. As for the user portion, type your user account name. We are using an account named ‘userdemo’ for our example here.
sudo chown -R userdemo:www-data /var/www/html/*
This should grant your files the required ownership. Before moving on, you must create a new directory for user uploads.
mkdir wp-content/uploads
The new directory will have group writing set already, however the new directory is not assigned with ‘www-data’ group ownership yet. Change that now.
sudo chown -R :www-data /var/www/html/wp-content/uploads

Modify Nginx Server Blocks

Now that your files and directories are configured, proceed to modifying the ‘nginx’ configuration so that it serves the content correctly. You can use the default nginx server block as a base for your new server block. Copy it over exactly like the below command.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress
Open the new file you have created in order to make a couple changes to it.
sudo nano /etc/nginx/sites-available/wordpress
Perform the next changes.
server {

        listen 80 default_server;

        listen [::]:80 default_server ipv6only=on;



        root /var/www/html;

        index index.php index.html index.htm;



        server_name your_domain.com;



        location / {

                # try_files $uri $uri/ =404;

                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/html;

        }



        location ~ \.php$ {

                try_files $uri =404;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;

                fastcgi_pass unix:/var/run/php5-fpm.sock;

                fastcgi_index index.php;

                include fastcgi_params;

        }

}
A summary of things you will need to change:
  1. Modify the value of the root directive to point to your new document root at ‘/var/www/html’.
  2. Edit the index parameter so that it searches for an ‘index.php’ file before any other files
  3. Modify the value of the ‘server_name’ directive so that it points to your server’s domain name or IP address.
  4. Change the ‘try_files’ inside the location / block so that it sends requests to PHP when they are not an exact match.
A couple of these might already be set because of your LEMP installation. Once you are finished with those changes, save and exit the file. You will have to link your new file to the sites-enabled directory in order to enable it. You can do it like this.
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
The file you have just linked conflicts with your old default file as it borrowed a lot from it. You must disable the old file.
sudo rm /etc/nginx/sites-enabled/default
Next, restart the web server and the PHP processor for the changes to take effect.
sudo service nginx restart

sudo service php5-fpm restart

Complete the Installation through the Web Interface

Since your WordPress is ready to go, you may finish the installation from your chosen web browser. Point your browser to your server’s domain name or IP address, like in the example below. http://example_domain.com If this shows your old default nginx page, you may want to refresh the page without the cache. You should see the basic WordPress welcome stage. Select your options (Site name, username, password, and email)  and then click the install WordPress button. Next, you will need to login with the account which you have just made. Then you will be presented with your WordPress dashboard through which you can start customizing your setup and creating content.

Conclusion

You will now have your WordPress instance up and running on an Nginx web server in Ubuntu 14.04. WordPress is a very flexible platform which you could use to customize your site. You can test this with several plugins, themes, and more, to find out which one works best for you.
Updated on 01 Jan 2018
0 Comments
Add a comment
Login for Comment