How To Install PHP 7, 7.2 & 7.3 On CentOS 7

Prerequisites

How to Install PHP 7.2 with Apache on CentOS

Step 1: Choose PHP Version to Install

The newest stable release version of PHP is PHP 7.3.1. However, some software repositories default to older versions of the software.
One advantage of using an older release is its high stability and reliability. Newer releases may include more features, but are often more experimental and could cause system instability. If you cannot decide which version is right for you, version 7.2 is a great place to start.

Step 2: Enable Additional Software Repositories

By default, the yum package manager does not include access to the repositories that store the PHP packages. Therefore, you need to enable access to these software packages.
1. First, start by installing the yum-utils package by entering the following command in a terminal window:

sudo yum install yum-utils –y

2. Then, enable the epel-release repository by entering the following:

sudo yum install epel-release –y

3. Finally, add the following software repositories that contain the PHP packages:

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

Step 3: Enable Repository For PHP Version

Next, use the yum-config-manager to point your installer to the repository of the PHP version you want to install:

sudo yum-config-manager ––enable remi–php70

This command configures your system to download and install PHP 7.0.
To install PHP 7.1, change the entry as follows:

sudo yum-config-manager ––enable remi–php71

Likewise, replace the last two characters with 72 to install PHP 7.2.
 

Step 4: Install PHP and its Dependencies

At this point, you can install PHP. Enter the following command in the terminal:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y

As you see, the line includes many of the modules and add-ons that help PHP integrate with your local server configuration.
The output will also indicate which dependencies have been installed.
 

Step 5: PHP Modules

You may have noticed that the installation script included more than just the base PHP package. Many of these are modules that provide basic functionality to PHP. Installing this set helps ensure that your PHP installation meets your expectations for usage.
Like many other Linux applications, you can enhance your system’s PHP functionality using modules.
To search for available modules and generate a list of modules, type in the following command:

sudo yum search php | more

Step 6: Verify PHP Version

Finally, verify your installation was successful. Check which version of PHP you are running with the command:

php –v

Optional: Install PHP 7.3

You can enable Remi’s PHP 7.3 repository the same way you enabled other repositories in Step 3. However, this installation may fail if you have extensions that are not compatible with PHP 7.3.
If this is a first-time installation and you are confident that your system is compatible, you can enable PHP 7.3. Install the release with the following commands:

sudo yum-config-manager ––enable remi–php73
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y

How To Install MySQL on CentOS 7

Prerequisites

Guide To Installing MySQL on CentOS 7

Step 1: Download Repository Packages

Open a browser window, and go to the following address:
https://dev.mysql.com/downloads/repo/yum/
This page lists MySQL setup packages in the Yum repository.
 
Find the Red Hat Enterprise Linux version that you want to download. (At the time of writing, the website offers Linux 8, Linux 7 and Linux 6.)
 
You can click the Download button, which takes you to a registration page. You can sign up if you’d like or select the No thanks, just start my download link.
 

Download MySQL with wget Command

Alternately, you can open a terminal and use the wget command to save the file. On the web page that lists release versions, you’ll see a gray subtext that shows something like “(mysql80-community-release-el7-1.noarch.rpm)”. That’s the setup package of a specific package.
Copy the name of the desired setup package, then open a terminal window and enter the following command:

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

The system should reach out and download the files. Make sure you check the website and copy the exact release ID – use that in your terminal command.

Leave the browser window open for the next step.

Step 2: Add the Software Repositories

The files we just downloaded provide access to the MySQL software repositories. Before adding them, use the md5sum command to authenticate the software:

sudo md5sum mysql80-community-release-el7-3.noarch.rpm

The system should respond with a long string of letters and numbers.
Switch back to the MySQL web page and look just below the Download link to find a gray string of numbers labeled MD5.
Compare the MD5 value on the web page to the MD5 value you generated in the terminal window. If they match, proceed to the next step.

If they don’t match, it’s possible that your download was corrupted in transit. Or, it’s possible that the download has been compromised. Repeat Steps 1 and 2 and overwrite the downloaded file. If the MD5 values still don’t match, stop the procedure.
To update the software repositories, use the command:

sudo rpm –ivh mysql80-community-release-el7-3.noarch.rpm

Make sure you’ve entered the release version from Step 1. This will add 2 new Yum repositories that we can get MySQL from.

Step 3: Install MySQL

Install MySQL on CentOS by entering the following:

sudo yum install mysql-server


The system will ask for confirmation, press Y to confirm.
It should also request that you accept a GPG (Gnu Privacy Guard) key. This is another security confirmation because we just added two new software sources. Press Y again, and the system should download and install the software.

Using MySQL

Managing MySQL Service

You’ll need to start the MySQL service by entering:

sudo systemctl start mysqld

To check the status of MySQL use the command:

sudo systemctl status mysqld

The system will display several lines of information about the MySQL service. In the Active line, it should display active: (running). You may also see a line below that shows a timestamp of when the service was started.

By default, the MySQL service is set to launch at startup.
To disable it, you can use the disable command:

sudo systemctl disable mysqld

To stop the MySQL service, use the stop command:

sudo systemctl stop mysqld

Find Temporary Password

The MySQL installation routine sets up a default temporary password.
Use the grep command to find the password:

sudo grep ‘temporary password’ /var/log/mysqld.log

Make a note of the password.

Configuring and Securing

Your new MySQL installation comes with a security script to make securing configurations easier.
Launch the script with the following terminal command:

sudo mysql_secure_installation

