[Tutorial #5] Bring WordPress Live

[Tutorial #5] Bring WordPress Live

Step by Step Instructions

  1. wget https://wordpress.org/latest.zip
  2. sudo mkdir /var/www/wordpress
  3. Move zip file into /var/www/wordpress and extract.
  4. cd /etc/apache2/sites-available/ 
  5. sudo cp 000-default.conf wordpress.conf
  6. Modify wordpress.conf so apache2 knows to forward any request or your domain to the wordpress app folder.
  7. sudo a2ensite wordpress.conf
  8. sudo service apache2 restart
  9. Go to PhpMyAdmin via your browser by youdomain.com/phpmyadmin .
  10. Create a new user “wordpress” and check to create the corresponding database.
  11. cd /var/www/wordpress
  12. sudo cp wp-config-example.php wp-config.php
  13. Modify config file to add your MySQL wordpress database information.
  14. sudo chown -R www-data:www-data /var/www/wordpress
  15. sudo chmod -R 755 /var/www/wordpress/
  16. Open browser and type in your domain. You should see wordpress installation page. Follow the instruction and you are done!

Introduction

In last tutorial we learned how to setup the LAMP (Linux, Apache2, MySQL, and PHP) stack. Now it’s time to configure them specifically for wordpress and bring it live. The main aspects of configuration are:

  1. Put wordpress files in a proper web folder.
  2. Edit and enable Apache2 config file to associate your domain to the wordpress files folder.
  3. Setup a wordpress database in MySQL.
  4. Create and change wordpress config file so it can connect to its database in MySQL.
  5. Change files’ owner and permissions so we can change wordpress from its online admin panel.
  6. Install wordpress via browser.

Before we proceed and discuss the details, I want to point out that some of the steps above apply to ANY web application setup:

  • Steps #1,2,5 are generic for any web applications and for other web server besides Apache2.
  • Steps #3 and #4 are generic for many web applications that use a database, even if it is not using MySQL.

Thus, learning and understanding these steps gives you benefits beyond working with wordpress.

Put wordpress files at a proper web folder

The first step of creating a web application is to put the necessary files in a specific folder, so the web server can find it.

Get wordpress:

You can get wordpress from its official website:

2016-09-05 12.55.38

WordPress download page as of 2016/09/05.

After downloading wordpress you can transfer the zip file from local machine to your server via scp (secured copy):

#> scp latest.zip your_user_name@your_domain_or_ip: 

This will copy the wordpress file latest.zip to your server’s user home directory.

If you login to the server via command line you can get wordpress directly by:

#> wget https://wordpress.org/latest.zip

Create a web app folder:

Next, create a folder to store wordpress’ extracted files. The conventional place on Linux is /var/www . Do:

#> sudo mkdir /var/www/wordpress

Then extract wordpress files in that folder. Of course you can use other name for your folder. In linux the command to extract a zip file is unzip :

sudo unzip latest.zip

Note:

  • If linux complained that unzip cannot be found, install it by sudo apt-get install unzip
  • The files might be contained in a wordpress folder (/var/www/wordpress/wordpress). In this case just move the entire files from it into /var/www/wordpress and remove the folder  /var/www/wordpress/wordpress .

Configure Apache2

In linux many software is configured by editing a configuration file. For Apache2 it is no exception.

Apache2’s config file folder

The default apache2 config file folder is /etc/apache2 . In general the folder /etc is where linux stores software’s configuration files.

The sub-folders sites-available and sites-enabled are of interest for adding new web applications. The process is:

  1. Add a new site config file in sites-available. Assume it is called newsite.conf .
  2. Do the command to enable the new site: sudo a2ensite newsite.conf .You should see now newsite.conf also appears in the folder sites-enabled.
  3. Do the command to restart apache2 and apply the changes: sudo service apache2 restart 

Add a new site config file in sites-available

In the folder sites-available there is a default config file 000-default.conf. We can generate our wordpress config file by simply copy it:

sudo cp 000-default.conf wordpress.conf

We now have to edit wordpress.conf so apache2 knows to forward requests to the right web app folder location. Open wordpress.conf and you should see this:

Screenshot_2016-09-05-13-19-59-1

At the place ServerAdmin we are going to edit it and change to:

ServerName your-domain.com

ServerAdmin your-email

ServerAlias /var/www/wordpress   

Here are the explanation:

  • ServerName tells apache2 to forward any HTTP requests that begin with your-domain.com to the web application.
  • ServerAdmin just represents you the admin by your email.
  • DocumentRoot tells apache2 where your web app folder is.

Enable new config file

Once the new file is created and edited, the next step is to enable it so the new changes apply to apache2. Simply do this:

#> sudo a2ensite wordpress.conf

#> sudo service apache2 restart

The first one “publishes” the config file so apache2 will take it. The second one is to restart apache2 to make sure it takes into the new config file.

Ensure apache2 can change wordpress (later)

Since wordpress has an admin panel that enables wordpress admins (you) to change wordpress, we must give the wordpress file folder proper ownership and permission to do so.

As we still need to create a wordpress config file for the database, we delay this step later.

Setup a wordpress database in MySQL

Now we configure MySQL so wordpress can (1) have a database to store/retrieve data (2) can access the database. Thanks to MyPhpAdmin it’s very easy to do so for wordpress.  The process is:

  • Login to MyPhpAdmin.
  • Create a new user wordpress with associated database wordpress.
  • Write down the created user password.

First log in from the url your-domain.com/phpmyadmin . You should see the top headers as follows:

Screenshot_2016-09-05-13-49-09-1

Click on the “Users” tab. Then click on “Add User” tab below. The Add User page looks like this:

2016-09-05 14.40.10

Input username (eg: wordpress), host as “localhost”, and find a good password (eg: from Norton password generator). Then in below check “Create database with same name and grant all privileges”. All these will:

  • Create a database called “wordpress”.
  • Create a user called “wordpress”.
  • The user can read/write/create tables in the “wordpress” database but not others.

Create and change wordpress config file so it can connect to its database in MySQL

Now we go to our web app folder /var/www/wordpress. You should see a file called wp-config-sample.php . This is the template file for your wordpress config file wp-config.php .  Copy this file and rename it to wp-config.php :

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

Open the config file. Find these lines and enter the corresponding values from your MySQL settings:

define('DB_NAME', 'wordpress');

define('DB_USER', 'wordpress');

define('DB_PASSWORD', 'yourpassword');

define('DB_HOST', 'localhost');

Now the wordpress is ready to connect to your MySQL database!

Change files’ owner and permissions so we can change wordpress from its online admin panel

As mentioned before, wordpress’ files must have proper ownership and permission settings so we can configure wordpress from the web admin panel. To do so, simply execute the two command:

#>sudo chown -R www-data:www-data /var/www/wordpress/*

#>sudo chmod -R  755  /var/www/wordpress/*

The first command changes the owner to www-data and group to www-data. www-data is the user/group of web server apache2.

The second command is harder to understand. See chmod online page for more information.

Install WordPress

Now everything is ready except to install wordpress from the browser. Open the browser and type your domain, and you should see the wordpress installation page.

Screenshot_2016-09-09-12-59-43-1

 

Screenshot_2016-09-09-13-00-41-1

Simply follow the instructions and press “install”. If you did all the previous steps correctly you should have wordpress installed within seconds!

2016-09-09 13.10.13

Screenshot_2016-09-09-13-01-29-1

Screenshot_2016-09-09-13-01-47-1

WordPress’s admin panel.