LAMP stack is the combination of Linux, Apache web server, MySQL/MariaDB, PHP. LAMP stack is used to deploy web-based applications and host dynamic websites.

In this tutorial we will show you how to install LAMP Stack on CentOS 8.

Installing Apache

Apache is available in the default CentOS repositories.

# dnf install httpd

Enable and start the Apache service.

# systemctl enable httpd
# systemctl start httpd

Open your web browser and go to your server. http://server-ip/ you will see something like this.

Install MariaDB

MariaDB is a pretty good database manager. It is a MySQL fork and therefore compatible with it. It is one of the open-source values. Install MariaDB on CentOS 8 with the command.

# dnf install mariadb-server

Enable and start MariaDB server.

# systemctl start mariadb
# systemctl enable mariadb

Now it is necessary to define a password for the root user. In addition to this, the installation must be secured.

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

When you’re finished, log in to the MariaDB console.

# mysql -u root -p

Create a new database.

MariaDB [(none)]> CREATE DATABASE database_name;

Create a new user and grant them full privileges on the custom database.

MariaDB [(none)]> GRANT ALL PRIVILEGES ON database_name.* TO 'db_user'@'localhost' IDENTIFIED BY 'password';

Flush the privileges to ensure that they are saved and available in the current session:

MariaDB [(none)]> FLUSH PRIVILEGES;

Install PHP

By default, the Apache webserver supports the HTML language only. We will need to install the PHP.

# dnf install php php-common php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-gd php-mbstring php-xml

Creating Virtual Host 

By default, Apache is configured to load all configuration files that ends with .conf from the /etc/httpd/conf.d/ directory.

# nano /etc/httpd/conf.d/example.com.conf

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

DocumentRoot /var/www/example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined

</VirtualHost>

Create virtual host home folder.

# mkdir -p /var/www/example.com

Apache should be made the owner of that folder.

# chown -R apache: /var/www/example.com

To test the Apache configuration, run the following command.

# httpd -t
Syntax OK

Restart Apache service.

# systemctl restart httpd

Testing PHP Information

To test PHP with the web server, you’ll have to create a phpinfo file to the document root directory.

# echo "<?php phpinfo();" > /var/www/example.com/index.php

Now we can test our webserver. Go to your browser and access your server hostname. You should see your server’s PHP information.

This means PHP scripts can run properly with Apache web server. You have successfully install LAMP.