The system will prompt you to enter the default root password. Enter the password you recovered earlier.
Next, the system will tell you that the password has expired, and prompt you to enter a new one. Enter a new password, make a note of it, then press Enter.
The system will rate your password strength, and ask if you want to enter a new, stronger password. If you’re satisfied with your password strength, hit the spacebar. To revise your password, press Y.
The Secure Installation script will continue, and you can safely reply Y to the rest of the prompts, which include:

  • Remove anonymous users
  • Disallow remote root login
  • Remove test database
  • Reload privilege tables

Log into MySQL

To launch MySQL from the command line, use the command:

mysql –u root –p

The system will prompt you to enter your password. Enter the password you set in Step 6, and the system should respond by displaying the MySQL shell.

It lists necessary information about the MySQL software, and your command prompt will change to mysql> to indicate that you’re logged into the MySQL software.
Once you’re done, you can log out with:

exit

The command prompt will change, indicating that you’ve logged out of MySQL.

How To Install the Apache Web Server on CentOS 7

Prerequisites

Installing Apache on CentOS

Step 1: Update Software Versions List

Ensure you are using the latest versions of the software. In a terminal window, input the command:

sudo yum update

The system should reach out to the software repositories and refresh the list to the latest versions.

Step 2: Install Apache

To install Apache on your CentOS server, use the following command:

sudo yum install httpd

The system should download and install the Apache software packages.

Step 3: Activate Apache

To activate Apache, start its service first.
1. Enter the following command in a terminal window:

sudo systemctl start httpd

This will start the Apache service.
2. Next, set the Apache service to start when the system boots:

sudo systemctl enable httpd

Step 4: Verify Apache Service

Display information about Apache, and verify it’s currently running with:

sudo systemctl status httpd

Step 5: Configure firewalld to Allow Apache Traffic

In a standard installation, CentOS 7 is set to prevent traffic to Apache.
Normal web traffic uses the http protocol on Port 80, while encrypted web traffic uses the https protocol, on Port 443.
1. Modify your firewall to allow connections on these ports using the following commands:

sudo firewall-cmd ––permanent ––add-port=80/tcp
sudo firewall-cmd ––permanent ––add-port=443/tcp

2. Once these complete successfully, reload the firewall to apply the changes with the command:

sudo firewall-cmd ––reload

Step 6: Configure Virtual Hosts on CentOS 7 (optional)

Virtual hosts are different websites that you run from the same server. Each website needs its own configuration file.
Make sure these configuration files use the .conf extension, and save them in the /etc/httpd/conf.d/ directory.
There are a couple of best practices to use when you’re setting up different websites on the same server:

  • Try to use the same naming convention for all your websites. For example:
    • /etc/httpd/conf.d/hostiger.com.conf
    • /etc/httpd/conf.d/hostiger.com.conf
  • Use a different configuration file for each domain. The configuration file is called a vhost, for a virtual host. You can use as many as you need. Keeping them separate makes troubleshooting easier.

1. To create a virtual host configuration file, enter the following into a terminal window:

sudo vi /etc/httpd/conf.d/vhost.conf

This will launch the Vi text editor, and create a new vhost.conf file in the /etc/httpd/conf.d directory.
2. In the editor, enter the following text:

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName hostiger.com
ServerAlias www.hostiger.com
DocumentRoot /var/www/html/hostiger.com/public_html/
ErrorLog /var/www/html/hostiger.com/logs/error.log
CustomLog /var/www/html/hostiger.com/logs/access.log combined
</VirtualHost>

Save the file and exit.
3. Next, enter the following command to create a directory for you to store your website files in:

sudo mkdir /var/www/hostiger/{public_html, logs}

4. Restart the Apache service to apply your changes by entering:

sudo systemctl restart httpd

Once the system finishes, you should be able to open a browser window to Hostiger.com and see a default Apache test page.
You can replace Hostiger above with the name of your domain. If you are hosting more than one domain, make sure you create a new directory in /var/www/ for each one. You can copy the code block in your /etc/httpd/conf.d/vhost.conf file, and replace Hostiger with another domain name that you’re hosting.

Apache Directories and Files

One of the main ways Apache functions is through configuration files. They are located at /etc/httpd.
Apache has a main configuration file: /etc/httpd/conf/httpd.conf .
If there are any other configuration files, they are included in the main configuration file. They should use the .conf extension and should be stored in the /etc/httpd/conf.d/ directory.
You can enhance Apache’s functionality by loading additional modules.
The configuration files for these modules should be stored in: /etc/httpd/conf.modules.d/ directory.
Log files record all the activity of the Apache service, including client activity on the websites your system is hosting. These logs can be found in:  /var/log/httpd/.

Commands For Managing Apache Service

Other commands that you can use to control the Apache service include:
Stop Apache Service:

sudo systemctl stop httpd

Prevent or disable Apache from starting when the system boots:

sudo systemctl disable httpd

Re-enable Apache at boot:

sudo systemctl enable httpd

Restart Apache and apply any changes you have made:

sudo systemctl restart httpd

Conclusion

Great job, you have installed Apache on your CentOS 7 server. The basic installation and management of the Apache service are simple.

How To Set or Change Timezone on CentOS 7

Checking the Current Timezone

In CentOS and other modern Linux distros, you can use the timedatectl command to display and set the current system’s time and timezone.

Changing Timezone in CentOS

Before changing the timezone, you’ll need to find out the long name for the timezone you want to use. The timezone naming convention usually uses a “Region/City” format.
To list all available time zones, you can either list the files in the /usr/share/zoneinfo directory or use the timedatectl command.

timedatectl list-timezones
...
America/Tijuana
America/Toronto
America/Tortola
America/Vancouver
America/Whitehorse
America/Winnipeg
...

Once you identify which time zone is accurate to your location, run the following command as sudo user:

sudo timedatectl set-timezone your_time_zone

For example, to change the system’s timezone to America/Toronto:

sudo timedatectl set-timezone America/Toronto

Run the timedatectl command to verify the changes: