23 C
Texas
angeloma
Senior Writer and partner

Install Nginx on Debian 11

Even though Apache is one of the most popular web servers out there. The truth is that many have switched to Nginx because of its speed and flexibility. So, in this post, you will learn how to install Nginx on Debian 11.

Nginx is an open-source web server that stands out for being fast and high performance. This makes it widely used by many websites that have or expect moderate traffic.

With Nginx, we can use many different configurations that make it very flexible and deployable in many situations. In addition to this, it can be used as a Load Balancer and as a Reverse Proxy for our web applications.

So learning how to install and set it up on Debian 11 can be useful for newbies who want to learn about this tool.

- Advertisement -

So, let’s get started.

Install Nginx on Debian 11

As expected, Nginx is present in the official Debian 11 repositories. So installing it by this method is recommended to ensure the integrity of the system.

So, connect via SSH to your server and upgrade it first

sudo apt update
sudo apt upgrade

Now, install Nginx as follows

sudo apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libdeflate0 libfontconfig1 libgd3 libgeoip1 libjbig0 libjpeg62-turbo libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libnginx-mod-stream-geoip libtiff5 libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6
  libxpm4 libxslt1.1 nginx-common nginx-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert
Recommended packages:
  geoip-database
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libdeflate0 libfontconfig1 libgd3 libgeoip1 libjbig0 libjpeg62-turbo libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libnginx-mod-stream-geoip libtiff5 libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6
  libxpm4 libxslt1.1 nginx nginx-common nginx-core
0 upgraded, 26 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,678 kB of archives.
After this operation, 13.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]

As with Apache, the Nginx service will start automatically at the end of the installation. So you can get the service started by running

sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-08-23 17:18:48 CEST; 13s ago
       Docs: man:nginx(8)
    Process: 4070 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 4071 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 4262 (nginx)
      Tasks: 3 (limit: 2277)
     Memory: 4.1M
        CPU: 41ms
     CGroup: /system.slice/nginx.service
             ├─4262 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─4264 nginx: worker process
             └─4265 nginx: worker process

Aug 23 17:18:48 osradar systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 23 17:18:48 osradar systemd[1]: Started A high performance web server and a reverse proxy server.

Normally you have a firewall running in Debian. So for Nginx to work you need to open the necessary ports in the firewall.

To do this, a single command line is required

sudo ufw allow 'Nginx Full'

Now yes, open a web browser and go to http://ip-server and you will see the following welcome screen.

1.- Install Nginx on Debian 11
1.- Install Nginx on Debian 11

This indicates that Nginx is ready for battle.

Enabling the PHP support on Nginx

Although Nginx is ready, it is normal that we add support for some programming languages. In this chaos, we will use PHP which is one of the most popular and the one is chosen by many.

So, install PHP and the php-fpm package.

sudo apt install php php-fpm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libgdbm-compat4 libperl5.32 libsodium23 mailcap mime-support perl perl-modules-5.32 php-common php7.4 php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache
  php7.4-readline psmisc
Suggested packages:
  perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libtap-harness-archive-perl php-pear
The following NEW packages will be installed:
  libgdbm-compat4 libperl5.32 libsodium23 mailcap mime-support perl perl-modules-5.32 php php-common php-fpm php7.4 php7.4-cli php7.4-common php7.4-fpm php7.4-json
  php7.4-opcache php7.4-readline psmisc
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.8 MB of archives.
After this operation, 66.7 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Remember that every web application requires other PHP dependencies that you will have to install. This example is to incorporate support and I have not installed any other modules.

The next step is to create a new configuration file for our site. This step is vital not to alter the default configuration of Nginx.

sudo nano /etc/nginx/sites-available/domain.conf

You can replace domain.conf with another configuration name although it is recommended that it is allusive to your domain for ease of access.

And add the following

server {
    listen 80;
    server_name your_domain;
    root /var/www/example;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}

The first thing you have to modify is the value of server_name and root which refers to the directory where the website will be.

Save the changes and close the editor.

Now create the directory path that we have specified in the file.

sudo mkdir -p /var/www/example/

And create a test PHP file

sudo nano /var/www/example/test.php

Add some PHP code

<?php
phpinfo();
>?

Save the changes and close the editor.

Then, enable the new configuration file.

sudo ln -s /etc/nginx/sites-available/domain.conf /etc/nginx/sites-enabled/

And restart Nginx to apply the changes.

sudo systemctl restart nginx

Just open your web browser and go to http://your-domain/test.php and you will see the following.

2.- PHP with Nginx on Debian 11
2.- PHP with Nginx on Debian 11

So, Nginx and PHP are working correctly on your site.

Enjoy it.

Conclusion

In this post, you have learned how to install Nginx on Debian 11. These steps have made it so that you can also complement it with PHP to make your web server more up to speed.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article