The LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP. It is an acronym for a Linux/UNIX server with Apache as the server engine, MySQL/MariaDB as the database engine, and PHP as the primary server-side script language. It is also known as LAMP Stack. Usually, Lamp Stack is selected for the construction and development of high-performance websites owing to the strong foundation it has.
Advantages of using Lamp stack in web development?
- Application and websites built with the lamp will have scalable features.
- Modules of Lamp Stack are customizable.
- Lamp Stack can work on a wide range of operating systems.
- Lamp Stack has obtained an impervious structure and high-end encryption for assuring security.
This step by step guide will help you to install/ configure the LAMP stack on CENTOS/RHEL 7.6 but should work for all versions of CENTOS /RHEL with minimal changes.
My Lab Setup :
Step 1: Check the server hostname, IP address
Note: In addition to the LAMP package (Linux, Apache, MySQL/MariaDB, and PHP), we need to install PhpMyAdmin. It is a free open source web interface tool, used to manage the MySQL/MariaDB database. By default, this package is not found in the official CentOS repositories. You can get it the same from the EPEL repository. It provides useful software packages that are not included in the official CentOS or Red Hat repositories.
Step 2 : Install the EPEL repository
Use the following command to install EPEL Repository
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
# yum -y install epel-release
Step 3: Install Apache Server
Use the following command to install Apache web server
# yum install httpd -y
Use the following command to Start httpd service and enable it to start automatically after every reboot
# systemctl enable httpd
# systemctl start httpd
# systemctl status httpd
Step 4: Add a Firewall rule
Use the following command to Open firewall ports for HTTP (80) and HTTPS (443) services.
# firewall-cmd --permanent --add-port=80/tcp
# firewall-cmd --permanent --add-port=443/tcp
# firewall-cmd --reload
To list all the applicable information for the default zone
# firewall-cmd --list-all
Step 5: Test the default Apache page in Browser and Terminal
Now, Open your web browser and give your server IP address as like "http://192.168.0.100" and if you see the Apache test page as shown below, then it means your Apache server is operation and successfully installed.
Use the following command to check the webpage in Terminal
# elinks 192.168.0.100
Step 6: Install MySQL / MariaDB Server
I've chosen to use MariaDB right here as a substitute for MySQL. MariaDB is an open source relational database administration system (DBMS) that is a well-matched drop-in substitute for the extensively used MySQL database technology. MariaDB is used due to the fact it is fast, scalable and robust. MariaDB is developed as open source software program and as a relational database, it affords an SQL interface for getting get entry to data.
Note : If you prefer to use the MySQL branded database in CentOS/ RHEL 7, issuing the following command.
# yum install mysql mysql-server -y
Use the following command to Install MariaDB
# yum install mariadb-server mariadb -y
Use the following command to Start MariaDB service and enable it to start automatically after every reboot
# systemctl enable mariadb
# systemctl start mariadb
# systemctl status mariadb
Step 6.1: Securing MySQL-MariaDB
Now, it is time to do the security fix to MariaDB. This fix will be given the choice to change the MariaDB root password, get rid of anonymous user accounts, disable root logins backyard of localhost, and remove test databases and reload privileges. This can be executed using the below command :
Note: By issuing the below command, you will be prompted to enter your MariaDB root password, simply press Enter due to the fact you haven’t set any password yet. Please be aware that this should not be confused by the root password of your server. Once you have done this, you will be asked a sequence of questions. We have provided the solutions below for simplicity. It is recommended that you acknowledgment 'YES' to these options:
Step 6.2: Restart MariaDB
# systemctl restart mariadb
Your Database (MariaDB) is ready now, let us move on to the next session
Step 7: Install PHP
PHP is a general-purpose server-side scripting language. It can run scripts, connect to our MySQL databases to get information and hand the processed content over to our web server to display.
You can choose to install PHP-FPM. PHP-FPM is an alternative of Fast CGI daemon for PHP, it approves the websites to deal with loads. PHP-FPM continues pools (workers that can reply to PHP request) to accomplish this. PHP-FPM is faster than the usual CGI-based methods, because it does not over consume the system resources especially memory. (# yum install php php-fpm)
Here I choose the traditional PHP only.
Use the following command to Install PHP
# yum install php php-mysql -y
After installing PHP and the dependent packages of PHP modules, we need to restart Apache service in order to work the PHP.
# systemctl restart httpd
Finally, the LAMP stack is configured successfully. Now we can look at the versions of each individual package of the LAMP stack (like Apache, Mysql/MariaDB, and PHP) in the command line.
Step 7: LAMP stack version check in Command Line
# php -v
# httpd -v
# mysql -V
Step 7.1: Now we can Test the PHP Process via web (apache) server
This can be accomplished by creating a very fundamental PHP script (we can call it info.php). It has to be placed the default document root of the Apache server is i.e, /var/www/html in (RHEL/CENTOS 7) directory and call it in a browser like 'http://192.168.0.100/info.php'. The file will display a lot of useful data about our PHP installation. Use the code to create phpinfo as following :
# vi /var/www/html/info.php
Now, open your browser and enter 'http://192.168.0.100/info.php'
The PHP information web page comes precisely like the below screenshot. This page essentially gives you data about your server from the point of view of PHP. It is beneficial for debugging and to ensure that your settings are being utilized correctly.
It is adviced to remove this file after this check due to the fact it should definitely provide data about your server to unauthorized users.
Step 8: Install phpMyAdmin
PhpMyAdmin is a free open source web interface tool, used to manage the MySQL/MariaDB database.
Use the following command to Install phpMyAdmin
# yum install phpMyAdmin -y
Step 8.1: Configure phpMyAdmin
Once you have completed the installation of phpMyAdmin, you need to edit the configuration file of phpMyAdmin. This file can be located in /etc/httpd/conf.d/phpMyAdmin.conf
# vi /etc/httpd/conf.d/phpMyAdmin.conf
Follow the changes made below , Most importantly you need to add 'Require all granted', it will permit you to access the phpMyAdmin on network.
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin
# Apache 2.4
Require all granted
# Apache 2.2
Deny from All
Allow from 127.0.0.1
Allow from ::1
Note: save the file :wq!
Step 8.2: Set authentication type in phpMyAdmin
Next, we need to change the authentication type in phpMyAdmin. The phpMyAdmin comes with 4 types of authentication (config,cookie,http,signon). You can choose either one of them based on your requirements. The default one comes with 'cookie'.
Following are the differences :
|‘config’ authentication ($auth_type = 'config') is the plain historic way: username and password are saved in config.inc.php.|
|‘cookie’ authentication mode ($auth_type = 'cookie') permits you to log in as any correct MySQL user account with the support of cookies.|
|‘http’ authentication allows you to log in as any correct MySQL user account through HTTP-Auth.|
|‘signon’ authentication mode ($auth_type = 'signon') permits you to log in from prepared PHP session data or the usage of furnished PHP script.|
Use the following command to find/edit the file :
# vi /etc/phpMyAdmin/config.inc.php
## FIND this line and here I choose http auth type, change cookie to http as like below :
$cfg['Servers'][$i]['auth_type'] = 'http'; // Authentication method (config, http or cookie based)?
Note: save the file :wq!
Step 8.3: Restart httpd service
Finally restart the httpd service in order to access the phpMyAdmin via http.
# systemctl restart httpd
When getting access to your phpMyAdmin, you will be prompted to enter the login credentials of the user account you created earlier, here I have created only the MySQL root account and password, therefore, you can use the same credentials to access the phpMyAdmin in your testing LAB, but in real-time you need to create different accounts based on requirement. It is recommended to use different user accounts for the individual projects for staging and production.
After entering the basic authentication, you’ll be taken to the phpMyAdmin login page where you need to enter your MySQL/MariaDB administrative user login credentials.
Once you log in, you’ll see the phpMyAdmin dashboard, which will appear something like this:
Congratulations, your LAMP stack is now complete and you can run any type of dynamic website. Also you can get entry to your MySQL/MariaDB database via phpMyAdmin.
I believe this article will help you to setup LAMP Stack at your end. Drop me your feedback/comment.