In this article, I am going to tell you that how can you install WordPress on Apache based Ubuntu server. I am going to show you a step by step guide on how you can perform this task. So you will have to go till the end with me. So here we go.

Initial requirements

There are some important stuff that you need to do on your server, before you go on with this guide.

In this article, I will be proceeding through the upcoming steps as a non-root user, and have sudo privileges. And you also need one, if you want to proceed. Moreover, you also need a LAMP (Linux, Apache, MySQL and PHP) stack on your VPS. You must have installed these components on your VPS. Once you have all of this, then you can proceed with the upcoming steps.

Step 1:

This is a preparatory step. In this step, you have to create User for WordPress and MySQL Database. A relational database management system is used by WordPress to manage and store the user information and website. As MySQL is already installed, so now we have to make a user for WordPress and a database. Now in order to get started, you have to log in to MySQL root (administrative) account. You can do so by using the following command:

mysql -u root -p

then you need to enter the password for MySQL root that you had setup before, when installing the software. Then a command prompt of MySQL will be given to you.

Now we have to create a separate database, which is controlled by WordPress. I will be naming this database as ‘wordpress’. You can create new database by entering this command:

CREATE DATABASE wordpress;

You must end every MySQL statement with a semi-colon (;). now to operate on our new database, we have to create a separate MySQL user account.

Now the account that I am going to make, I’ll call it ‘wordpressuser’ and I will set its password as ‘password’. You can change both of these things to whatever you want. Now to create a user, you need to type the follwing:

CREATE USER 'wordpressuser'@localhost IDENTIFIED BYpassword;

Now at this stage, you are having a database and a user account, and both of them are made for WordPress specifically. But these two things are not related yet, the database is not accessible by the user yet. Now you have to grant the access of the database to the user. You can do so by entering the following command:

GRANT ALL PRIVILEGES ON 'wordpress'.* TO 'wordpressuser'@localhost;

Now after this, the user will have the access to the database. The next thing we have to do is to flush the privileges. By doing this, the current MySQL instance will know the recent changes we have made in privileges.

FLUSH PRIVILEGES;

Now we can exit MySQL. Enter the following command:

exit

Now you should be back at your regular command prompt.

Step 2:

Now in this step, you are going to download WordPress. Now to get the up-to-date version of WordPress, enter the following command:

By doing this, a compressed file will be downloaded. It will contain the directory contents of the WordPress files, which will be archived. Now we need to rebuild the WordPress directory by extracting the files. You can do this by entering the following command:

tar xzvf latest.tar.gz

By doing this, a new directory ‘wordpress’ will be created in the home directory. We also need a few more packages that we need, while we are downloading stuff. After we update our local package index, we can get them form the default repositories of Ubuntu directly. Enter this code:

sudo apt-get update' sudo apt-get install php5-gd libssh2-php

By doing this, you can work with images, and by using your SSH login credentials, it will also allow you to update portions of your site and install plugins.

Step 3:

In this step, we will be configuring WordPress. Later on, we will be doing we will be doing configuration through web interface, most of the time. But first, we have to do some work. The first thing is that the WordPress directory that you just unpacked, you have to move into it:

cd ~/wordpress

by default, the sample configuration file is included, and it mostly matches all of our required configurations. But for WordPress to recognize that file, we have to copy it to the default configuration file location. Enter this command:

cp wp-config-sample.php wp-config.php

Now we have to open this file in a text editor:

nano wp-config.php

It will be already suitable according to our needs. But we just need to make some modifications to the parameters that are holding our database information. If we want the wordpress to correctly authenticate and connect to the database that we created, then we have to find the settings for DB_USER, DB_NAME and DB_PASSWORD.

You have to fill in the values of those parameters with the information for the database that you created. It will look like this:

Insert image a.

You just need to change only these values. Save and then close the file when you are finished.

Step 4:

Now we have configured our application, now we have to copy it into the document root of Apache, and there, it can be served to our website’s visitors.

The ‘rsync’ command is the most reliable and easiest way to transfer files from one directory to another. It has good data integrity features, and it preserves permissions. In Ubuntu 14.04, the location of document root is /var/www/html/. You have to enter the following command to transfer the WordPress files to that location:

sudo rsync -avP ~/wordpress/ /var/www/html/

All the contents of the directory that you unpacked will now be copied to the document root. Now we have to move into the document root, and make some final permission changes:

cd /var/www/html

To increase the security, you will have to change the ownership of the files.

