How to Setup a Web Server with LAMP Stack on Ubuntu 18.04 VPS

LAMP stack is a group of four Open-source software applications that are used together to run a full-blown web server. These include: Linux, Apache, MySQL and PHP.

Most of the world’s busiest sites run on Linux OS. Apache, on the other hand, is the cornerstone of highly trafficked websites and is one of the most used software in the internet economy.

The high-quality web server is fast, reliable and compatible with Linux.

MySQL is a Relational Database Management System (RDMS)that powers web applications by providing a secure environment for data storage.

PHP (Hypertext Preprocessor) is a general-purpose scripting language used to code dynamic websites.

PHP can be embedded in HTML and is executed on the server, therefore, offering great security for web applications.

This is a step-by-step guide for installing Linux, Apache, MySQL, and PHP on your Ubuntu 18.04 VPS.

At the end of the guide, you should be able to run a website on your Virtual Private Server.

Prerequisite

Step 1: Login to your VPS account

First, log in to your VPS account by entering the login credentials assigned to you when you created an account.

Next, create a VPS instance and select Ubuntu 18.04 as the Operating System. You will also see an option for selecting the server location(data center), vCPUs, RAM, Disk space and bandwidth.

If you are running just a few websites, a VPS plan with the below features should work pretty well:

  • 1 GB RAM
  • 1 vCPU
  • 25 GB disk space
  • 1 TB data transfer.

Next, SSH to VPS server using the public IP address of your instance, username (usually root) and password that was assigned to you.

If you are running Linux or Mac OS on your local computer, use the built-in Command Line Interface(CLI) to connect to your VPS server using the syntax below:

$ ssh root@198.18.0.12

Replace 198.18.0.12 with the public IP address associated with your VPS instance.

If you are on Windows, you can connect to your server through PuTTY SSH client.

Step 3: Creating a Non-root User with sudo Privileges

For security purposes, it is advisable to create a non-root user that can perform sudo tasks when your first set up a Linux server. You can then elevate privileges when running super admin tasks using the sudo command.

To create the user, run the command below and replace james with your desired name:

$ adduser james

You will be prompted to create a password for the new user. Next, press Enter multiple times to skip the other optional fields. Towards the end of the questions, you will be asked to confirm if the details for the new user are correct. Hit Y and press Enter to proceed.

Next, add the user to the sudoers group by running the command below:

$ adduser james sudo

You can now log out and login with the new user in order to install LAMP stack on your Ubuntu 18.04 server.

Step 4: Updating the Software Index

Ubuntu 18.04 has a nice command line package manager (apt). You can use it to install software applications as required.

The package manager maintains an index of software on the server.

First, update the package list information by running the command below:

$ sudo apt-get update

Step 5: Installing Apache Web Server on Ubuntu 18.04

We now have a VPS running Ubuntu 18.04 as the Operating System. We can now go ahead and install our first software – Apache. To do this,  run the command below:

$ sudo apt-get install apache2

Press Y and hit Enter when prompted to confirm the installation.

Then, enter the public IP address associated with your server on a web browser to test Apache installation.

http://198.18.0.12

If the software was installed successfully, you should see the below web page:

The default website root directory on Apache is /var/www/html. So if you want to publish your website, you should upload all your files to that directory via an FTP client like Filezilla.

Step 6: Installing MySQL Server on Ubuntu 18.04

Next, we can install the MySQL database server on our VPS instance by running the command below:

$ sudo apt-get install mysql-server

Just before the installation is finalized, you will be prompted to enter a root password for your MySQL server.

You should not confuse this with the root password for your Ubuntu 18.04 server. Also, select a strong password with a mix of both letters and numbers. Also, ensure a good length of at least 8 characters.

MySQL server should be installed without any problems. To confirm if the installation was completed successfully, run the command below:

$ sudo service mysql status

Output

MySQL Server Status on Ubuntu 18.04 VPS

The default MySQL server installation is not secure. Luckily we can use the mysql_secure_installation command to harden the installation. Just run the command below:

$ sudo mysql_secure_installation

You will be taken through a series of questions. Enter the  answers below and hit Enter after each response:

Setup ‘validate password’ plugin? [Y/N] Y
Password Validation Policy Level: 2
Root Password: PASSWORD
Re-enter password: REPEAT PASSWORD
Continue with the password provided? Y
Remove anonymous users? [Y/N] Y
Disallow root login remotely? [Y/N] Y
Remove test database and access to it? [Y/N] Y
Reload privilege tables now? [Y/N] Y

MySQL server will be secured and you will get a success message at the end.

Step 6: Installing PHP Scripting Language on Ubuntu 18.04

PHP works together with a web server to deliver web content to a client. Once you request a PHP file, the PHP software processes the request and sends HTML back to your browser via a web server and in our case Apache.

To install PHP, run the command below:

$ sudo apt-get install php

Then, reload Apache web server:

$ sudo service apache2 restart

To test if PHP is working, create a file on the root of your website using nano text editor:

$ sudo nano /var/www/html/info.php

Then copy paste the below content:

<?php

phpinfo();

?>

Press CTRL + X, Y and hit Enter to save the file:

Then, on a web browser visit the address below and replace the IP address with the public IP address associated with your Ubuntu 18.04 VPS

http:// 198.18.0.12/info.php

If you see the below web page, it means PHP is working as expected.

PHP info page on Ubuntu 18.04 VPS

PHP does not install all modules that are required for running a full-blown website by default. Luckily, we can install all these packages with a single command:

$ sudo apt-get install php-cli php-common php-mbstring php-gd php-intl php-xml php-mysql php-zip php-curl php-xmlrpc

Press Y and hit Enter when prompted to confirm the installation.

Then, restart Apache using the command below:

$ sudo service apache2 restart

Conclusion

In this guide, we have taken you through the steps of installing Linux, Apache, MySQL, and PHP in order to run and host a website from your VPS server.

If you are new to VPS hosting, sign up with Digital Ocean today and enjoy $100 free test credit.

Ads by Google

Leave a Reply

Your email address will not be published. Required fields are marked *