VPS

How to Install Linux, Apache, MySQL, PHP (LAMP) stack on CentOS 7

Introduction

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

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:

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 Testing 123...

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:

mysql_secure_installation

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

Now, access info.php via IP, like this:

http://your_server_IP_address/info.php

You should see the default PHP information page with its version in the header:

PHP info page

Conclusion

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.

About the author

Edgaras G.

Edgaras is a veteran server administrator at Hostinger. He makes sure that every server runs at full throttle and has all the latest technological advancements. When he's not working, Edgaras enjoys skiing and exploring the world.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

[href]
[href]
More in VPS
How to install and use Nano text editor
How to Install and Use Nano Text Editor

Close