How to Install Nginx, MySQL, and PHP on CentOS 7: A Step-by-Step Guide

Harsha Nanayakkara
6 min readMay 1, 2023

In this article, I’ll guide you through the process of installing latest Nginx, MySQL, and PHP on CentOS 7. We’ll cover how to find the latest download links, set up the repository, download and install using the yum command.

Introduction

If you’re looking to set up a web server on CentOS 7, you’ll need to install Nginx, MySQL and PHP. These three components are the foundation of a web server and are essential for hosting dynamic websites. While the LEMP stack installs all three components at once, installing them individually provides greater flexibility and control over your server’s configuration.

Installing Nginx

Nginx is a lightweight and high-performance web server that’s known for its speed and scalability. Here’s how to install it on CentOS 7:

Step 1: Check the latest download link

The first step is to check the latest download link for Nginx. You can find this on the Official Nginx website.

Link: https://nginx.org/en/linux_packages.html#RHEL

Step 2: Set up the repository

Next, we need to set up the Nginx repository on the CentOS 7 server. We can do this by creating a new file in the /etc/yum.repos.d/ directory, like so:

$ sudo vi /etc/yum.repos.d/nginx.repo

Then add the following content to the file, Save and close the file.

(Only the stable nginx package repository is used here. But if you’d like to use mainline nginx packages follow the instructions here.

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

Step 3: Install Nginx using the YUM command

Now, you can install Nginx by running the following command:

$ sudo yum install nginx -y

Step 4: Start the Nginx service and enable it to start on boot

Once the installation is complete, start the Nginx service and enable it to start on boot

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Installing MySQL

MySQL is a popular open-source database management system that’s widely used in web applications. Here’s how to install it on CentOS 7.

Step 1: Check the latest download link and download

To install MySQL, you’ll need to first check the latest download link on the MySQL website.

Link: https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm

$ wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm

Note: Under Downloads (https://dev.mysql.com/downloads/) there are 02 sections as shown below. We are using the link in MySQL Yum Repository since it will install the MySQL Yum repository, which will allow you to install and update MySQL Community Server using the Yum package manager. If you use MySQL Community Server, you will directly download and install a specific version of MySQL Community Server. However, you may need to manually install any dependencies that are required by the package.

MySQL Community Download Links

Step 2: Install MySQL Yum Repository Package on CentOS 7

Now, the package can be installed and the MySQL Yum repository will be configured on your system. This will enable you to install and update MySQL Community Server and related packages using Yum package manager.

Using rpm:

$ sudo rpm -ivh mysql80-community-release-el7-7.noarch.rpm

OR using yum localinstall:

$ sudo yum localinstall mysql80-community-release-el7-7.noarch.rpm

You’ll see the following repo files are added inside /etc/yum.repos.d/ once the above command is executed.

MySQL repo list

Step 3: Install MySQL using the YUM command

Now, you can install MySQL by running the following command:

$ sudo yum install mysql-community-server -y

Step 4: Start the MySQL service and enable it to start on boot

Once the installation is complete, start the MySQL service and enable it to start on boot.

$ sudo systemctl start mysqld
$ sudo systemctl enable mysqld

Step 5: Get the default root password

In MySQL 8.0 and later versions, the default root password is randomly generated during installation and stored in the error log file. You can view the error log file using the following command:

$ sudo grep 'temporary password' /var/log/mysqld.log

Step 6: Secure MySQL installation

This step is important because it helps to secure our MySQL installation by.

$ sudo mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:
Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Step 7: Create DB user and grant permissions

It is always a good practice to have separate users for MySQL with applicable permissions. Login to MySQL server as root user with the changed password.

$ mysql -u root -p
Enter password:
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'P@55w0rd#234';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
mysql> FLUSH PRIVILEGES;

Note: Privileges should be carefully decided. For an example, here newuser can do anything on all databases and tables for demo purposes.

Installing PHP

PHP, which stands for Hypertext Preprocessor, is an open-source, server-side scripting language widely used for web development. It is a popular choice among web developers due to its simplicity, ease of use and wide availability of resources and support. The easiest way to install PHP is by using Remi Repository.

Step 1: Enable the EPEL repository

The EPEL (Extra Packages for Enterprise Linux) repository provides additional packages for CentOS, which are not included in the default CentOS repositories. The Remi repository depends on some packages that are available in EPEL, which is why we need to install EPEL before installing the Remi repository. yum-utils is also required to run some yum commands.

$ sudo yum install epel-release -y
$ sudo yum install yum-utils -y

Step 2: Install and Enable the Remi repository

This repo can be downloaded from Official Remi’s RPM Repository (https://rpms.remirepo.net/) under Distribution Choice — Repository content as below image. I selected the highlighted one for CentOS 7.

$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Now, since we are going to install latest PHP version (i.e. 8.2 as of the time of writing) the respective repository should be enabled. For any other version, respective repository has to be enabled as required.

$ sudo yum-config-manager --enable remi-php82

Step 3: Install PHP 8.2 and some of the most common PHP modules

$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

In conclusion, installing and configuring Nginx, MySQL and PHP on CentOS 7 requires a few steps but can be easily achieved by following the instructions and commands provided in this guide.

I sincerely hope this article will be helpful. Moreover, I highly value your feedback and support!

Thank you for reading and stay safe!

--

--

Harsha Nanayakkara

An enthusiastic autodidact who is passionate to gain and freely share knowledge. I would really appreciate your feedback and support!