Overview

This article will walk you through building a website with Linux, Apache, MySQL and PHP i.e. the LAMP stack, and install WordPress for content management.

Concept

A bit of concept first.

Computer and Server

A computer is anything that computes and that includes desktop, laptop, mobile devices and server.

A server is a computer that is intended to provide services. A web server is one that provides web service e.g. hosting a website.

The 3 major ratings of a computer/server are

  1. CPU
  2. RAM or working memory
  3. Storage capacity

And in the case of a web server, there is one more consideration, that is transfer capacity.

Internet, Internet Protocol (IP) address and domain name

Internet is a group of computers/servers connected together.

Public IP address e.g. 162.243.139.49 is an unique identifier of each publicly accessible computer/server, like a geographic coordinate which represents a unique location on earth.

Domain name e.g. kurtcms.org is a human-friendly version of an IP address. It translates to an IP address. It is like a street address or a PO box.

Internet Corporation for Assigned Names and Numbers (ICANN) manages and maintains IP addresses and domain names.

Linux, Apache, MySQL and PHP (LAMP)

Linux

Linux is an operating system commonly used for server. Linux to server is like Windows and Mac OS to personal computer.

A bit unlike Windows and Mac OS, Linux comes in different flavours. Different Linux flavours are called distributions or distro for short.

A Linux distro comprises a collection of tools that help perform tasks. Different distros may come with different tools for different tasks.

Some of the most common Linux distro are Ubuntu, Debian, Red Hat and Fedora.

Ubuntu is our choice for this article. It is one of the most popular Linux distros with plenty of resources on the web.

Apache

Apache HTTP Server is a web server application. It is one of the oldest and one of the most commonly used web servers.

Some heavy-traffic websites prefer light-weight alternative like LiteHTTP or nginx.

MySQL

MySQL is a database management system. It is one of the most commonly used database systems.

Some resource-intensive websites prefer light-weight alternative like SQLite

phpMyAdmin is a tool to manage MySQL databases on a friendly web interface.

PHP

PHP is a server-side scripting language designed for web development.

WordPress

WordPress is a content management system (CMS). It helps you create and manage content on a web server.

Each CMS has it pros and cons. Alternative includes Drupal and Joomla!.

Command Line Interface (CLI) and Command

An interface the means to interacting with a computer.

Modern personal computers have intuitive graphical interface, that is a desktop, a start menu for Windows or a dock for Mac, and icons that represent files or application that you can click to open or to execute.

To interact with a server, however, the interface of choice is usually the command line interface. It is a text box where you can enter command that you would like the remote server to execute, and where you will receive output or reply from the server, in form of text.

If a graphical user interface is a movie, a command line interface is a novel.

A command is what you send to a remote server, asking it to perform certain operations.

A command is usually constructed as follow.

COMMAND OPTION INPUT

For example the command to remove a file is rm which stands for remove. To remove a file named wp-config.php located in the folder /var/www/domain, you can enter the below command in a command line interface.

rm /var/www/wp-config.php

Where the command is rm and the input is /var/www/wp-config.php.

Options are optional. For this particular operation, no option is needed.

But to remove a directory, we will need to add a -r option which tells the rm command to remove the directory and its contents recursively.

rm -r /var/www/domain

Other command operations include change your working directory with the cd command.

cd /var/www/domain

Where cd is the command and /var/www/domain is the input telling the command where you would like to change the working directory to.

The command to copy a file is cp.

cp /var/www/domain/wp-config-sample.php /var/www/domain/wp-config.php

Where cp is the command and /var/www/domain/wp-config-sample.php /var/www/domain/wp-config.php are the input. This input comprises of two elements. The first element is the file that you would like to make a copy of and the second element is the path and name of the new file.

The command to connect to a server via SSH is ssh.

ssh -i /User/Kurt/Desktop/rsa-cert.pem ubuntu@162.243.139.49

Where ssh is the command with an option of -i /User/Kurt/Desktop/rsa-cert.pem and an input of ubuntu@162.243.139.49. The option tells the command that we will be authenticating with a certificate and the input is the username that we will be authenticating the connection with and the IP address of the server we are establishing a connection to.

Most command reserves the -h or –help option for manual page, or the manpage. When you are unsure what a particular command does, what options it allows, or how its input should be constructed, simply enter the command with a -h or –help options without input, and it will output a manpage to guide you through.

cp -h

There are tools and commands for different operations. You can find plenty of resources for these tools and commands on the web. It is a good idea to be familiar with the tools and command for basic operation.

Ubuntu and Substitute User Do (sudo)

All Linux system has a superuser called root. This root user is the God in your Linux universe who has permission to perform every operation and access to every file.

With great power, comes great responsibility. Executing a harmful command with the root user account can have a catastrophic effect. Executing one specific command with the root user can destroy the entire Linux operating system.

Ubuntu, among other popular Linux operating systems, hence by default disables the root account, and instead takes advantage of a tool called Substitute User Do (sudo) to perform operations that require root privilege without exposing the root account.

What Substitute User Do (sudo) does basically is execute any given command as another user. By default it will execute the given command as root.

For example to install the apache web server, you can execute the apt-get command and tells it to install the apache2 package. Such operation requires root privilege. To do this without having to log in to the root account, you can use sudo.

sudo apt-get install apache2

This allows a user to harness the full power of the root account while limiting the downside of mis-executing harmful operation.

Domain Name Registration

Registering a Domain Name

There are many commercial offerings for registering a domain name. They are called domain registrars.

GoDaddy (http://www.godaddy.com/) is one of them.

GoDaddy offers different discounts/promo year-round. Look for coupon code online. 20-30% off is usual. Never buy at list price.

Registering a domain is straightforward. Once it is done, you can go to your domain registrar management console, edit the A record in the DNS zone file, and point it to the public IP address of your web server.

The new A record should take effect relatively quickly depending on the TTL value.

Looking up Info Regarding a Domain

Go to whois.net and enter the domain of interest.

Setting up a Web Server

Leasing a Server from Hosting Service Provider

There are many commercial offerings for hosting service. You pay a fixed subscription, sometimes with a variable usage cost, and in return you will be provided access to a server for hosting service.

Amazon Web Services (http://aws.amazon.com/free/) offers new customers 750 hours of EC2 micro instance for 12 months i.e. 1 micro instance server free for a year.

Registering an account is straightforward. Once it is done, you can go to the AWS Management Console web interface, under “EC2”, look for “Instances”, and launch a server, or as it is called by AWS, an “instance”.

Again Ubuntu is our choice for this tutorial. It is the most popular Linux distro with plenty of resources on the web.

After launching a new instance, remember to go to “Security Groups”, to allow for inbound SSH (TCP on port 22) and HTTP (TCP on port 80) connections so you can connect to your server remotely via SSH and your server can listen for incoming HTTP webpage connection.

One alternative to Amazon Web Services is Digital Ocean. It is designed with developer in mind. Cost-effectively starting US$5 a month, it is easy to setup and it comes with a friendly web interface.

https://www.digitalocean.com/?refcode=6c58206600bd

Singing up using the referral URL above will give your Digital Ocean account US$10 credit. Once you have spent US$25 with Digital Ocean, I will receive US$25 credit.

Connecting to the Server

Two Common Tools to Connect to a Remote Server
Secure Shell (SSH)

Securely connect to a remote host to execute command. On Linux and Mac, SSH is built-in. On Windows, use PuTTY (http://www.putty.org/), a free SSH and telnet client.

SSH File Transfer Protocol (SFTP)

Securely connect to a remote host for file transfer. Use FileZilla (https://filezilla-project.org/) or Cyberduck (https://cyberduck.io/).

Connect to the Server with SSH

Use SSH to connect to the remote server. The default username for Ubuntu on AWS is ubuntu.

ssh -i PATH-TO-CERTIFICATE USERNAME@IP-ADDRESS

ssh -i /User/Kurt/rsa-cert.pem ubuntu@162.243.139.49

Use chmod to change the permission of the certificate file to 600 if prompted “Permissions 0644 for ‘/User/Kurt/Desktop/rsa-cert.pem’ are too open.”.

chmod 600 PATH-TO-CERTIFICATE

chmod 600 /User/Kurt/Desktop/rsa-cert.pem
Enable SSH Access with Password

Using a certificate to establish a SSH connection is more secure but less handy. To enable SSH connection with a password, edit the SSH config file.

sudo nano /etc/ssh/sshd_config

Control + w to search for “PasswordAuthentication” in the config file and change it to “yes”. Control + x to exit, press “y” and “enter” to save.

PasswordAuthentication yes

Reload SSH for the new setting to take effect.

sudo service ssh reload

Create or change the password for the ubuntu user.

sudo passwd ubuntu

You will asked to type in a new password.

Install Apache, MySQL, phpMyAdmin and PHP

Update the distro package lists to see if any of the installed software has a newer release.

sudo apt-get update

Upgrade all software to the latest release.

sudo apt-get upgrade

Install Apache, MySQL, phpMyAdmin and PHP.

sudo apt-get install apache2 mysql-server php5 phpmyadmin

When installing MySQL, you will be asked to create a password for MySQL root account.

When installing phpMyAdmin, you will be asked to provide the MySQL root account password, you will also be asked to create a password for phpMyAdmin root account.

Once the installation is complete, go to your server’s IP address e.g. 162.243.139.49 with a browser and you should see an Apache welcome page.

Configuring Apache

Make a subfolder under /var/www/ to house all the web content.

sudo mkdir /var/www/DOMAIN

sudo mkdir /var/www/kurtcms.org

The folder is created under a sudo (Substitute User DO) command hence the folder is owned by root. Now change the folder’s owner to www-data, that is the user under which the Apache web server runs.

sudo chown www-data:www-data /var/www/DOMAIN

sudo chown www-data:www-data /var/www/kurtcms.org

Make a copy of the default config and name it by your domain name.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/DOMAIN.conf

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/kurtcms.org.conf

Modify your domain config file to your settings. Use the nano editor to edit it. When done, control + x to exit, press “y” and “enter” to save.

sudo nano /etc/apache2/sites-available/DOMAIN.conf

sudo nano /etc/apache2/sites-available/kurtcms.org.conf

Modify these in your domain config file.

ServerName DOMAIN
ServerAlias www.DOMAIN
ServerAdmin EMAIL
DocumentRoot /var/www/DOMAIN

<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>

For example for kurtcms.org.

ServerName kurtcms.org
ServerAlias www.kurtcms.org
ServerAdmin kurtcms@gmail.com
DocumentRoot /var/www/kurtcms.org

<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>

For tighter security, we should disable directory listing. This setting is in the default config file. Change it with the nano editor. When done, control + x to exit, press “y” and “enter” to save.

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

Modify these in the default config file.

ServerAdmin EMAIL
DocumentRoot /var/www/DOMAIN

For example.

ServerAdmin kurtcms@gmail.com
DocumentRoot /var/www/kurtcms.org

Enter these in the default config file.

<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Tell Apache to enable the website.

sudo a2ensite DOMAIN.conf

sudo a2ensite kurtcms.org.conf

This command enables the rewrite module/function of Apache. Rewrite function is by default disabled but it comes in handy so we will enable it now.

sudo a2enmod rewrite

Restart Apache for new settings to be effective.

sudo service apache2 restart

Alternatively ask Apache to reload the settings.

sudo service apache2 reload

If apache restarts or reloads the configs without outputting any error message then, voila, your site is enabled on this web server.

Install WordPress

Download WordPress

Download WordPress.

sudo wget http://wordpress.org/latest.tar.gz -P /var/www/DOMAIN

sudo wget http://wordpress.org/latest.tar.gz -P /var/www/kurtcms.org

Uncompress the WordPress archive.

sudo tar -xzvf /var/www/DOMAIN/latest.tar.gz

sudo tar -xzvf /var/www/kurtcms.org/latest.tar.gz

Change the owner and group of the WordPress files to www-data.

sudo chown -R www-data:www-data /var/www/DOMAIN/wordpress

sudo chown -R www-data:www-data /var/www/kurtcms.org/wordpress

Move the content of the DOMAIN/wordpress folder to the DOMAIN/ parent folder.

sudo mv /var/www/DOMAIN/wordpress/* /var/www/DOMAIN/

sudo mv /var/www/kurtcms.org/wordpress/* /var/www/kurtcms.org/

Remove the empty DOMAIN/wordpress subfolder

sudo rm -r /var/www/DOMAIN/wordpress/

sudo rm -r /var/www/kurtcms.org/wordpress/

WordPress is downloaded to your domain folder.

Create a Database for WordPress

Create a shortcut to the phpMyAdmin web interface under your domain folder.

sudo ln -s /usr/share/phpmyadmin /var/www/DOMAIN/phpmyadmin

sudo ln -s /usr/share/phpmyadmin /var/www/kurtcms.org/phpmyadmin

Go to DOMAIN/phpmyadmin e.g. kurtcms.org/phpmyadmin.

Login with your phpMyAdmin root account e.g. username: root, password: the one you entered during the installation.

Go to “Users” > “Add User”

User name: DOMAIN e.g. kurtcms_org
Host: localhost

Click “Generate” to generate a password. Copy the generated password to a local notepad.

Check “Create database with same name and grant all privileges” before clicking “Go” to create a new user and a database with a matching name.

Create a wp-config.php config file by copying the wp-config-sample.php sample config file.

sudo cp /var/www/DOMAIN/wp-config-sample.php /var/www/DOMAIN/wp-config.php

sudo cp /var/www/kurtcms.org/wp-config-sample.php /var/www/kurtcms.org/wp-config.php

Edit the wp-config.php config file.

sudo nano /var/www/domain/wp-config.php

sudo nano /var/www/kurtcms.org/wp-config.php

Modify these in the wp-config.php config file to match your database.

define('DB_NAME', 'kurtcms_org');
define('DB_USER', 'kurtcms_org');
define('DB_PASSWORD', 'generated_password');
define('DB_HOST', 'localhost');

Go to DOMAIN/wp-admin/install.php e.g. kurtcms.org/wp-admin/install.php to install WordPress.

Install Plugins

Go to http://DOMAIN/wp-admin/plugin-install.php e.g. http://kurtcms.org/wp-admin/plugin-install.php

Type in the name of the plugin you would like to install and click “Install Now”

These are good for a fresh setup.

  • Google Analytics by Yoast
  • WordPress SEO by Yoast
  • Jetpack by WordPress.com
  • 404 to Start
  • Redirection

Enabling Swap File

Swap file is a portion of your storage that is set aside as virtual working memory. It allows you to efficiently increase the amount of working memory of your server without installing additional physical working memory.

Allocate a 2GB file block and call it swapfile on root.

fallocate -l 2G /swapfile

Change the permission of the certificate file to 600.

chmod 600 /swapfile

Tell your server the swapfile is ready.

mkswap /swapfile

Verify that the swap file is enabled on your server.

free -m

You should see something like this.

Swap: 2047 0 2047

To make this change permanent, edit the file system table so the swap is enabled when the server starts.

nano /etc/fstab

Add this to the last line. Control + x to exit, press “y” and “enter” to save.

/swapfile none swap sw 0 0

Setting a portion of the storage aside as virtual working memory is an efficiently way to increase the total amount of working memory. The read/write speed however is no match to physical working memory. To keep the system on the faster physical working memory and use the slower virtual working memory if it must, we can tune the swapiness parameter. The higher the swappiness, the more aggressive your server will be using virtual working memory. 10 is good measure for a server with adequate working memory.

sysctl vm.swappiness=10

To make this change permanent, edit the sysctl config file so the parameter is loaded when the server starts.

nano /etc/sysctl.conf

Add this to the last line. Control + x to exit, press “y” and “enter” to save.

vm.swappiness=10

Google Analytics and Google Webmaster

Google Analytics and Google Webmaster are helpful tools to monitor and provide insights on your website.

Google Analytics has a helpful guide to get you started at https://support.google.com/analytics/answer/1008015?hl=en

To install tracking code on your WordPress website, you can simply install the Google Analytics by Yoast plugin.

Google Webmaster has a helpful page to get you started as well at https://support.google.com/webmasters/topic/4564315?hl=en&ref_topic=4581229

Back Up Your Content

Taking a Snapshot

Most if not all hosting providers allow you to take a snapshot of your server and restore your server from a snapshot. Given your web content and database are located in the same server, taking a snapshot of the server will back up everything.

Taking a snapshot of an Amazon EC2 instance is rather straightforward, simply go to the AWS Management Console web interface, under “EC2”, look for “Elastic Block Storage”, to take a snapshot of your server.

Back Up Your Content Manually

Two places where your content is stored.

  • Under your /var/www/DOMAIN folder where you will find your content management system e.g. WordPress and its content e.g. plugins and themes.
  • Inside your MySQL database that you created for your content management system e.g. WordPress where you will find the your WordPress data e.g. posts and pages.
Create a Backup Copy of Your /var/www/ Folder

Make an archive of your /var/www/ directory with tar.

sudo tar -cvzf /var/www.tgz /var/www

Then use any SFTP (SSH File Transfer Protocol) tool to connect to your server and download the www.tgz for safe-keeping.

And to extract the archive.

sudo tar -zxvf www.tgz
Create a Backup Copy of Your MySQL Database

Go to DOMAIN/phpmyadmin e.g. kurtcms.org/phpmyadmin. Login with your phpMyAdmin root account.

Go to “Export” > “Go” to export all databases in SQL format, or select the “Custom” option to select which database(s) to be exported.

To import a SQL database, go to “Import”, select the SQL database, and “Go”.

If you receive a warning that the maximum file size allowed is 2,048KiB, or the database you are importing is larger than the maximum upload file size permitted, then increase the maximum upload file size in the PHP config file.

sudo nano /etc/php5/apache2/php.ini

Control + w to search for “upload_max_filesize” in the config file and make it larger e.g. 10M. Control + x to exit, press “y” and “enter” to save.

upload_max_filesize = 10M

Divorce with Amazon Web Services

To divorce with Amazon Web Services after the first 12 months of free EC2 micro instance, you can go to the AWS Management Console web interface, under “EC2”, and make sure that all your instances are terminated and the list is empty.

Also remember to go to “Elastic IPs”, to release all IP addresses. Amazon charges for unused public IP address.

Conclusion

Building a website is fun! It is true that a number of components need to be put together and it might seem hectic at first. Once this is done, content creation and management with WordPress should be a breeze.

In the world of web design, the only limit is your imagination.