How to Install and set up PHP and Nginx (LEMP) on Rocky Linux/Alma Linux 8

In this guide we are going to install and set up PHP and Nginx in Rocky Linux/Alma Linux 8. We will also set up a virtual host to serve a simple php info page.

PHP is a general-purpose scripting language geared towards web development. It is one of the popular programming languages for the web. Popular tools such as WordPress are coded using php. Big companies like Facebook also uses php heavily.

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. It started out as a web server designed for maximum performance and stability. Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

Related Content:

Ensuring that the server is up to date

Before proceeding, it is always a good practice to ensure that the server is up to date. Use these commands to achieve this

1
sudo df update -y

Installing PHP on Rocky Linux/Alma Linux 8

For php files to be served, php needs to be set up in the system. In this guide we will install php 7.4.

PHP 7.4 is not yet available in the default repositories. The Remi Repository is the perfect repo to install php7.4. Remi repo is a free and stable YUM repository mainly for the PHP stack. It contains packages for the latest versions of PHP.

To enable Remi Repository in our Centos Server:

1
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Confirm that the remi repository has been installed and enabled with this command:

1
2
3
4
5
6
7
8
9
$ sudo dnf repolist
repo id                                 repo name
appstream                               CentOS Linux 8 - AppStream
baseos                                  CentOS Linux 8 - BaseOS
epel                                    Extra Packages for Enterprise Linux 8 - x86_64
epel-modular                            Extra Packages for Enterprise Linux Modular 8 - x86_64
extras                                  CentOS Linux 8 - Extras
remi-modular                            Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe                               Safe Remi's RPM repository for Enterprise Linux 8 - x86_64<

You should see the remi repos as part of the list.

Now that the repository has been installed, lets search for php. Use this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ sudo dnf module list php
Last metadata expiration check: 0:49:30 ago on Wed 22 Dec 2021 04:23:13 AM UTC.
Rocky Linux 8 - AppStream
Name           Stream             Profiles                            Summary
php            7.2 [d]            common [d], devel, minimal          PHP scripting language
php            7.3                common [d], devel, minimal          PHP scripting language
php            7.4                common [d], devel, minimal          PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name           Stream             Profiles                            Summary
php            remi-7.2           common [d], devel, minimal          PHP scripting language
php            remi-7.3           common [d], devel, minimal          PHP scripting language
php            remi-7.4           common [d], devel, minimal          PHP scripting language
php            remi-8.0           common [d], devel, minimal          PHP scripting language
php            remi-8.1           common [d], devel, minimal          PHP scripting language

From the list. we can see that the default one is 7.2. Performing a dnf install php will install the 7.2 which we do not want. Let’s enable the 7.4 using this command:

1
sudo dnf module enable php:remi-7.4

If for some reason its failing, you can reset the existing module with this command:

1
sudo dnf module reset php

Now that the repo has been enabled, lets install php with this command:

1
2
3
sudo dnf install -y php \
    php-common \
    php-cli

Once successful. confirm the version installed with this command:

1
2
3
4
5
$ php -v
PHP 7.4.27 (cli) (built: Dec 14 2021 17:17:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

Installing Nginx on Rocky Linux

We will use Nginx to serve the PHP content. Nginx, as a stable high-performance web server and with a very low consumption of resources, is the perfect match for PHP-FPM. Nginx has an asynchronous architecture that is much more scalable, based on events.

Nginx is available in the default Rocky Linux/Alma Linu repos. Install it using this command:

1
sudo dnf install -y nginx

Confirm the installed nginx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ rpm -qi nginx
Name        : nginx
Epoch       : 1
Version     : 1.14.1
Release     : 9.module+el8.4.0+542+81547229
Architecture: x86_64
Install Date: Wed 22 Dec 2021 05:16:08 AM UTC
Group       : System Environment/Daemons
Size        : 1669719
License     : BSD
Signature   : RSA/SHA256, Thu 10 Jun 2021 09:22:38 AM UTC, Key ID 15af5dac6d745a60
Source RPM  : nginx-1.14.1-9.module+el8.4.0+542+81547229.src.rpm
Build Date  : Thu 10 Jun 2021 09:10:40 AM UTC
Build Host  : ord1-prod-x86build005.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : infrastructure@rockylinux.org
Vendor      : Rocky
URL         : http://nginx.org/
Summary     : A high performance web server and reverse proxy server
Description :
Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
IMAP protocols, with a strong focus on high concurrency, performance and low

The service will not be started by default. To start Nginx, use this command:

1
sudo systemctl start nginx

Confirm the status using this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ sudo systemctl status nginx
 ● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           php-fpm.conf
   Active: active (running) since Wed 2021-12-22 05:17:32 UTC; 45s ago
  Process: 118235 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 118228 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 118226 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 118237 (nginx)
    Tasks: 3 (limit: 23167)
   Memory: 5.0M
   CGroup: /system.slice/nginx.service
            nginx: master process /usr/sbin/nginx
            nginx: worker process
           118239 nginx: worker process

Dec 22 05:17:32 rockylinuxsrv.citizix.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 22 05:17:32 rockylinuxsrv.citizix.com nginx[118228]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 22 05:17:32 rockylinuxsrv.citizix.com nginx[118228]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 22 05:17:32 rockylinuxsrv.citizix.com systemd[1]: Started The nginx HTTP and reverse proxy server.

To enable the service on boot, use this systemd command:

1
sudo systemctl enable nginx

Installing PHP FPM in Rocky Linux/Alma Linux 8

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely-popular alternative PHP (Hypertext Processor) FastCGI implementation. PHP-FPM is the most popular alternative implementation of PHP FastCGI. It has additional features which are really useful for high-traffic websites. When using Nginx with PHP-FPM, performance at the level of memory consumption is improved.

PHP runs as a separated service when using PHP-FPM. By using this PHP version as language interpreter, requests are processed through a TCP/IP socket; so that the Nginx web server only handles the HTTP requests and PHP-FPM interprets the PHP code. The fact of having two separate services is key for increasing efficiency.

Install php-fpm using this command:

1
sudo dnf install -y php-fpm

The service will be not be started by default. To start it, use this systemd command:

1
sudo systemctl start php-fpm

Check the service status to confirm that it is running using this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ sudo systemctl status php-fpm
 ● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-12-22 05:17:32 UTC; 3min 5s ago
 Main PID: 118227 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 6 (limit: 23167)
   Memory: 22.9M
   CGroup: /system.slice/php-fpm.service
            php-fpm: master process (/etc/php-fpm.conf)
           php-fpm: pool www
           php-fpm: pool www
           php-fpm: pool www
           php-fpm: pool www
           118234 php-fpm: pool www

Dec 22 05:17:32 rockylinuxsrv.citizix.com systemd[1]: Starting The PHP FastCGI Process Manager...
Dec 22 05:17:32 rockylinuxsrv.citizix.com systemd[1]: Started The PHP FastCGI Process Manager.

Creating a simple site and configuring virtual host

Nginx allows you to serve multiple sites using virtual hosts. The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are differentiated by their host name. Visitors to the Web site are routed by host name or IP address to the correct virtual host. Virtual hosting allows companies sharing one server to each have their own domain names.

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domainto be used should already be configured to point to the IP address of the server. Confirm that with:

1
dig -t A site1.citizix.com

Creating a directory and adding sample content

The default page of nginx is configured as a default virtual host serving content from /var/www/html. It’s a best practice to create the directory of your site in the /var/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

1
sudo mkdir /var/www/site1.citizix.com

Lets create a simple index.php page to serve from our site. You will require vim installed for this to work, if not use this command:

1
sudo dnf install -y vim

Edit the file:

1
sudo vim /var/www/site1.citizix.com/index.php

Add content to the file:

1
<?php phpinfo(); ?>

Creating virtual host (Server blocks)

An Nginx server blocks can be used to encapsulate configuration details and host more than one domain on a single server. We are going to create a virtual host for our site (site1.citizix.com).

Nginx virtual hosts configurations are stored in the directory /etc/nginx/conf.d/. Lets create a config for our site:

1
sudo vim /etc/nginx/conf.d/site1.citizix.com.conf

Add the following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server {
    listen 80;
    server_tokens off;
    client_max_body_size 100M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com.log;
    error_log  /var/log/nginx/site1.citizix.com.log;

    root /var/www/site1.citizix.com;
    index index.php;

    if ($host !~* ^(site1.citizix.com)$) {
        return 444;
    }

    location / {
        proxy_read_timeout 600;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Let us restart the nginx and php-fpm service for the configuration to apply

1
2
sudo systemctl restart php-fpm
sudo systemctl restart nginx

Testing the configuration

To test the configuration, open your browser and load the domain you set up. In my case it is http://site1.citizix.com. You should see the php info page.

If you haven’t yet configured your DNS settings or simply do not want the site to go live yet, you can modify your computer’s hosts file. To do this, use the following command in your local computer’s CLI

1
sudo vim /etc/hosts

Then, add the IP address of your actual server followed by the domain name you are configuring, for example:

1
2
# Virtual Hosts Local Test
10.2.11.11 site1.citizix.com

Now you should be able to save the file and access it from within a web browser.

Conclusion

Nginx virtual hosts or server blocks are a great way to add additional websites to the same origin server. The number of configuration possibilities for a given site are nearly endless when you start modifying the virtual host configuration file to suit your the specific needs of your site.

In this guide we learnt how to set up a virtual host to serve php content in Rocky Linux/Alma Linux or RHEL based system.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy