A LAMP is actually an acronym which represents the Linux operating system, with the Apache web server, data is stored in a MySQL database, and content is processed by PHP. In this guide, we’ll get a LAMP stack installed on an Ubuntu 16.04 virtual private server (VPS).

Before we get started, you need to login to your VPS as ‘root’ via SSH. You can use a SSH client like PuTTY. In this tutorial I’ve used MobaXterm which is a very good software bundled with SFTP and other clients.

Install Apache And Allow In Firewall

After you login to your VPS visa SSH, you’ll see a terminal like this.

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 2

Before we start, run a apt-get update

sudo apt-get update

The Apache web server is among the most popular web servers in the world which makes it a great default choice for hosting a website. We use apt to install apache2.

sudo apt-get install apache2

You will see the following prompt : “After this operation, 6,438 kB of additional disk space will be used. Do you want to continue? [Y/n]”

Press Y to continue.

We need edithe apache config file to get rid of a recurring error. To do this type the following on you terminal.

sudo nano /etc/apache2/apache2.conf

This will open up the config file in ‘nano’. Nano is a text editor that uses the command line interface to edit files. Here’s the basics, you can use the keyboard arrows/mouse scroll wheel to navigate in a document. Make the changes you need and once you are done, press Ctrl + x and then press Y to save changes.

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 3
How the file looks in Nano

Now we want to add our server’s IP address. Add this line to the bottom of the config file.

ServerName server_IP

Replace server_IP with the IP of your VPS server, you can also put your domain name here but make sure the domain A records are pointed towards this server. See the following image for a representation.

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 4

After you are done, save and exit. Now type the following command to restart apache and let the changes take effect.

sudo systemctl restart apache2

Now we need allow apache to receive requests in the firewall. If you have not yet set up a firewall, you should set it up via ‘ufw’. Assuming you have it set up, type the following command to let apache through.

sudo ufw allow in "Apache Full"

You can check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser. If you see something similar, congrats your apache webserver is running !

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 5

Install MySQL And Configure

MySQL is an open-source relational database management system. This is absolutely necessary if you want to set a WordPress site or any other CMS. Installing MySQL is pretty easy. Run the following command.

sudo apt-get install mysql-server

You will be shown a list of the packages that will be installed, along with the amount of disk space they’ll take up. Press Y to continue.

During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. Make you set a strong password for your admin user.

Install PHP And Configure

PHP will help us display dynamic content. It can run scripts, connect to our MySQL database. It is a crucial part of a webserver. Run the following command to install.

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

We want apache to prefer PHP files in a directory and serve that PHP file. To do this we edit the dir.conf file.

sudo nano /etc/apache2/mods-enabled/dir.conf

Put index.php at the front after DirectoryIndex.

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Refer to the following photo for a demonstration.

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 6

Now we need restart the apache webserver.

sudo systemctl restart apache2

See the apache status.

sudo systemctl status apache2

You’ll see something similar which means your configuration is fine !

Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 7

If you want to just create a LAMP stack then you can stop here. If you want to setup your MySQL server for wordpress or other CMS like Joomla, Drupal, Ghost, Magento etc continue reading.

Create MySQL Database and User for WordPress/Other CMS

Type the following to login into mysql account.

mysql -u root -p

We will now create a database. The following command will create a database named ‘wordpress’ . You can name it whatever you like.

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 8

Next, we are going to create a separate MySQL user account that we will use exclusively to operate on our new database. We name it ‘wordpressuser’ and set a strong password by changing ‘password’.

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

We need to flush the privileges so that the current instance of MySQL knows about the recent changes we’ve made:

FLUSH PRIVILEGES;

Exit the mysql prompt.

EXIT;

A thing you should notice is the semi colon after sql query. You need to type a semi colon after every sql query otherwise it would not work.

Install Additional PHP Extensions

We can download and install some of the most popular PHP extensions for use with WordPress by typing:

sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc
sudo systemctl restart apache2

Allow for .htaccess Overrides and Rewrites

sudo nano /etc/apache2/apache2.conf

Towards the bottom of the file, add the following block.

<Directory /var/www/html/>
    AllowOverride All
</Directory>
Install Apache, MySQL, PHP LAMP stack on An Ubuntu VPS For Wordpress/Other CMS 9

Next, we can enable mod_rewrite so that we can utilize the WordPress permalink feature

sudo a2enmod rewrite

Restart apache for the changes to take effect.

sudo systemctl restart apache2

Wrapping Up

You can now use these mysql credentials to continue to install wordpress. If you liked this article, share it !