Today we’re exploring LAMP. Nope, not the thing in the corner of your room providing decorative illumination, but the software stack comprising of Linux, Apache, MySQL (or increasingly MariaDB) and PHP (or Perl, or Python, etc). It’s tried and tested and used on a large quantity of web servers on the internet.
This time the aim is to show you how easy it is to take a basic CentOS 7 and get it up and running to serve a PHP based website using the combination of Apache, MariaDB and PHP. For this guide, we are going to assume you have a dedicated server, or VPS, running a bare CentOS 7 installation and are using a user account with sudo or root access.
For the first, optional, step we can install the nano text editor. If you are familiar with and would rather use vi, then feel free to swap the nano sections of the guide for vi. If you don’t know what I’m talking about then you probably want to install nano for use.
sudo yum install nano
What is MariaDB?
With that step taken care of, the next thing to do is to install MariaDB. For those who are unfamiliar, MariaDB is a fork of MySQL made by the original MySQL developers after the acquisition of Sun Microsystems (then owner of MySQL) by Oracle. The aim of MariaDB is to remain compatible with MySQL in order to be a drop in replacement. With concerns that Oracle would be moving MySQL to a closed source project, many Linux distributions have moved to offering MariaDB instead of MySQL in their standard package, CentOS is one of those distributions.
Installation is as simple as:
sudo yum install mariadb-server mariadb
Once installed, next we need to ensure it’s enabled to start up after the server reboots:
sudo systemctl enable mariadb.service
Finally we’ll start the MariaDB service running:
sudo systemctl start mariadb.service
Before you can use the database you need to create a root user account to manage it. This is done through a handy script:
This will ask you some questions, the main ones here are to set the root user password. After that you can safely answer Y to the other questions and MariaDB will be ready to go.
Once your database is configured the next step is to install the Apache web server, which is done with the following command:
sudo yum install httpd
Once installed, it also needs to be enabled and started:
sudo systemctl enable httpd.service
sudo systemctl start httpd.service
Now if you browse to your server’s IP address in a web browser you should see a CentOS test page for Apache. If not then the server’s firewall may be blocking you. The default CentOS firewall is now firewalld and you can allow traffic to your web browser through it with the following commands:
sudo firewall-cmd --permanent --zone-public --add-service=http
sudo firewall-cmd --permanent --zone-public --add-service=https
sudo firewall-cmd --reload
If you try again now you should be able to see your the test web page.
The next step is to install PHP, which is again a simple task from the repositories:
sudo yum install php php-mysql
This will install PHP and the required module for PHP to be able to interface with MySQL/MariaDB. Once installed, we’ll need to restart Apache so that it can make use of PHP:
sudo systemctl restart httpd.service
Now to test that PHP and Apache are working well together we’ll create a test file.
sudo nano /var/www/html/info.php
Paste in the following text:
Save and exit the file, then go back to your browser window where you previously brought up the test page, and append “/info.php” to the end of the server’s IP address. You should then get a PHP branded page with information about the installed version of PHP.
If that last step worked as expected then you are ready to go and start setting up your chosen website software on your server.