kurtcms.org

Thinking. Writing. Philosophising.

Email Github LinkedIn

Web Development: Build A Website with Linux, Apache, MySQL and PHP (LAMP), and WordPress

Posted on March 23, 2015 — 19 Minutes Read

What follows will be a walk-through on 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 servers.

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 network capacity.

Internet, Internet Protocol (IP) address and domain name

The Internet is a group of servers connected together. Public IP address (RFC 1918) is an unique identifier of each publicly accessible server, like a geographic coordinate which represents a unique location on earth. Domain name (RFC 1034) 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 servers. Linux to server is like Windows and Mac OS to personal computers. A bit unlike Windows and Mac OS though, Linux comes in different flavours. Different Linux flavours are called distributions or distros 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 distros are Ubuntu, Debian and Fedora.

Ubuntu is the Linux distro of choice for this walk-through. It is one of the most popular Linux distros with plenty of help and 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 alternatives such as NGINX.

MySQL

MySQL is a database management system for relational databases. It is one of the most commonly used database management systems. Some resource-intensive websites prefer light-weight alternatives such as SQLite.

phpMyAdmin is a tool to manage MySQL databases on a user-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. Alternatives include Drupal and Joomla.

Command Line Interface (CLI) and Command

An interface is a means to interact 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 applications that you can click with a mouse or touchpad to open or execute. To interact with a server, however, the interface of choice is often 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 was a movie, a command line interface would be like 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 argument(s) 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/directory, you can enter the below command in a command line interface.

$ rm /var/www/directory/wp-config.php

Where the command is rm and the input is /var/www/directory/wp-config.php. Arguments are optional. For this particular operation, no argument is needed. To remove a directory, however, you will need to add a -r argument which instructs the rm command to remove the directory and its contents recursively which is what the r stands for.

$ rm -r /var/www/directory

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

$ cd /var/www/directory

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

The command to copy a file is cp.

$ cp /var/www/directory/wp-config-sample.php /var/www/directory/wp-config.php

Where cp is the command and /var/www/directory/wp-config-sample.php /var/www/directory/wp-config.php is 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 full path and name for the new copy.

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

$ ssh -i /cert/rsa-cert.pem [email protected]

Where ssh is the command with an argument of -i /cert/rsa-cert.pem and an input of [email protected]. The argument tells the command that you will be authenticating with a certificate, and the input is the username that you will be authenticating the connection with, and the IP address of the server that you are establishing a connection to.

Most commands reserve the -h or --help argument for manual page, or the manpage for short. When you are unsure what a particular command does, what arguments it allows, or how its input should be constructed, simply enter the command with a -h or --help argument 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 them on the web. It is a good idea to be familiar with those for basic file operations before proceeding further.

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. Needless to say that with great power, comes great responsibility. Executing a harmful command with the root privilege could have a catastrophic effect. One specific command for example with the root permission could eradicate your entire Linux operating system. For this reason, Ubuntu, along with some other Linux operating systems, disables the root account by default, and instead takes advantage of a tool called Substitute User Do (sudo) to perform operations that require root privilege without exposing the root account. It executes any given command as root, or as any other user should you choose to.

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

$ sudo apt install apache2

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

Domain Name Registration

Registering a Domain Name

There are many commercial offerings for registering a domain name. They are called domain registrars. The best one around at the moment is perhaps Cloudflare. While it offers a range of free and paid services such as Content Delivery Network (CDN), Distributed Denial-of-Service (DDoS) mitigation and Zero Trust Network etc, it provides also domain name registration at cost. There is no better alternative cost-wise and its domain name management interface on web is as good as the any other if not better. Alternatives include GoDaddy and Google Domains.

Registering a domain is rather straightforward regardless of the registrar of choice. 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 Time to Live (TTL) value.

Looking up Info Regarding a Domain

Go to the ICANN Domain Name Registration Data Lookup website and enter the domain of interest.

Setting up a Web Server

Leasing a Server from a Hosting Service Provider

There are many commercial offerings for hosting services. You pay a fixed subscription, sometimes with a variable usage cost, and in return you will be provided access to a server, virtual or physical, for your hosting needs. Amazon Web Servuces (AWS) is one of the them, and it stands itself out by being the first in this field with an offering back as early as in August, 2006. Alternatives include Microsoft Azure and Google Cloud Platform (GCP). Most cloud computing service providers offer new customers free trial for select compute size and usesge, of various durations. Be sure to take advantage of them if you are just starting out your wonderful journey.

