In part 1 of this tutorial, we explained why we decided to create a LAMP server instead of preconfiguring it via a control panel such as cPanel or Virtualmin. We have configured the Remi repository for PHP and installed PHP 7.3 and PHP-FPM. We have also installed the latest version of MariaDB and fulfilled some prerequisites.
In this CentOS 7 LAMP Server Tutorial, we will fully configure Apache and PHP-FPM for our first virtually hosted Web site, then test everything to make sure it works. Let's go!
First, let's add the user to our server and create the web hosting directories and log files generated by Apache.
The website will live in / home / lowend / public_html and the logs will be in / home / lowend / logs. Paste the following items to create the directories and set their properties (CHange OWNership) and their permissions (CHange MODe):
mkdir / home / lowend / public_html
mkdir / home / lowend / logs
chown lowend.lowend / home / lowend / public_html
chmod 755 -R / home / lowend
We need to create a directory for the configuration files of our website and tell Apache where to look for them. We can name it as we see fit, but the standard is "sites-enabled". It resides in the same directory as the rest of the Apache configuration files, / etc / httpd.
mkdir / etc / httpd / sites-enabled
echo "Include enabled sites / *. conf" >> /etc/httpd/conf/httpd.conf
By default, Apache searches only index.html files for use as an index page. We will host a dynamic PHP site. So we have to ask Apache to look for pages index.php too. Apache uses the "DirectoryIndex" directive to choose which file names to look for and in what order. We will add "index.php" by replacing "DirectoryIndex index.html" with "DirectoryIndex index.html index.php" in the main Apache configuration file, httpd.conf. For that, we will use the Stream EDitor (sed):
sed -i / DirectoryIndex index.html / DirectoryIndex index.html index.php / & 39; /etc/httpd/conf/httpd.conf
We must now inform Apache of our website, lowend-tutorial.tld. We told Apache to look for .conf files in / etc / httpd / sites-enabled / and include them in its configuration. This is where we will put our configuration file. Use the nano editor to open a new file, named after the domain name.
Paste in the following configuration text. We have added many comments to indicate the role of each section. Be sure to read the comments, as they are really part of this tutorial.
#define our VirtualHost. Independent of the IP (hence the asterisk), it serves pages on port 80
#Set the ServerName - the name of the VirtualHost, not your entire server.
# Pasqu '# no IP address is used, ServerName defines the domain name of the virtual host.
# Allow the use of the prefix "www"
#DocumentRoot defines the location of the website files
DocumentRoot / home / lowend / public_html
#Sett options for our public_html directory
Options -Index + FollowSymLinks + MultiViews
Require all granted
# Set the location and the name of the error
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerging.
# Defines how much information goes into our log files
# Set the location and name of the log of access
Combined CustomLog /home/lowend/logs/lowend-tutorial.tld-access.log
# Configure a way for Apache to communicate with PHP-FPM via
# a single Unix socket containing the user name
# We will explain what is a socket in the next section
# PHP-FPM requires at least one parameter to start, so we will use this one
ProxySet Disablereuse = off
# Tell Apache to direct all requests for PHP files to our proxy, which then passes them to PHP-FPM, which runs the PHP program and returns the results to Apache.
Proxy SetHandler: fcgi: // php-fpm
Save the file with CTRL + O and exit with CTRL + X. We have now talked about our website at Apache, but we have not said anything about PHP-FPM. We will then finish the configuration of PHP-FPM and create its configuration file for our website.
We will now edit the Apache Multi-Processing Module (MPM) configuration file. There are different MPMs available to suit different use cases. As we will be using PHP-FPM, we need to use the MPM-worker module and disable the MPM-prefork module. Apache has a directory where it loads additional configuration files for its modules as if they were part of httpd.conf, its main configuration file. This directory is /etc/httpd/conf.modules.d/. The file we are modifying is 00-mpm.conf. The prefix "00" guarantees that it will be loaded before the other configuration files with a numerically superior prefix.
Comment out the mod_mpm_prefork.so line and uncomment the mod_mpm_worker.so line. here is what our appearance looks like:
Save the changes (CTRL + O) and exit nano (CTRL + X). If you want to know more about MPM work packages and their differences, here's an excellent explanation of ServerFault: https://serverfault.com/questions/383526/how-do-i-select-which-apache-mpm-to-use
We talked about our virtual host at Apache, and even told him where to expect a Unix socket to communicate with him. A Unix socket is a way for the processes to communicate with each other and resides in the file system. Any process that has access to the file has access to the socket and can communicate with the program that created the socket. Pretty cool, right?
We will create a PHP-FPM pool for our "lowend-tutorial.tld" account. Each account will get its own pool that will run under this user. In this way, each PHP-FPM has access to reading and writing to the account. PHP-FPM creates a socket for each PHP-FPM pool and we have already configured Apache to look for that socket.
Since we use PHP 7.3 from Remi, this configuration file must be in the Remi PHP 7.3 configuration directory. This directory is at
This directory already contains "www.conf"Which is a default configuration file. We will use it as a base to create our own configuration file, lowend.conf. Then we will disable www.conf by renaming it www.conf.disabled. Here are the commands to execute:
cp /etc/opt/remi/php73/php-fpm.d/www.conf /etc/opt/remi/php73/php-fpm.d/lowend.conf
mv /etc/opt/remi/php73/php-fpm.d/www.conf /etc/opt/remi/php73/php-fpm.d/www.conf.désactivée
We need to edit a little lowend.conf. Open it with nano:
This configuration file is commented with a semicolon, so everything with one; before it's just a comment. The first uncommented line you will see is [www]. This gives a name to the PHP-FPM pool, which must be unique for this process pool. Change [www] at [lowend].
The next uncommented lines you will see are "user" and "group". These are configured to run the PHP-FPM pool as the "apache" user. We need to change them to work as a "low end" user.
; RPM: apache user chosen to provide access to the same directories as httpd
user = lowend
; RPM: Keep a group authorized to write to the log directory.
group = low end
A little further away is a line that defines where PHP-FPM listens for communication. This is not correct for our use. Comment on the line "listen":
; listen = 127.0.0.1:9000
and add this line below:
listen = /var/run/php73-fpm/php73-fpm.lowend.sock
The last addition we are going to make is just below the "listen.mode" line, a few lines down. We need to tell PHP-FPM to listen to Apache user (apache) connections and give it read / write access to the socket. Paste in the following:
listen.owner = apache
listen.group = apache
listen.mode = 0666
Save the changes (CTRL + O) and exit nano (CTRL + X). We are almost there!
We told Apache to look in / var / run / php73-fpm, and PHP-FPM to put the socket in this directory, but this directory does not exist yet. It's easy enough to fix – just create it with the following command:
mkdir / var / run / php73-fpm
It's time to enable PHP-FPM to start every time the server is started, and we need to start it now.
systemctl enable php73-php-fpm
systemctl start php73-php-fpm
Finally, we need to restart Apache to apply any changes made to its configuration:
systemctl restart httpd
Apache and PHP-FPM are configured, enabled, and started manually. MariaDB is ready to support database related tasks and FirewallD has been configured to allow port 80 traffic. Before calling it "done", we need to check the operation.
If it was a non-virtual hosting, we would just have to access the IP address of the server in a browser and find that it loads a page. . But this is a virtual hosting, and the mere fact of visiting the IP does not tell us anything. We must visit the domain name associated with the virtual host. In our case, it's "lowend-tutorial.tld". To do this, we must actually point the domain name to the main IP address of the server. Your domain registrar should offer you this option. If it is not, you can use CloudFlare DNS or any other free DNS provider to point yourdomain.com and www.votredomaine.com on the main IP address of your server with A records.
Once the DNS is set up, try visiting the website. For us it's "lowend-tutorial.tld". You should see the Apache test page in plain text because there is no index.html or index.php in / home / lowend / public_html. Here is what it looks like:
This tells us that Apache works, but says nothing about PHP. To test this, we will use phpinfo () to provide us with a test page for PHP. The test page should go to / home / lowend / public_html because this is the DocumentRoot we provided to Apache. Be sure to replace "lowend" in the following command to create a file called "test.php".
sudo -u lowend echo ""> /home/lowend/public_html/test.php
Now visit the page in a browser by accessing yourdomain.com/test.php. It should look like this:
If your PHP test page worked, congratulations! If that does not work, do not despair. Come back through this tutorial and make sure each step is right. It's easy to miss a minor detail, and all you need is a small mistake.
We hope you have found this tutorial both informative and useful. In the next article in this series, we will install WordPress, the most popular (and most popular!) Content management system on the Internet. Stay tuned!