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?
- Applications 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 and 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 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
List all applicable information for the default zone. If you want to know more about firewalld services, have a look at this article on 15 basic useful firewall-cmd commands in Linux.
# 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 "http://192.168.0.100" and if you see the Apache test page as shown below, then it means your Apache server is operational 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 an open source software program and as a relational database, it affords an SQL interface for getting entry to data. Even performance wise MariaDB is better than MySQL, click here to learn more about MariaDB vs Mysql.
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 is the time to do a security fix for MariaDB. This fix will give you the option to change the MariaDB root password, remove anonymous user accounts, disable root logins in the 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 confusing due to 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. We recommend that you acknowledgment 'YES' for 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 to Fast CGI daemon for PHP, it approves the websites to deal with loads. PHP-FPM continues to pool (workers that can reply to PHP requests) 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.1: LAMP stack version check in Command Line
# php -v
# httpd -v
# mysql -V
Step 7.2: 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). The default document root of the Apache server has to be placed in the 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 follows :
# 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. This can be beneficial for debugging and to ensure that your settings are being utilized correctly.
It is advised 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 will need to edit the configuration file of phpMyAdmin. This file can be located at /etc/httpd/conf.d/phpMyAdmin.conf
# vi /etc/httpd/conf.d/phpMyAdmin.conf
Following the changes made below, most importantly you need to add 'Require all granted', it will permit you to access the phpMyAdmin on the 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, cookies, 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 requirements. It is recommended to use different user accounts for 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. You can also enter your MySQL/MariaDB database via phpMyAdmin.
I believe this article will help you set up the LAMP Stack on your end. Drop me your feedback/comments.