Tutorials

Install a LAMP Stack on CentOS 7

Table of Contents

Introduction

A "LAMP stack" is a combination of operating system and software which enables you to host websites and web apps on your server.

The term "LAMP" stands for Linux, Apache, MySQL/MariaDB and PHP.

  • Apache: Runs the web server
  • MySQL/MariaDB: Stores website data
  • PHP: Processes web content

In this tutorial, we will install a LAMP stack on a CentOS 7 server.

Requirements

  • A server running CentOS 7
  • A static IP Address for your server

Install Apache Web Server

You can install Apache easily using the CentOS package manager. To do this, run:

sudo yum install httpd

Once installation has finished, you will need to start Apache service and enable it to start on boot. To do this, run:

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

Next you will need to allow the default Apache port 80 (HTTP) and 443 (HTTPS) using firewalld. You can do this by running the following commands:

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp

Reload the firewall service for the changes to take effect.

sudo firewall-cmd --reload

You can test the Apache service using your browser by visiting http://server-ip-address You will see the default CentOS 7 Apache web page, It should look something like this:

alt text

Install MariaDB

MariaDB is a drop-in replacement for MySQL. It is easy to install, offers many speed and performance improvements, and is easy to integrate into most MySQL deployments. MariaDB offers more storage engines than MySQL, including Cassandra, XtraDB and OQGRAPH

You can install it by running the following command:

sudo yum install mariadb-server mariadb

Once the installation is complete, you will need to start MariaDB by running the following command:

sudo systemctl start mariadb

Be sure that MariaDB starts at boot:

sudo systemctl enable mariadb

Now, you will need to secure the MariaDB installation. You can do this by running:

sudo mysql_secure_installation

Answer all the questions shown as below:

  • Enter current password for root (enter for none): currentrootpasswd
  • Set root password? [Y/n]: Press Enter
  • New password: rootsqlpasswd
  • Re-enter new password: rootsqlpasswd
  • Remove anonymous users? [Y/n]: Press Enter
  • Disallow root login remotely? [Y/n]: Press Enter
  • Remove test database and access to it? [Y/n] : Press Enter
  • Reload privilege tables now? [Y/n] : Press Enter

Your MySQL installation should now be secure.

Finally, use the following command to enable the MariaDB service to start on boot:

sudo systemctl enable mariadb.service

Install PHP

PHP is an open source web scripting language which is used to build dynamic web pages.

To install PHP and related modules, use the following command:

sudo yum install php php-mysql php-gd

Once PHP is installed, restart the Apache server:

sudo systemctl restart http.service

You can test your PHP installation by creating a PHP info page. Create the page with the command:

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

Add the following content:

<?php
phpinfo();
?>

Save and exit the file. Switch to a web browser and visit the URL http://server-ip-address/info.php, You should see a default PHP information page similar to this one:

alt text

Finally, it is a good idea to remove the PHP info page after you are done with it, because it exposes a lot of information about your system.

To do this, run:

sudo rm -rf /var/www/html/info.php

Conclusion

Congratulations! You have successfully installed a LAMP stack on your server. Now, you can easily host many kinds of websites on your server.

Feel free to leave me a comment if you have any question.

 
  • PHP section has a typo in it - line below

    sudo systemctl restart http.service - should be this

    sudo systemctl restart httpd-service

  • I would instead recommend you to try out Debian OS for setting up LAMP based stack. Debian is quite stable and has timely patches. I have been using Debian OS on my EC2 server because my PHP website hosting provider, Cloudways, has Debian in its stack. The stack is similar to LAMP but it has both Apache and Nginx.

Log In, Add a Comment