LAMP Stack is a group of open source programs that work together to enable a server to host dynamic websites or web applications. LAMP represents Linux (operating system), Apache (web server), MySQL (MariaDB database), PHP (scripting language). In this guide, we will learn how to install LAMP on CentOS 7.
Prerequisites
Update the software packages of your CentOS Server to the latest version. To do that run the following command:
sudo yum -y update
This tutorials requires you to have root
access of your server. If you are working on Windows, you will require an SSH client like PuTTY.
How to Install LAMP on CentOS
Now that Linux (CentOS) is already installed and software packages are updated to the latest version, we will be installing Nginx, MySQL and PHP.
Step #1: Install Apache on CentOS 7
Apache web server is the most popular web server in the world. Most importantly, it is a great choice for hosting a website.
We can easily install Apache web server using the yum
package manger of CentOS.
sudo yum install httpd
Since we are using a sudo
command, the install command will get executed with root privileges and you will be asked to enter the regular user’s password.
Once it installs, you can start the Apache web server on your CentOS VPS by running:
sudo systemctl start httpd.service
Apache web server should be up and running on your server. You can check the status of your Nginx web server by typing:
sudo systemctl status httpd
Now, we would want our Apache to start automatically on server boot. To enable that, run:
sudo systemctl enable httpd.service
Now enter the IP address of your server in the browser and press enter. You should see a page something like this:
If you do not know the IP address of your server, you can use a tool called iproute2
.
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Alternatively, you can type:
curl http://icanhazip.com
Step #2: Installing MySQL (MariaDB)
Now that we have installed Apache web server up and running, it’s time to install MySQL (MariaDB).
MariaDB is a community-developed fork of MySQL database. It is a commercially supported and intended to remain free and open-source software under GNU General Public Licence.
To install MariaDB server along with MySQL/MariaDB-PHP, run:
sudo yum install mariadb-server php-mysql
Now enable and start the MariaDB service by typing:
sudo systemctl start mariadb sudo systemctl enable mariadb
In this step, we need to secure the MariaDB installation by running:
sudo mysql_secure_installation
Once you hit enter, a prompt will ask you to enter the current root password, which you do not have since you just installed the MariaDB. So you can leave the password field blank and press enter.
Now the prompt will show you a password confirmation option. You need to type Y and press enter and follow the instructions to set the password.
Now prompt will return a series of security questions. Answer Y for the following prompts:
Remove anonymous users? [Y/n] Disallow root login remotely? [Y/n] Remove test database and access to it? [Y/n] Reload privilege tables now? [Y/n]
We also need to enable the MariaDB so that it starts automatically every time a server is rebooted. To do that, run:
sudo systemctl enable mariadb
Step #3: Install PHP
To run our website or web application and display dynamic content, install the PHP FastCGI processing manager.
We are alsogoing to install php-mysql
and php-fpm
packages by leveraging the CentOS package manager yum
:
sudo yum install php php-mysql php-fpm
Ensure that PHP is up and running:
sudo systemctl start php-fpm
Also, enable php-fpm
to start the automatically on server reboot:
sudo systemctl enable php-fpm
Step #4: Test PHP Processing
In order to test our LAMP Stack configuration, create an index.php
file inside the root directory of your website example.com
.
sudo nano /var/www/example.com/index.php
Paste the following one line code:
Save the index.php
file by pressing CTRL + X.
Now restart the PHP-FPM and Apache:
sudo systemctl restart php-fpm sudo systemctl restart httpd.service
Now go to http://example.com/index.php
.
Replace example.com with your domain name.
It will open a page containing PHP information which will look something like this:
It also confirms the successful installation of LAMP Stack.
You may also like: How to Install LEMP on CentOS 7: 5 Easy Steps
Conclusion
In this guide, we covered how to install LAMP on CentOS 7. I hope you will find this tutorial useful.
If you have any questions or feedback, feel free to comment below.