How to Install Apache, PHP, and MySQL on a CentOS 7 Cloud Server?

18-01-2024 02:21:27

In this article, we will learn how to install a LAMP environment, which comprises Apache, PHP, and MySQL, on a CentOS 7 cloud server. The installation process is divided into the following steps.

Updating the Operating System

Before installation, we update the operating system to the latest version and reboot the cloud server.

yum update -y
reboot

Installing Apache

To install Apache, use the following command:

yum install httpd -y
systemctl start httpd

To check the version of Apache, use the following command:

httpd -v

The result will be displayed as follows:

Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18

To allow access to port 80, configure firewalld with this setting:

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

Additionally, set Apache to start automatically with the system:

systemctl enable httpd

If the default Apache page appears when accessing the cloud server's IP address http://IPAddress via a browser, it indicates that Apache has been successfully installed.

Installing PHP

Install PHP using the following command:

yum install php -y

Next, install the necessary PHP extensions:

yum install php-{bcmath,intl,gd,mcrypt,mbstring,mysql,fpm} -y

To check the version of PHP, use this command:

php -v

The result will be displayed as follows:

PHP 5.4.16 (cli) (built: Nov  1 2019 16:04:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

To view the installed PHP modules, use this command:

php -m

The result will be displayed as follows:

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
intl
json
libxml
mbstring
mhash
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]

Installing MySQL

Install MySQL using the following command:

yum install mariadb-server -y
systemctl start mariadb.service

To check the MySQL version, use the following command:

mysql -V

The result will be displayed as follows:

mysql  Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1

Then, run the MySQL secure installation wizard:

mysql_secure_installation

In the MySQL secure installation wizard, you will need to enter the root password and answer some security-related questions. Typically, the default values provided by the wizard are sufficient.

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]
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]
 ... 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]
 ... 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]
 - 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]
 ... 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!

Also, set MySQL to start automatically with the system:

systemctl enable mariadb.service

Finally, restart Apache to work in conjunction with PHP:

systemctl restart httpd

This concludes the tutorial on installing Apache, PHP, and MySQL on a CentOS 7 cloud server.