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:
- How to Install and set up PHP and Nginx (LEMP) on OpenSUSE Leap 15.3
- How to Install Apache PHP 7.4 (LAMP) stack on Rocky Linux/Centos 8
- How to install and set up PHP and Apache(LAMP stack) on Ubuntu 20.04
- How to Install and set up PHP and Nginx (LEMP) on Ubuntu 20.04
- How to install and set up PHP and Apache(LAMP stack) on Debian 11
- How to Install and set up PHP and Nginx (LEMP) on Debian 11
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
|
|
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:
|
|
Confirm that the remi repository has been installed and enabled with this command:
|
|
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:
|
|
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:
|
|
If for some reason its failing, you can reset the existing module with this command:
|
|
Now that the repo has been enabled, lets install php with this command:
|
|
Once successful. confirm the version installed with this command:
|
|
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:
|
|
Confirm the installed nginx
|
|
The service will not be started by default. To start Nginx, use this command:
|
|
Confirm the status using this command:
|
|
To enable the service on boot, use this systemd command:
|
|
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:
|
|
The service will be not be started by default. To start it, use this systemd command:
|
|
Check the service status to confirm that it is running using this command:
|
|
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:
|
|
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:
|
|
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:
|
|
Edit the file:
|
|
Add content to the file:
|
|
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:
|
|
Add the following content
|
|
Let us restart the nginx and php-fpm service for the configuration to apply
|
|
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
|
|
Then, add the IP address of your actual server followed by the domain name you are configuring, for example:
|
|
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.