How to install the LAMP stack on CentOS 8

H

LAMP is an acronym which comes from Linux, Apache, MySQL and PHP. This is the default stack for running most web applications on a CentOS 8 server.

To proceed with the installation of the LAMP Stack, you should first update all the packages. This can be accomplished by running:

dnf -y update

Next, we will install Apache, PHP and MariaDB (MySQL replacement):

dnf -y install httpd mariadb-server php php-common php-pecl-apcu \
php-cli php-pear php-pdo php-mysqlnd php-pgsql php-gd php-mbstring php-xml \
mod_ssl certbot

If you’re looking for a different PHP version, check our this guide on how to install PHP 7.3 or PHP 7.4 in CentOS 8. The guide is complementary to the one you’re following now.

Next, we’ll use the default webroot /var/www/html and add the necessary options so .htaccess files are respected.

echo '<Directory "/var/www/html/">'>/etc/httpd/conf.d/www.conf
echo '   DirectoryIndex index.html index.php'>>/etc/httpd/conf.d/www.conf
echo '   Options FollowSymLinks'>>/etc/httpd/conf.d/www.conf
echo '   AllowOverride All'>>/etc/httpd/conf.d/www.conf
echo '   Require all granted'>>/etc/httpd/conf.d/www.conf
echo '</Directory>'>>/etc/httpd/conf.d/www.conf

Now, we will start the Apache and MariaDB services and make sure they also start on boot:

systemctl restart mariadb
systemctl enable mariadb
systemctl restart httpd
systemctl enable httpd

Next, we secure the MariaDB installation. This can be done by either running the mysql_secure_installation utility:

mysql_secure_installation

Or by running the following script which does most work for you and will print your automatically generated MariaDB root password:

echo "DELETE FROM mysql.user WHERE User='';"|mysql
echo "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', \
 '127.0.0.1', '::1');"|mysql
echo "DROP DATABASE IF EXISTS test;"|mysql
echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"|mysql
pass=$(head -c 16  /dev/random | md5sum | cut -f 1 -d\ )
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY '$pass';"|mysql
echo "[client]">/root/.my.cnf
echo "user=root">> /root/.my.cnf
echo "password=$pass" >> /root/.my.cnf
echo "The MariaDB password is $pass"

Next we allow the ports 80 (http) and 443 (https) in the firewall:

firewall-cmd --add-port=80/tcp --zone=public --permanent
firewall-cmd --add-port=443/tcp --zone=public --permanent
firewall-cmd --reload

And we are done. You should have a fully functioning LAMP stack installed on your CentOS 8 VPS.

To test it, run:

echo "<?php phpinfo();" > /var/www/html/index.php

And access the IP in your browser. You should see a page with a lot of information regarding your php installation. Keep in mind that your webroot is /var/www/html/

You should consider next setting up a SSL certificate for your domain name. This can be easily done for free by requesting a certificate from Let’s encrypt using the certbot utility:

certbot certonly -d yourdomain.com --webroot --webroot-path=/var/www/html

Follow up the certbot utility wizard and in the end you’ll be provided with 2 files: the certificate and the key. To set them up globally for apache, edit /etc/httpd/conf.d/ssl.conf and, the certificate file path under SSLCertificateFile and the key under SSLCertificateKeyFile and restart apache.

Keep in mind that the let’s encrypt certificates are valid only for 90 days, after which they need to be renewed by running:

certbot renew && systemctl restart httpd

Last, but not least, you should consider setting up the automatic updates to ensure your server is always updated, if you do not do it manually very often. A full guide can be found here on how to enable the automatic updates in CentOS 8 .

Recent Posts

Archives

Categories