In this article, we will see how to install LAMP in CentOS 8 step by step, so that you will have this Apache, PHP and MariaDB / MySQL stack available for the applications that require it on your server or CentOS VPS .
A LAMP stack or environment is based on a Linux system with Apache web server, the PHP server-side language, and MySQL or MariaDB database engine. In this guide, we will install these components, but you have specific guides for each of them on this same site.
How to install LAMP on CentOS 8
To install a LAMP stack on CentOS 8 we will use the packages available in the distribution repositories, so the first task is to update the system:
1 |
sudo yum update -y |
You have two possibilities when installing the LAMP stack, depending on whether you prefer to use MySQL or MariaDB as a database service.
We will start by installing the common packages corresponding to the Apache web server, PHP and the PHP extension for connecting to databases. We will install these packages with yum :
1 |
sudo yum install -y httpd php php-mysqlnd |
The httpd.service and php-fpm.service services are created that are not started or enabled to automatically start alongside CentOS 8.
We will start the services to make them available immediately:
1 |
sudo systemctl start php-fpm httpd |
And if we want the services to be continuously available, we must enable them for automatic start with the system:
1 |
sudo systemctl enable php-fpm httpd |
Database Engine Installation
If you prefer to use MySQL Server 8 install the mysql-server package :
1 |
sudo yum install -y mysql-server |
The service that is created is mysqld.service , we proceed to start it and enable it:
1 2 |
sudo systemctl start mysqld sudo systemctl enable mysqld |
Instead, if you want to choose to use MariaDB Server 10.3 install the mariadb-server package :
1 |
sudo yum install -y mariadb-server |
The mariadb.service service is created , which we will have to start and enable:
1 |
sudo systemctl enable mariadb |
Setting up the CentOS 8 firewall
If you want the web server to be accessible from the network you must provide a rule or exception for the firewall:
1 |
sudo firewall-cmd --permanent --add-service = {http, https} |
If you only want to offer the HTTP service then you can only specify that protocol:
1 |
sudo firewall-cmd --permanent --add-service = http |
You have to reload the firewall settings to apply the changes:
1 |
sudo firewall-cmd --reload |
Testing the service
To test the integration of the web server with PHP, we will create a test script in this language:
1 |
sudo nano /var/www/html/info.php |
And we will put this content:
1 |
<? phpinfo (); ?> |
We access through a browser, indicating the server address and the /info.php path (in this example, http: //centos8.local/info.php )
How to configure LAMP services in CentOS 8
The services are already configured by default, but it is necessary to make some minimum and quick adjustments.
Configure PHP
The main configuration file is php.ini that we must edit:
1 |
sudo nano /etc/php.ini |
We look for the directive date.timezone :
1 2 3 4 5 6 7 |
... [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone ; date.timezone = ... |
It is deactivated, we must give it the value of the time zone that interests us:
1 |
date.timezone = Asia/Karachi |
You can check the possible values for this directive at http://php.net/date.timezone .
If your LAMP CentOS 8 server is for production and publicly exposed operation we are done, but if it is for development you need to configure some error handling settings.
Look for the following directives:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
... error_reporting = E_ALL & ~ E_DEPRECATED & ~ E_STRICT ... display_errors = Off ... display_startup_errors = Off ... |
And change the appropriate values for development:
1 2 3 4 5 6 7 |
... error_reporting = E_ALL ... display_errors = On ... display_startup_errors = On ... |
After making any changes to the configuration of the php.ini file, it is necessary to reload the php-fpm service :
1 |
sudo systemctl reload php-fpm |
Configure MariaDB / MySQL
Either database service creates a root account without a password, which is a huge security problem if it is not fixed.
You can pass the interactive mysql_secure_installation script that, among other things, will allow you to create a password for root .
But it also allows you to delete test databases, anonymous users and eliminate remote access to root .
The script is launched the same for both database systems:
1 |
sudo mysql_secure_installation |
But it has some peculiarities for each system.
In MySQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y | Y for Yes, any other key for No: n Please set the password for root here. New password: Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y | Y for Yes, any other key for No): 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? (Press y | Y for Yes, any other key for No): y Success. By default, MySQL 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? (Press y | Y for Yes, any other key for No): 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? (Press y | Y for Yes, any other key for No): y Success. All done! |
In MariaDB:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
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 authorization. Set root password? [Y / n] and 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] and ... Success! Normally, root should only be allowed to connect from 'localhost'. Este ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y / n] and ... 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] and - 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] and ... 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! |
Conclusion
Now that you know how to install a LAMP stack in CentOS 8, you can now support applications and CMS that require it, or carry out your own advanced web developments.