To interact with your site, you have to give user ownership to the non-rooted and regular user (with sudo privileges). If you wish, this can be your regular user, but some people say that you should create an additional user for this purpose. But it is your choice that what you want to do.

Now in this article, I will be using the account that I set up while I initially set up the server. I will be calling this account ‘demo’. You can also use the account that you set up while you initially set up your server.

In order to allow Apache to interact with the content, we will have to give to our web server process the group ownership, and it is www-data. In order to assign these ownership values, you have to type the following:

sudo chown -R 'demo':www-data *

This command will set up the ownership properties that we are trying to look for. While we are working with ownerships and permissions, it will be better if we also assign correct ownerships on our uploads directory. By doing this, we will be allowed to upload contents and images to the website. At the current time, there are too much restrictions in the permissions.

Now the first thing is that, at your document root beneath the wp-content, you have to manually create the uploads directory. Now this will be the parent directory of your content, which is:

mkdir /var/www/html/wp-content/uploads

Now you have a directory to keep the uploaded files, but still, the permissions are very restrictive. To write to this directory, the web server itself needs our permissions. This can be done by assigning the group ownership of this directory to your web server. Enter the following command:

sudo chown -R :www-data /var/www/html/wp-content/uploads

by entering this command, the web server will be allowed to create directories and files under this category, and give us the permission to upload content to the server.

Step 5:

You can now complete the installation through web interface, because now you have configured your software and you have your files. The next thing is that you have to navigate to your public ip address or your server’s domain name in your web browser.

Now there will be an initial configuration page of WordPress in front of you, and there you have to create an initial administrator account.

Now you have to fill every information, and after that you have to click on the install button at the bottom. Now you will get a confirmation about the installation, and then you have to login with your newly created account.

Now after logging in, the WordPress interface will be presented to you.

Step 6:

This is an optional step. Dynamically, WordPress creates URLs that look like this by default:

So this is not most attractive or useful interface for search engines or users, so majority of the users like to modify it. So in this step, you will be creating pretty and good looking permalinks, which will attract more visitors and it will be in a human friendly format. Now to do this, you need to do a few things first, if you want it to work with Apache on Ubuntu 14.04.

Allowing URL rewrites by modifying Apache

The first thing is that to allow for .htaccess overrides, you need to modify the Apache virtual host file for WordPress. This can be done by editing the virtual host file. This is 000-default.conf by default, but if you created another configuration file, your file might be different.

sudo nano /etc/apache2/sites-available/000-default.conf

you have to set up a few things inside this file. You have to create a directory section where you allow overrides, and the ServerName should be set by you. You should now see a screen like this:

Save and then close the file when you are finished. Now the rewrite module should be enabled, it will allow you to modify the URLs. Enter the following command to do this:

sudo a2enmod rewrite

Now you have to restart Apache after these changes have been made.

sudo service apache2 restart

.htaccess file creation

Now you have to create an actual file because Apache is now configured to allow rewrites through .htaccess files. This file should be placed in your document root. To create an empty file, you have to enter the following command:

touch /var/www/html/.htaccess

With your user group and username, this will be created. But the web server should be the group owner, we have to adjust the ownership. Enter the following command to do this:

sudo chown :www-data /var/www/html/.htaccess

So now we have the correct ownership of this file.

But we might need to adjust the permissions. The necessary rewrite rules for you will be generated by WordPress. The rules will be implemented automatically if it has the write permissions to this file. But you will have to manually edit this file and add correct rules, if it does not have the permissions. Each configuration depends upon your preference for convenience over security. If you allow web server to write access to this file, then it will be convenient, but there may be some unnecessary security risk. If you want this file automatically updated by wordpress, you have to confirm that it has the correct permissions. Enter the following command to do this:

chmod 664 /var/www/html/.htaccess

this file can also be manually updated. Web server can be allowed to only read the privileges by you. Enter the following command to do so:

chmod 644 /var/www/html/.htaccess

Changing the permalink settings in WordPress

When the server side changes have been made by you, then the permalinks settings can be easily changed through the administration interface of WordPress.

First you have to click on the Settings menu, and then click on the Permalinks button.

To organize the URLs, you can choose any of the preconfigured settings, but you can create your own too.

Click on save changes to generate the rewrite rules after your selections have been made. You should be able to see a message like this if you allowed the web server to write access to your .htaccess file:

But if you had not allowed the web server, then you will have to manually add the provided rewrite rules to that file. Copy the lines given by wordpress and then edit that file on your server:

nano /var/www/html/.htaccess

Now on your Ubuntu 14.04 VPS, you will have a WordPress instance fully functioning. So this was it for today.