Hello Linux lovers. Welcome to another tutorial of CentOS 8. In this article, I am going to build a web server on my CentOS 8. I am going to use the LAMP Stack. LAMP is a short name for Linux, Apache, MySQL and PHP. A Great combination for a web server. Basically, you can build your own web server with any Linux distribution. I put links to some tutorials below.
CenTOS is a free, powerful Linux distribution that suitable for bot desktop and server use. Do not get confused with term Desktop and Server. You still be able to install any Server application on the CentOS Desktop. It’s going to be a pretty long article. Grab your coffee and let’s start.
Steps to Install Web Server on CentOS 8
Step 1. Update your system
It is important to have an updated system. Before doing this installation, we need to update CentOS first.
sudo dnf update
Maybe it is necessary to reboot your system.
Step 2. Install Apache Web Server
Now, let’s install the Apache web server.
sudo yum install httpd httpd-tools
After install, we need to start and enable the Apache web server on startup.
sudo systemctl start httpd
sudo systemctl enable httpd
Now check the service status and make sure it is running and active as follow
Change Firewall Settings
By default, CentOS firewall blocks the remote access to port 80 (HTTP) and 443 (HTTPS). So, we need to add the exception.
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo systemctl reload firewalld
Finally, we need to make “apache” user as the owner of the web directory (/var/www/html)
chown apache:apache /var/www/html -R
Test the web server
In case you want to test your web server, you can do the following. Create a new file called index.html in the /var/www/html folder.
echo “Welcome to this site!” > /var/www/html/index.html
Now open your web browser and type: localhost or 127.0.0.1 in the address bar. You should see the following message.
At this point, the Apache web server is up and running. Get ready for the next steps.
Step 2. Install MySQL Server (MariaDB) on CentOS 8
Now we are going to install MariaDB Server. MariaDB is a fork of MySQL Server.
sudo yum install mariadb-server mariadb -y
Next, do the following to enable and start the database server
sudo systemctl start mariadb
sudo systemctl enable mariadb
Don’t forget to check the Mariadb service status
sudo systemctl status mariadb
Next, we need to configure the database
You will need to answer some questions
[root@localhost Downloads]# sudo 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] n … skipping. 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!
Step 4. Install PHP on CentOS 8
The following step we are going to install PHP on CentOS 8. Use this command to start
yum install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring -y
And then, we need to start the php-fpm
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Now restart the Apache web server
sudo systemctl restart httpd
We need to tell the SELinux to allow Apache to execute PHP code via PHP-fpm
setsebool -P httpd_execmem 1
It is good to test the PHP and ensure it works properly. We need to create a new info.php in /var/www/html directory.
And then paste this line
<?php phpinfo(); ?>
Close and save the file. And now, open the web browser and type:
You should see something like this
That’s it. Basically now you should have a working web server with MySQL/MariaDB database back end.