If you decided to build a web server to host a website, the first thing that you will have to do is install LAMP stack. It consists of services like Apache, MySQL (or MariaDB) and PHP engine, it is also known as LAMP (Linux, Apache, MySQL, PHP). After finishing this tutorial you will master all basic steps in order to successfully install LAMP on CentOS 7 VPS.
What you’ll need
Before you begin this guide you’ll need the following:
- SSH root access to the server
Table of Contents
Step 1 — Installing Apache
Initially released on 1995, Apache is the most popular and longest-standing HTTP server software. Apache project is maintained by open-source community Apache Software Foundation, which means that it’s free of charge. We can install it pretty easily as it comes with default CentOS software repositories. Just run this command in terminal:
yum install httpd -y
Now, start the Apache service and enable it on boot:
systemctl start httpd.service systemctl enable httpd.service
To verify if it’s started correctly, execute
ps aux | grep httpd command:
[root@vps ~]# ps aux | grep httpd root 15817 0.5 0.2 221792 4908 ? Ss 04:33 0:00 /usr/sbin/httpd -DFOREGROUND apache 15818 0.0 0.1 221792 2628 ? S 04:33 0:00 /usr/sbin/httpd -DFOREGROUND
As we might see, HTTP daemon started successfully. You should be able to access your server via IP address now.
Firstly, check your server IP using
dig +short myip.opendns.com @resolver1.opendns.com
Once you obtained it, input the IP address to preferred web browser. The result should be a page with text such as
It works! or
Step 2 — Installing MySQL (MariaDB)
After successful implementation of a web server, we will install actual database service. MariaDB is a community-developed fork of the very well known MySQL service, the good news is that it’s also free. As it comes with default CentOS repositories as well, just run Yum:
yum install mariadb-server mariadb -y
Once the installation completes, start MariaDB and enable it on boot:
systemctl start mariadb systemctl enable mariadb
After starting MariaDB, execute initial security script to remove some risky defaults:
Firstly, MariaDB will ask you for root password, however, since this is initial installation, you don’t have any, so just press enter. Next prompt will ask if you want to set a root password, enter Y and follow the instructions:
Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization. New password: password Re-enter new password: password Password updated successfully! Reloading privilege tables.. ... Success!
For all other questions, you can safely click ENTER key and accept default settings. At this point your database is ready and we can proceed further to PHP installation.
Step 3 — Installing PHP
PHP engine is required to process our PHP code, access database and serve the content to Apache. Once again, you can use Yum to install required PHP packages. Run this command in the terminal:
yum install php php-mysql -y
In order for Apache to recognize PHP engine, please restart it:
systemctl restart httpd.service
That’s it! You have just built a web server.
Step 4 — Testing PHP engine
You can check if PHP is actually working by creating a test file. Just run this command:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
info.php via IP, like this:
You should see the default PHP information page with its version in the header:
As you might have already noticed, building a simple web server is not that hard and it does not require a huge package of skills. You just installed LAMP stack on CentOS 7. Apache will serve content to the browser, while PHP will execute the code and make queries to the MariaDB in order to get required data.