This Knowledgebase article details steps on how to install LAMP stack on CentOS & RHEL OS Flavours.
What’s LAMP Stack?
A software stack is a set of software tools bundled together. LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of which are open source. It is the most common software stack that powers dynamic websites and web applications. Linux is the operating system; Apache is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.
Step 1: Update Software Packages
Before we install the LAMP stack, it’s a good idea to run the following command to update repository and software packages.
dnf update
Step 2: Install Apache Web Server on CentOS/RHEL
Enter the following command to install Apache Web server. The httpd-tools package will install some useful utilities like Apache HTTP server benchmarking tool (ab).
dnf install httpd httpd-tools
After it’s installed, we can start Apache with this command:
systemctl start httpd
Enable Apache to auto start at system boot time by running the following command.
systemctl enable httpd
Now check its status.
systemctl status httpd
Output:
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 06:43:15 UTC; 14s ago
Docs: man:httpd.service(8)
Main PID: 14515 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 5092)
Memory: 24.8M
CGroup: /system.slice/httpd.service
├─14515 /usr/sbin/httpd -DFOREGROUND
├─14516 /usr/sbin/httpd -DFOREGROUND
├─14517 /usr/sbin/httpd -DFOREGROUND
├─14518 /usr/sbin/httpd -DFOREGROUND
└─14519 /usr/sbin/httpd -DFOREGROUND
“Enabled” indicates that auto start at boot time is enabled and we can see that Apache is running.
Check Apache version.
httpd -v
Output:
Server version: Apache/2.4.37 (centos)
Server built: Oct 7 2019 21:42:02
By default, CentOS/RHEL forbids public access to port 80. To allow other computers to access the web page, we need to open port 80 in firewalld, the dynamic firewall manager on RHEL/CentOS. Run the following command to open port 80.
firewall-cmd --permanent --zone=public --add-service=http
If you want to enable HTTPS on Apache later, then you also need to open port 443.
firewall-cmd --permanent --zone=public --add-service=https
The --permanent option will make this firewall rule persistent across system reboots. Next, reload the firewall daemon for the change to take effect.
systemctl reload firewalld
Now the Apache web page is accessible publicly.
We need to make user apache as the owner of web directory. By default it’s owned by the root user.
chown apache:apache /var/www/html -R
By default, Apache uses the system hostname as its global ServerName. If the system hostname can’t be resolved in DNS, then you will probably see the following error after running sudo apachectl configtest
command.
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
To solve this problem, we can set a global ServerName in Apache. Install the Nano command-line text editor and use it to create a new configuration file.
sudo dnf install nano
sudo nano /etc/httpd/conf.d/servername.conf
Add the following line in this file.
ServerName localhost
Save and close the file. To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X. Reload Apache for the change to take effect.
sudo systemctl reload httpd
Now if you run the sudo apachectl configtest
command again, you won’t see the above error message.
Step 3: Install MariaDB Database Server on CentOS/RHEL
MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on CentOS/RHEL.
dnf install mariadb-server mariadb -y
After it’s installed, we need to start it.
systemctl start mariadb
Enable auto start at system boot time.
systemctl enable mariadb
Check status:
systemctl status mariadb
Output:
● mariadb.service - MariaDB 10.3 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 09:02:53 UTC; 33s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 18608 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 30 (limit: 5092)
Memory: 77.0M
CGroup: /system.slice/mariadb.service
└─18608 /usr/libexec/mysqld --basedir=/usr
“Enabled” indicates that auto start at boot time is enabled and we can see that MariaDB server is running. Now we need to run the security script.
mysql_secure_installation
When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.
<image1>
Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Note: The letter Y is capitalized, which means it’s the default answer.)
<image2>
Now you can run the following command and enter MariaDB root password to log into MariaDB shell.
mysql -u root -p
To exit, run
exit;
Step 4: Install PHP on CentOS/RHEL
Install PHP and some common modules using the following command.
dnf install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring -y
Apache web server on CentOS/RHEL by default uses PHP-FPM instead of mod_php to run PHP code, so in the above command we also installed php-fpm. After it’s installed, we need to start it.
systemctl start php-fpm
Enable auto start at system boot time.
systemctl enable php-fpm
Check status:
systemctl status php-fpm
Output:
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 09:54:37 UTC; 3s ago
Main PID: 19755 (php-fpm)
Status: "Ready to handle connections"
Tasks: 6 (limit: 5092)
Memory: 24.5M
CGroup: /system.slice/php-fpm.service
├─19755 php-fpm: master process (/etc/php-fpm.conf)
├─19757 php-fpm: pool www
├─19758 php-fpm: pool www
├─19759 php-fpm: pool www
├─19760 php-fpm: pool www
└─19761 php-fpm: pool www
“Enabled” indicates that auto start at boot time is enabled and we can see that PHP-FPM is running. The php-fpm package installs a php.conf file in /etc/httpd/conf.d/ directory, so we need to restart Apache web server, in order to run PHP code.
systemctl restart httpd
We also need to run the following command to tell SELinux to allow Apache to execute PHP code via PHP-FPM.
setsebool -P httpd_execmem 1
Congratulations! You have successfully installed Apache, MariaDB and PHP7.2 on RHEL or CentOS.