How to Install and set up PHP and Nginx (LEMP) on OpenSUSE Leap 15.3

In this guide we are going to install and set up PHP and Nginx in OpenSUSE Leap 15.3. 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.

# Table of Content

  1. Ensuring that the server is up to date
  2. Installing PHP on OpenSUSE Leap 15.3
  3. Installing Nginx on OpenSUSE Leap 15.3
  4. Installing php-fpm in OpenSUSE Leap 15.3
  5. Creating a simple site and configuring virtual host
  6. Testing the configuration

# 1. 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

sudo zypper refresh
sudo zypper update -y

# 2. Installing PHP on OpenSUSE Leap 15.3

PHP is available in the default OpenSUSE repos. The latest version of PHP as of writing this blog is PHP 7.4.

Install PHP and some common packages using this command:

sudo zypper install -y \
    php \
    php-cli

Check the installed version of PHP

~> zypper info php7
Repository 'Update repository with updates from SUSE Linux Enterprise 15' is out-of-date. You can run 'zypper refresh' as root to update it.
Loading repository data...
Reading installed packages...


Information for package php7:
-----------------------------
Repository     : Update repository with updates from SUSE Linux Enterprise 15
Name           : php7
Version        : 7.4.6-3.29.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 10.3 MiB
Installed      : Yes
Status         : up-to-date
Source package : php7-7.4.6-3.29.1.src
Summary        : Interpreter for the PHP scripting language version 7
Description    :
    PHP is a server-side HTML embedded scripting language designed
    primarily for web development but also used as a general-purpose
    programming language.

    This package contains the standard implementation of PHP, namely Zend
    PHP. Included are the PHP command-line binary and the configuration
    file (php.ini). This package must be installed in order to use PHP.
    Additionally, extension modules and server modules (e.g. for Apache)
    may be installed.

    Additional documentation is available in package php-doc.

Check the installed php version using this command:

~> php -v
PHP 7.4.6 (cli) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

# 3. Installing Nginx on OpenSUSE Leap 15.3

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 UpenSUSE repos. Install it using this command:

sudo zypper install -y nginx

Confirm the installed nginx

~> zypper info nginx
Loading repository data...
Reading installed packages...


Information for package nginx:
------------------------------
Repository     : Main Repository
Name           : nginx
Version        : 1.19.8-3.6.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 2.2 MiB
Installed      : Yes
Status         : up-to-date
Source package : nginx-1.19.8-3.6.1.src
Summary        : A HTTP server and IMAP/POP3 proxy server
Description    :
    nginx [engine x] is a HTTP server and IMAP/POP3 proxy server written by Igor Sysoev.
    It has been running on many heavily loaded Russian sites for more than two years.

The installed Nginx service is not started by default. To start the service, use this command:

sudo systemctl start nginx

Confirm that the service is up and running using this command:

~> 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)
     Active: active (running) since Mon 2021-12-20 18:56:15 UTC; 36s ago
    Process: 31740 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
   Main PID: 31741 (nginx)
      Tasks: 2 (limit: 4587)
     CGroup: /system.slice/nginx.service
             ├─31741 nginx: master process /usr/sbin/nginx -g daemon off;
             └─31742 nginx: worker process

Dec 20 18:56:15 ip-10-2-40-44 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 20 18:56:15 ip-10-2-40-44 nginx[31740]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 20 18:56:15 ip-10-2-40-44 nginx[31740]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 20 18:56:15 ip-10-2-40-44 systemd[1]: Started The nginx HTTP and reverse proxy server.

To enable the service on boot, use this command:

sudo systemctl enable nginx

# 4. Installing PHP FPM in OpenSUSE Leap 15.3

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:

sudo zypper install -y php-fpm

Copy default config files

sudo cp /etc/php7/fpm/php-fpm.conf.default /etc/php7/fpm/php-fpm.conf
sudo cp /etc/php7/fpm/php-fpm.d/www.conf.default /etc/php7/fpm/php-fpm.d/www.conf

The service will be not be started by default. Start php-fpm using this command:

<meta charset="utf-8">sudo systemctl start php-fpm

Check its status using this command:

~> 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 Mon 2021-12-20 08:56:16 UTC; 21s ago
   Main PID: 24608 (php-fpm)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 4587)
     CGroup: /system.slice/php-fpm.service
             ├─24608 php-fpm: master process (/etc/php7/fpm/php-fpm.conf)
             ├─24609 php-fpm: pool www
             └─24610 php-fpm: pool www

Dec 20 08:56:16 ip-10-2-40-44 systemd[1]: Starting The PHP FastCGI Process Manager...
Dec 20 08:56:16 ip-10-2-40-44 systemd[1]: Started The PHP FastCGI Process Manager.

To enable php-fpm on boot, use this command:

sudo systemctl enable php-fpm

# 5. 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 domain to be used should already be configured to point to the IP address of the server. Confirm that with:

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:

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:

sudo zypper install -y vim

Edit the file:

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

Add content to the file:

<?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:

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

Add the following content

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

sudo systemctl restart php-fpm
sudo systemctl restart nginx

# 6. 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

sudo vim /etc/hosts

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

# 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 OpenSUSE Leap 15.3.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy