What is a LAMP? A "LAMP" stack is a group of open source software that is typically installed together to enable a server to host dynamic and/or static websites.
This term is actually an acronym which stands for:
Linux - the operating system that will run your applications (we will use CentOS);
Apache - Apache is the most widely used web server software;
MySQL - the database that will store all the information (we will use the free version MariaDB);
PHP - the processor for managing your dynamic content.
In this guide, we'll get a LAMP stack installed on an CentOS 7 VPS. CentOS will fulfill our first requirement: a Linux operating system.
Before you begin with this guide make sure you have at least the following requirements:
- A server (could be virtual such as a VPS or bare metal) with internet connection;
- CentOS 7 installed operating system with SSH enabled, root access and a non-root user;
- A sufficient amount of goodwill.
Very well! We are ready to have a fully working webserver in less than 60 minutes!
Step 0 - Access your server
Access your server via SSH, you will not need a graphical interface that would only overload your server without any particular advantage for our purpose.
Step 1 - Install Apache
The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.
We can install Apache easily using CentOS's package manager, yum.
A package manager allows us to install most software pain-free from a repository maintained by CentOS without worrying about dependencies.
Lets start to install Apache by typing in the console:
sudo yum install httpd
Since we are using a sudo command, these operation will be executed with root privileges, so make sure you have root access to your server. It will ask you for your regular user's password to verify your intentions.
Afterwards, your web server is installed.
Once the install process is completed, start Apache service by typing:
sudo systemctl start httpd.service
This command will not return anything in the console but if you want to be sure that process is correctly started up and running type:
sudo systemctl status httpd.service
We should expect an output like this:
Last login: Tue May 15 15:33:50 2018 from 18.104.22.168 [[email protected] ~]# sudo systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2018-02-16 10:47:34 CET; 2 months 27 days ago Docs: man:httpd(8) man:apachectl(8) Main PID: 20198 (httpd) Status: \"Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec\" ....
You can even do a spot check right away to verify that everything went as planned by visiting your server's IP address (private in case you do not have a public one) in your web browser:
If Apache is working properly you will see the default CentOS 7 Apache web page, which is there for informational and testing purposes.
The last thing you will want to do is enable Apache to start on boot using the following command:
sudo systemctl enable httpd.service
Step 2 — Install MySQL (MariaDB)
Now that we have our web server up and running, it is time to install MariaDB, a MySQL drop-in replacement completely free. Basically, it will organize and provide access to databases where your site can store information.
Again, we can use yum to acquire and install our software. This time, we'll also install some other "helper" packages that will assist us in getting our components to communicate with each other:
sudo yum install mariadb-server mariadb
When the installation is complete, we need to start MariaDB with the following command:
sudo systemctl start mariadb
Now that our MySQL database is running, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit, for example protecting root access with password (by default MySQL come with unprotected root access).
Start the interactive securing script by typing:
The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.
Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the steps:
Enter current password for root (enter for none): New password:ÃÂ Re-enter new password:ÃÂ
For the rest of the questions, you should simply hit the "ENTER" key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.
As per Apache service we will want to enable MariaDB to start on boot using the following command:
sudo systemctl enable mariadb.service
At this point, your database system is set up with and we can continue with the next step.
Step 3 — Install PHP 7.x
PHP is the component of our setup that will process code to display dynamic content.
We can once again leverage the yum system to install our components but since precompiled PHP 7.x binaries are available for CentOS 7 from the Webtatic, IUS, or Remi's RPM repositories we need first to load the necessary repositories.
To setup the Webtatic YUM repo we will type:
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Now we are ready to install PHP 7.x, to understand what versions are available we can type the following command that will give us a list of versions we could install:
In our case we will proceed by installing the 7.1 version with some of the most commonly used extensions to enhance the functionality of PHP, let's type:
sudo yum install -y mod_php71w php71w-cli php71w-common php71w-gd php71w-mbstring php71w-mcrypt php71w-mysqlnd php71w-xml
Note: if you want to customize the extensions to be installed (only if you know what you are doing) you can type the following to have a list of available components with a short description:
yum search php71w-
This should install PHP without any particular problem. Just a common sense rules it's suggested to perform a backup of the configuration file before to use the webserver in order to be sure that once we change the configuration we keep a safe default copy.
To do this just type:
sudo cp /etc/php.ini /etc/php.ini.bak
Note: If you want to customize your php configuration you should read my other tutorial "How to configure PHP 7.x".
Now we need to restart the Apache web server in order for it to work with PHP. Type:
sudo systemctl restart httpd.service
At this point, your LAMP stack is installed and configured. We should still test out our PHP though.
Step 4 — Test PHP Processing on your Web Server
In order to test and make sure that our system is configured properly for PHP, we can create a very basic PHP script.
We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the "web root".
In CentOS 7, this directory is located at /var/www/html/. We can create the file at that location by typing:
sudo vi /var/www/html/info.php
This will open a blank file. We want to write the following text inside the file:
When you are finished, save and close the file typing:
CTRL + C :wq ENTER
If you are running a firewall (enable by default on CentOS 7), you need to allow HTTP and HTTPS traffic, typing the following commands:
sudo firewall-cmd --permanent --zone=public --add-service=httpÃÂ sudo firewall-cmd --permanent --zone=public --add-service=https
Please note that we are not allowing traffic to MySQL from external sources, considering that for most of the standard needs you won't need to operate the database from an external source. In case you are interested in more details on MySQL customization feel free to read my "MySQL customization" that will give you a deeper understanding on how it works.
Once created our firewall rules it's necessary to reload it to apply the new config:
sudo firewall-cmd --reload
Now we are ready to test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit the following page in our web browser.
The address you want to visit will be:
If everything went good and your PHP is working as expected you will see a page like this:
This page basically gives you information about your server from the perspective of PHP including installed version for each module. It is useful for debugging and to ensure that your settings are being applied correctly.
Congratulations! Now you have a LAMP stack installed, you have many choices for what to do next. Basically, you've installed a platform that will allow you to install most kinds of websites and web software on your server in less than 60 minutes!
Have fun with your new LAMP platform!