Install a LAMP stack on your VPS (Tutorial)

install_lamp_stack_vps

LAMP is still the most popular software stack for setting up a web server. In this tutorial, we’ll show you how to install a LAMP stack on your VPS, so that you can start hosting your sites right away.

What is LAMP?

LAMP is a software stack designed to host web content, like websites and web apps. It’s called LAMP because it’s built on Linux (L), Apache web server (A), a MySQL database (M), and the PHP scripting language (P).

In some modern installations, another programming language can be plugged into that “P” position. And some developers substitute MariaDB for MySQL. But the MySQL/PHP version is the classic LAMP stack, so that’s what we’ll show you how to install here.

Why is it called the LAMP stack?

LAMP is referred to as a software stack, because each of the pieces builds on top of the other.

Linux is your operating system, which creates the base layer. Apache sits on top of Linux because it needs Linux to operate. MySQL sits alongside Apache to store all the information it needs.

And PHP sits atop the stack to drive the actual display of the website, pull info from the database and create your user experience.

How to install LAMP on your server

Prerequisites

  • A VPS running any of our OS options— CentOS 6/7, Ubuntu 16.04/18.04.
  • A non-root, sudo-enabled user. If you only have a root user, see our SSH Tutorial for details on creating new users.

Notes

This tutorial requires the use of IP addresses. Whenever you see the IP_ADDRESS variable, replace it with your server’s IP address.

Ubuntu 16.04/Ubuntu 18.04

The easiest and fastest way to install LAMP stack in Ubuntu 16.04 is to use tasksel, a tool that installs each element of LAMP stack with a single command.

Tasksel is installed by default in Ubuntu 16.04, If tasksel is missing in your system, then install it by issuing following command from the terminal:

$ sudo apt-get install tasksel

To run tasksel from the command line, type:

$ sudo tasksel 

Now tick LAMP Server and press the Tab key once, followed by the Enter key. You will be prompted to provide a root password for MySQL, and once you’ve done that, the installation will be complete within a few minutes.

Find the versions of Apache, MySQL, and PHP by issuing the following three commands from the terminal.

$ apache2 -v
Server version: Apache/2.4.18 (Ubuntu)
Server built:   2018-04-18T14:53:04
$ mysql -V
mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper
$ php -v
PHP 7.0.30-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.30-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

8 Year Anniversary Sale—> 16GB RAM from $9.99/mo!

It’s been 8 years since we launched our all-SSD VPS cloud. To celebrate, we’re offering amazing deals on all of our most popular plans– like a 16GB RAM SSD VPS loaded with resources for as little as $9.99/month!

Get limited-time deals!⚡

CentOS 6/CentOS 7

You need to install each element of LAMP stack separately in CentOS 6/CentOS 7, since the packages are not grouped like they are in Ubuntu.

Before proceeding with the installation of a LAMP stack in CentOS 6/CentOS 7, update your system using the following yum command.

$ sudo yum update && sudo yum upgrade

CentOS 7 enables a firewall by default, and if it is running in your system, open up port number 80using firewall-cmd. Opening port 80 will allow web traffic to pass through the firewall. You need the following steps only if your VPS is running CentOS 7.

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --reload

Apache

Now install each element of the LAMP stack one by one. To start, install Apache by issuing the following command in the terminal:

$ sudo yum install httpd

Next, enable Apache to start automatically during system boot. The steps are different for CentOS 6 and CentOS 7.

In CentOS 6:

$ sudo chkconfig httpd on
$ sudo service httpd start

In CentOS 7:

$ sudo systemctl enable httpd.service
$ sudo systemctl start httpd.service

MySQL

Install MySQL server using following yum command and start it. Optionally, you can run mysql_secure_installation to make it secure and enable it during system boot.

In CentOS 6:

$ sudo yum install mysql-server
$ sudo service mysqld start
$ sudo mysql_secure_installation
$ sudo chkconfig mysqld on

In CentOS 7:

$ sudo yum install mariadb-server
$ sudo systemctl start mariadb
$ sudo mysql_secure_installation
$ sudo systemctl enable mariadb

PHP

The default repository of CentOS contains PHP 5, which is fine for some purposes, but many will want to install the more recent and improved PHP version 7. To install PHP 7, you need to add a new repository to your CentOS installation.

In CentOS 6:

$ sudo yum install epel-release yum-utils
$ sudo yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

In CentOS 7:

$ sudo yum install epel-release yum-utils
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Now enable the remi repository using yum-config-manager before moving forward with the installation of PHP 7, along with few other popular PHP modules. Type in the following commands on both CentOS 6 and CentOS 7.

$ sudo yum-config-manager --enable remi-php72
$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql

Once the installation of PHP 7 is done, restart Apache to apply new settings:

In CentOS 6:

$ sudo service httpd restart

In CentOS 7:

$ sudo systemctl restart httpd.service

You can also list all available PHP modules using the following command and then install the required modules one by one.

$ sudo yum search php-

Find the installed version of each element of the LAMP stack using the following commands:

$ httpd -v
$ php -v
$ mysql -V

Test your new LAMP stack installation

To test the installation of LAMP stack, create a phpinfo file inside the root directory of Apache, which is /var/www/html.

$ cd /var/www/html
$ sudo echo "<?php echo phpinfo();?>" > info.php

Point your favorite web browser to http://IP_ADDRESS/info.php. You can view the PHP version, system information, build date, and more. Seeing this page load confirms that the LAMP stack installation was successful.

Next Steps: Host a website on your LAMP stack

Now that you know how to install a LAMP stack, it’s time to get to building your own little home on the Internet!

LAMP is perfect for hosting WordPress sites, so if you’re looking to set one up just head to our tutorial on how to install WordPress using LAMP to get started!

If you need an extra boost, or don’t want to spend time in the terminal, you might want to look into some free cPanel alternatives to help you manage everything.

If you enjoyed this tutorial, be sure to sign up to our mailing list for new tutorials and server wisdom, delivered straight to your inbox!

cheap_vps_16GB

cheap_vps_16gb