Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7


Introduction

CentOS 7 is a popular Linux distribution known for its stability and security. If you're looking to set up a web server on CentOS 7, you'll likely need to install Apache, MySQL or MariaDB, and PHP. In this comprehensive guide, we will walk you through the installation process of these components, along with examples and their outputs, to ensure a successful setup.

Prerequisites

Before we begin, make sure you have the following prerequisites in place −

CentOS 7 installed on your server or virtual machine.

Root access or sudo privileges to execute commands with administrative rights.

Update the System

To start, let's update the system to ensure that we have the latest packages and dependencies.

Open a terminal and execute the following command −

sudo yum update -y

Install Apache

Apache is a widely used web server software. To install Apache on CentOS 7, follow these steps −

Enter the following command to install Apache −

sudo yum install httpd -y

Once the installation is complete, start the Apache service −

sudo systemctl start httpd

To enable Apache to start automatically at boot time, run the following command −

sudo systemctl enable httpd

Verify if Apache is running by accessing your server's IP address or domain name in a web browser. You should see the Apache default page.

Install MySQL 8 or MariaDB 10

Next, we need to install a relational database management system. You can choose either MySQL 8 or MariaDB 10 based on your preference. Here, we'll cover both installations.

For MySQL 8 −

Execute the following command to install the MySQL repository −

sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y

Install MySQL 8 by running the following command −

sudo yum install mysql-server -y

Once the installation is complete, start the MySQL service −

sudo systemctl start mysqld

To secure your MySQL installation, run the security script −

sudo mysql_secure_installation

For MariaDB 10 −

Enter the following command to install MariaDB −

sudo yum install mariadb-server -y

Start the MariaDB service −

sudo systemctl start mariadb

Secure the MariaDB installation using the security script −

sudo mysql_secure_installation

Install PHP 7

PHP is a server-side scripting language required for dynamic web page development. To install PHP 7 on CentOS 7, follow these steps −

Execute the following command to install PHP and its extensions −

sudo yum install php php-mysqlnd -y

Once the installation is complete, restart the Apache service to enable PHP −

sudo systemctl restart httpd

To test if PHP is working correctly, create a PHP info file. Use the following command to create a file named info.php in the default web server directory −

sudo nano /var/www/html/info.php

In the editor, add the following PHP code −

<?php
   phpinfo();
?>

Save the file and exit the editor. Now, access http://your_server_IP_address/info.php in a web browser. You should see a PHP information page displaying detailed information about your PHP installation.

Configure PHP and Test the Installation

To further configure PHP, you can modify the PHP configuration file according to your needs. The configuration file for PHP on CentOS 7 is located at /etc/php.ini. You can use a text editor like nano or vi to open and edit the file.

For example, to increase the maximum file upload size, search for the upload_max_filesize directive in the php.ini file and modify it to your desired value. Save the changes and restart Apache for the modifications to take effect −

sudo systemctl restart httpd

Now, let's test the PHP installation by creating a simple PHP script. Create a new file called test.php in the default web server directory −

sudo nano /var/www/html/test.php

Add the following PHP code to the file −

<?php
   echo "PHP is working correctly!";
?>

Save the file and exit the editor. Now, access http://your_server_IP_address/test.php in a web browser. You should see the message "PHP is working correctly!" displayed on the page, confirming that PHP is installed and configured properly.

Verify MySQL or MariaDB Installation

To ensure that the MySQL 8 or MariaDB 10 installation was successful, you can perform a simple check.

For MySQL, execute the following command −

sudo systemctl status mysqld

If MySQL is running properly, you should see an output indicating that the service is active and running.

For MariaDB, run the following command −

sudo systemctl status mariadb

If MariaDB is running correctly, you will see the service status as active.

Connect to MySQL or MariaDB

To connect to MySQL or MariaDB from the command line, you can use the MySQL client utility. Open a terminal and enter the following command −

mysql -u root -p

You will be prompted to enter the root password for MySQL or MariaDB. Once authenticated, you will be in the MySQL or MariaDB command-line interface.

To verify the connection, you can execute a simple query. For example, let's show all the databases −

SHOW DATABASES;

This command will display a list of databases available in the MySQL or MariaDB server.

Conclusion

In this article, we have covered the step-by-step installation process of Apache, MySQL 8 or MariaDB 10, and PHP 7 on CentOS 7. We have also provided examples and outputs to help you understand the installation and configuration steps.

Updated on: 17-Jul-2023

565 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements