This guide provides a step-by-step walkthrough for installing an Apache web server on a CentOS 7 server with PHP and MySQL. This setup, commonly known as a LAMP stack, stands for Linux, Apache, MySQL, and PHP.
We’ll be installing the latest PHP versions (7.0, 7.1, 7.2, and 7.3) on a CentOS 7.6 server.
1. Preliminary Note
Throughout this tutorial, I will use the hostname server1.example.com
with the IP address 192.168.0.100
. Replace these with your actual settings where applicable.
First, we will add the EPEL repository to install the latest phpMyAdmin:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY* yum -y install epel-release
To edit files via the shell, I will install the nano text editor. If you prefer using vi, feel free to skip this step.
yum -y install nano
2. Installing MySQL / MariaDB
MariaDB is a fork of MySQL developed by the original MySQL developer, Monty Widenius. It is fully compatible, and we’ll use it in this guide. Install MariaDB with the following command:
yum -y install mariadb-server mariadb
Next, set up system startup links to ensure that MySQL starts automatically with the system:
systemctl start mariadb.service systemctl enable mariadb.service
Secure your MySQL installation by setting passwords and removing unnecessary user accounts:
mysql_secure_installation
[...]
3. Installing Apache
CentOS 7 includes Apache 2.4, which we can install using the following:
yum -y install httpd
Configure Apache to start at boot:
systemctl start httpd.service systemctl enable httpd.service
You’ll need to open HTTP (80) and HTTPS (443) ports in the firewall for remote access:
firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --reload
To verify your installation, access your server’s IP in a browser, e.g., http://192.168.0.100
and you should see the Apache placeholder page:
4. Installing PHP
The default PHP version in CentOS is quite outdated (PHP 5.4). We recommend installing newer versions (PHP 7.0 to 7.3) from the Remi repository:
Add the Remi repository:
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Additionally, install yum-utils:
yum -y install yum-utils
Update your yum repository:
yum update
Choose your desired PHP version:
4.1 Install PHP 5.4
For PHP 5.4:
yum -y install php
4.2 Install PHP 7.0
For PHP 7.0:
yum-config-manager --enable remi-php70 yum -y install php php-opcache
4.3 Install PHP 7.1
For PHP 7.1:
yum-config-manager --enable remi-php71 yum -y install php php-opcache
4.4 Install PHP 7.2
For PHP 7.2:
yum-config-manager --enable remi-php72 yum -y install php php-opcache
4.5 Install PHP 7.3
For PHP 7.3:
yum-config-manager --enable remi-php73 yum -y install php php-opcache
Restart Apache to apply changes:
systemctl restart httpd.service
5. Testing PHP
Create a PHP info file to test your PHP installation:
nano /var/www/html/info.php
<?php phpinfo(); ?>
Access it in your browser (e.g., http://192.168.0.100/info.php
):
Verify that PHP 7.3 is working correctly and identify the server handler.
6. Enabling MySQL Support
Install the php-mysqlnd
package for MySQL support:
yum -y install php-mysqlnd php-pdo
Install additional common PHP modules:
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel
Restart Apache once more:
systemctl restart httpd.service
Check http://192.168.0.100/info.php
again in your browser to confirm all modules are loaded. Remove the file afterward for security:
rm /var/www/html/info.php
7. Installing phpMyAdmin
Install phpMyAdmin to manage your MySQL databases via a web interface:
yum -y install phpMyAdmin
Edit the phpMyAdmin configuration to allow remote connections:
nano /etc/httpd/conf.d/phpMyAdmin.conf
[...]
Restart Apache to apply changes:
systemctl restart httpd.service
Access phpMyAdmin at http://192.168.0.100/phpmyadmin/
:
8. Useful Links
- Apache: http://httpd.apache.org/
- PHP: http://www.php.net/
- MySQL: http://www.mysql.com/
- CentOS: http://www.centos.org/
- phpMyAdmin: http://www.phpmyadmin.net/
FAQs
What is a LAMP stack?
A LAMP stack consists of Linux (OS), Apache (web server), MySQL (database), and PHP (programming language), which together create a powerful platform for web application development and deployment.
Why do we use MariaDB instead of MySQL?
MariaDB is a fork of MySQL and offers improvements in performance, reliability, and features, while maintaining compatibility with MySQL. It is often chosen for its enhancements and because it is open-source.
Why is it necessary to enable firewalld-cmd for HTTP and HTTPS services?
Enabling these services allows incoming traffic on the web server’s standard ports (80 for HTTP and 443 for HTTPS), which is necessary for accessing web applications externally.
How can I secure my PHP installation?
Keep your PHP version updated, disable unused modules, set proper permissions, and remove configuration files like info.php
once they are no longer needed.
What should I do if I cannot access phpMyAdmin remotely?
Ensure that your Apache configuration file for phpMyAdmin allows remote IPs. Also, check that your firewall rules allow incoming traffic on the necessary ports.