Registering an account on AWS is rather straightforward. Once it is done, you can go to the AWS Management Console and launch a server, or as it is called by AWS, a EC2 instance. Again Ubuntu is the Linux distro of choice for this walk-through. It is the most popular Linux distro with plenty of help and resources on the web. After launching a new Ubuntu 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 by SSH and your server can listen for incoming HTTP webpage connection.

One alternative to the estbalished providers is DigitalOcean. Despite the lack of a free trial, it is designed with developer in mind with plenty of pre-provisioned virtual machine images, and helpful tools that allows you to deploy in seconds. It also takes pride in its industry leading price-performance that leaves the prominent providers in the dust. Starting cost-effectively at US$4 a month, its Droplet is an excellent alternative. Singing up to DigitalOcean using this referral URL will give your DigitalOcean account 60-day, US$100 credit, and once you have spent US$25 with DigitalOcean, I will receive US$25 credit in return as well.

Connecting to the Server

Secure Shell (SSH) and SSH File Transfer Protocol (SFTP)

A Secure Shell (SSH) allows you to connect to a remote host. On Linux and Mac, SSH is built-in. On Windows, use instead PuTTY, a free SSH and telnet client. File may be transferred on a SSH connection also. To securely connect to a remote host for file transfer, use FileZilla or Cyberduck.

Connect to the Server with SSH

Enter the below in a command line interface to connect to your remote server with SSH. The default username for Ubuntu on AWS is ubuntu.

ssh -i *full-path-to-certificate* *username@ip-address*

$ ssh -i /cert/rsa-cert.pem [email protected]

Change the permission of the certificate file to 600 if prompted permissions 0644 for *full-path-to-certificate* are too open.

chmod 600 *full-path-to-certificate*

$ sudo chmod 600 /cert/rsa-cert.pem

Enable SSH Access with Password

Using a certificate to authenticate a SSH connection is more secure but it is also less handy. To enable SSH connection with a password, edit the SSH configuration file with the nano text editor.

$ sudo nano /etc/ssh/sshd_config

Locate the PasswordAuthentication parameter by control + w and type in the parameter name. Then change its value from no to yes.

PasswordAuthentication yes

Control + x to exit, and y and enter to save.

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 be asked to type in a new password.

Install Apache, MySQL, phpMyAdmin and PHP

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

$ sudo apt update

Upgrade all software to the latest release.

$ sudo apt upgrade -y

Install Apache, MySQL, phpMyAdmin and PHP.

$ sudo apt install apache2 mysql-server php phpmyadmin

When installing MySQL, you will be asked to create a password for the MySQL root account. When installing phpMyAdmin, you will be asked to provide the MySQL root account password that you have just created, you will also be asked to create a password for the phpMyAdmin root account. Once the installation is complete, go to your server’s IP address with a browser and you should be greeted with 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 command as root and as such it is owned by it. Change the folder’s owner to www-data, that is the user under which the Apache web server runs, to provide it with all the permissions it needs.

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 the domain config file to your settings.

sudo nano /etc/apache2/sites-available/*domain*.conf

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

These should be modified in your domain config file.

ServerName *domain*
ServerAlias *domain*
ServerAdmin *email-address*
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 [email protected]
DocumentRoot /var/www/kurtcms.org

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

Control + x to exit, and y and enter to save.

Modify these in the default config file. For security reason, you should disable directory listing also.

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

Modify these in the default config file.

ServerAdmin *email-address*
DocumentRoot /var/www/*domain*

For example.

ServerAdmin [email protected]
DocumentRoot /var/www/kurtcms.org

Enter these in the default config file.

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

Control + x to exit, and y and enter to save.

Once this is all set, tell Apache to enable the website.

sudo a2ensite *domain*.conf

$ sudo a2ensite kurtcms.org.conf

Apache comes with a rewrite module that comes in rather handy. Enabling it requires a single command.

$ sudo a2enmod rewrite

Restart Apache for the new settings to come in effect.

$ sudo service apache2 restart

Alternatively tell 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 the latest release of WordPress to your website folder under /var/www/.

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 downloaded 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 all 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 ready for configuration in your domain folder.

Create a Database for WordPress

Before creating a database on MySQL for WordPress using phpMyAdmin, a shortcut to the phpMyAdmin web interface under your domain folder needs to be created.

sudo ln -s /usr/share/phpmyadmin /var/www/*domain*/phpmyadmin

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

Once this is down, you can go to http://*domain*/phpmyadmin e.g. http://kurtcms.org/phpmyadmin on your browser and be greeted with the phpMyAdmin login page.

Login with your phpMyAdmin root account and the password you created during its installation, and click on Add User Account under User Account, enter a new user name e.g. your domain name and enter localhost for hostname. Click Generate to generate a password and note down the generated password.

User name: kurtcms_org
Host name: localhost
Password: (redacted)

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

WordPress will need to be instructed to use this newly created database. Create a wp-config.php config file by make a copy of the sample config file, wp-config-sample.php.

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 settings.

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

Go to http://*domain*/wp-admin/install.php e.g. http://kurtcms.org/wp-admin/install.php and follow the instructions on screen to install WordPress.

Install Plugins

Plugins are handy tools that may be installed on WordPress. Go to http://*domain*/wp-admin/plugin-install.php e.g. http://kurtcms.org/wp-admin/plugin-install.php and type in the name of the plugin you would like to install and click Install Now

These are a great start for a fresh setup. Do read more about them on their respective pages.

Enabling Swap File

Swap file is a portion of your storage that is set aside as virtual RAM. It provids additional RAM should the system need it.

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

$ sudo fallocate -l 2G /swapfile

Change the permission of the swap file to 600.

$ sudo chmod 600 /swapfile

Instruct your server that the swapfile is ready and where it is located.

$ sudo mkswap /swapfile

Verify that the swap file is enabled on your server.

$ sudo free -m

You should see them in the work.

Swap: 2047 0 2047

Edit the file system table so that swap is enabled when the server starts.

$ sudo nano /etc/fstab

Add this to the last line.

/swapfile none swap sw 0 0

Control + x to exit, and y and enter to save.

Setting a portion of the storage aside as virtual RAM is an efficient way to increase the total amount of working memory. The read/write speed of storage, which the virtual RAM is made of, is nonetheless no match to physical RAM. To keep the system on the faster physical RAM and use the slower virtual RAM only if needed, you can tune the swappiness parameter. The higher the swappiness, the more aggressive your server will be using virtual RAM. 10 is a starting point for a server with adequate physical RAM.

$ sudo sysctl vm.swappiness=10

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

$ sudo nano /etc/sysctl.conf

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

vm.swappiness=10

Cloudflare Content Delivery Network (CDN)

Cloudflare offers a free CDN service that caches the static content of your website in their global Point of Presence (PoP) closest to your audience. This will reduce your web page load time and speed up your website. Cloudflare has a number of helpful documentations to get you started. Advanced features are available on subscription as well.

Google Analytics and Google Search Console

Google Analytics and Google Search Console are helpful tools to monitor, and provide insights, on your website. Google Analytics has a helpful guide to get you started. To install tracking code on your WordPress website, you can simply install the Yoast SEO plugin and follow the steps on screen.

Google Search Console has a helpful page to get you started as well.

Back Up Your Content

Taking a Snapshot

Most if not all hosting providers allow you to take a snapshot of your server, to which, if needed, you may restore. Given your web content and database are located in the same server, taking a snapshot of the server will back up everything. This is rather straightforward on an AWS EC2 instance. Simply go to the AWS Management Console, look for Elastic Block Storage under EC2, and you will find the option to take a snapshot of your server. Taking a snapshot of a Droplet on DigitalOcean is a matter of a few clicks as well.

Back Up Your Content Manually

If nonetheless you will need to back up your web content manually, there are two places where you will have look.

  • Under the /var/www/domain folder where you will find your content management system and its associated contents e.g. plugins and themes.
  • Inside the MySQL database that you created for your content management system where you will find the your other 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 tool to connect to your server and download the www.tgz for safe-keeping.

For a restore, the backup archive will need to be extracted.

$ sudo tar -zxvf www.tgz

Create a Backup Copy of Your MySQL Database

Go to http://*domain*/phpmyadmin e.g. http://kurtcms.org/phpmyadmin. Login with your phpMyAdmin root account.

Click Go under Export to export all databases in SQL format, or select the Custom option to manually select which database(s) to be exported.

For a restore, go to Import and select the SQL database backup in your local system.

If you receive a warning that the maximum file size allowed is 2,048KiB, or that 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

Locate the upload_max_filesize parameter by control + w and type in the parameter name. Then increase its value to one that fits.

upload_max_filesize = 10M

Control + x to exit, and y and enter to save.

Divorce with Amazon Web Services

To divorce with AWS after the free trial, you can go to the AWS Management Console, and make sure that all your instances are terminated under EC2. Remember also to go to Elastic IPs to release all IP addresses. AWS charges for unused public IP address.

Thoughts

Building a website is fun! It is true that a number of components need to be pieced 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 development, the only limit is your imagination.