Dynamic websites and software written in PHP require the installation of the appropriate packages and libraries on the server. In this article we will describe how to install PHP on a VPS running the CentOS 7 operating system. We will talk about the automatic and manual methods, and also share some useful information about the initial setup after the installation has been completed.
Many popular control panels have a built-in capability to automatically install all key LAMP components:
Interface and location of different menus on all control panels are different, and on some control panels only manual installation via console is provided, so everything is individual. As a rule, at the stage of creating a server through the control panel, you will be able to choose additional software - including PHP.
If the control panel you are using allows you to automatically install PHP on CentOS 7 without too much dancing around, make sure you take advantage of it, because manual installation can be tricky.
The main difficulty will be that the official repositories contain only older versions of PHP. At the same time, these versions are often needed to make other software work. As a consequence, upgrading to the most current version of PHP 7.4, released in 2019, you may encounter problematic updates. For example, a couple of years ago, such nuances occurred when upgrading PhpMyAdmin. Today, the problem has been eliminated, and all popular software is working steadily, since more than 3 years have passed since the release of version 7.4.
You can use any convenient repositories for manual installation, but we recommend Epel, Remi or Webtatic. You may already have some of them connected by default, so let's check their list first:
The console will show you information about all connected repositories, and if necessary, you will be able to connect new ones. For example, to add Remi, enter:
yum install epel-release
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
However, this is not enough to make the repository work, you must also edit its config using the nano editor, find the line "enabled=0" and change the value to 1. Do not forget to save the changes. After these manipulations the installation of PHP is done with a simple command:
yum --enablerepo=remi-php74 install php
As a result of these actions, you will install packages and software to work with PHP on the VPS. The command will also do:
yum install -y php
Next install a package of popular modules for PHP, as they may be needed for some software:
yum install -y php-mysql php-mbstring php-mcrypt php-devel php-xml php-gd
After that we restart the server with a command:
systemctl restart httpd
When the server has booted back up, you can proceed with further configuration of the software.
During the initial setup, you will need to run the script for MySQL:
You will be presented with a menu of settings in question and answer format. The program will ask if you have a root level password, but you can safely skip this step, as we don't have one yet. You must specify a new MySQL database password, and then update the privileges.
After that we set up a CentOS server with PHP. Open the configuration file with the nano editor: sudo nano /etc/php.ini. Here you need to change the default limits to be able to work with big data. Here are the parameters we need:
memory_limit = 128M
post_max_size = 64M
upload_max_filesize = 64M
If this is not enough for your tasks, install as many as you need. After making the changes save and close the file.
Now go to the configuration file of the Apache web server, it is located in the directory sudo nano /etc/httpd/conf/httpd.conf. Here you need to change some indexes:
DirectoryIndex index.html index.htm index.php
Find the <Directory "/var/www/html"> module and set the AllowOverride parameter to "All". Now your server will be able to execute the .htaccess directive for those processes that are necessary for the secure operation of websites.
Next we go to the directory sudo nano /etc/httpd/conf.d/welcome.conf, in this config you need to disable the test virtual host - for this we comment all lines, putting before each of them the label #.
Now we proceed to create a virtual host. To do this, create a config:
sudo nano /etc/httpd/conf.d/name.conf
"Name" you can replace by any other suitable name, it is important that the file has a specified extension. Now open the newly created config and insert the following information into it:
CustomLog /var/log/domain.com/access.log common
Require all granted
Only instead of hostzealot.com you write your domain name, do not be confused. Confirm the changes and go back to the console. Earlier in the config we prescribed the path for saving the logs, but the subdirectory itself does not exist yet. Let's create it:
sudo mkdir /var/log/yourdomain.com
Next, create a test page of our site:
sudo nano /var/www/yourdomain.com/index.php
And inside this file we put three lines:
Confirm the changes and exit the editor. Restart the VPS:
sudo service httpd restart
As a result of all our actions PHP and the Apache webserver will start working, which you can easily tell by the performance of the test page. When you try to access the server by IP, you will get an access error, but when you log in via the domain you will get to the PHP configuration page. Everything works but you have to delete the test page for security reasons:
sudo rm -f /var/www/yourdomain.com/index.php
The final step of setting up PHP on VPS under CentOS will be the installation of the phpMyAdmin web application. After that, you will be able to develop sites and applications in php. Here we end our material, if there are still questions - contact our experts on the telephone numbers listed on the site, or write to Livechat.