How to Install Nginx on Debian 9

Install Nginx on Debian 9

In this tutorial we are going to learn how to install Nginx on Debian 9. We will also install and configure its prerequisites. Nginx is one of the most common open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. The specialization of Nginx is its capability to operate on little resource and handle a high number of requests. Nginx works as an internet server, proxy, load balancer and Reverse Proxy. Today Nginx is installed on more afterward 450 million servers net, and is used by a few of the maximum traffic sites on the internet such as Dropbox and Netflix.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Debian 9 Stretch minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Nginx on Debian 9

Step 1. First, before we install packages, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS:

sudo apt update
sudo apt upgrade

Step 2. Installing Nginx on Debian 9 Stretch.

Nginx is available in the Debian package repositories simple. Follow this command to install Nginx:

sudo apt install nginx

Once the installation is completed, check the status and the version of the Nginx service, with:

systemctl start nginx
systemctl enable nginx

Step 3. Configure Firewall ports for Nginx.

If you use iptables to filter connections to your system, you’ll need to open HTTP (80) and HTTPS (443) ports:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Step 4. Verifying Apache Installation.

To verify that everything works correctly, open your browser, type your server IP address or doman name http://Your_IP_or_Your_Domain/

Step 5. Nginx Configuration File Structure.

  • Nginx configuration files are stored in the /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • Server block (vhost) configuration files are stored in /etc/nginx/sites-available directory. The configuration files found in this directory are not used by Nginx unless they are linked to the /etc/nginx/sites-enabled directory.
  • Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a sites-available directory to the sites-enabled directory.
  • To write more maintainable code, it’s a good idea to a follow a standard naming convention. For example if your domain name is mydomain.com then your the configuration file should be named /etc/nginx/sites-available/mydomain.com.conf.
  • The /etc/nginx/snippets directory contains configuration snippets that can be included in the server block files. If you use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log files for each server block.

Congratulation’s! You have successfully install and configured Nginx on your Debian Stretch. Thanks for using this tutorial installing Nginx web server on Debian 9 (Stretch) system.

How to Install Nginx on Ubuntu 18.04

Install Nginx on Ubuntu 18.04

In this tutorial we will show you how to install Nginx on Ubuntu 18.04. We will also install and configure its prerequisites. Nginx is one of the most common open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. The specialization of Nginx is its capability to operate on little resource and handle a high number of requests. Nginx works as an internet server, proxy, load balancer and Reverse Proxy. Today Nginx is installed on more afterward 450 million servers net, and is used by a few of the maximum traffic sites on the internet such as Dropbox and Netflix.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 18.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Nginx on Ubuntu 18.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Nginx on Ubuntu 18.04 Bionic Beaver.

Nginx is available in the Ubuntu package repositories simple. Follow this command to install Nginx:

apt-get install nginx

Once the installation is completed, check the status and the version of the Nginx service, with:

systemctl start nginx
systemctl enable nginx

Step 3. Configure Firewall ports for Nginx.

If you are using ufw you need to open HTTP port 80 and/or HTTPS port 433:

sudo ufw allow 'Nginx Full'

To verify the change run:

### sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Step 4. Test Nginx in your browser.

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “Welcome to nginx!” Web page which means Nginx Web server is running correctly.

Congratulation’s! You have successfully install and configured Nginx on your Ubuntu 18.04 Bionic Beaver server. Thanks for using this tutorial installing Nginx web server on Ubuntu 18.04 LTS (Bionic Beaver) system.

How to Install Nginx on CentOS 7

Install Nginx on CentOS 7

In this tutorial we’ll learn how to install Nginx on CentOS 7. We will also install and configure its prerequisites. Nginx is one of the most common open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. The specialization of Nginx is its capability to operate on little resource and handle a high number of requests. Nginx works as an internet server, proxy, load balancer and Reverse Proxy. Today Nginx is installed on more afterward 450 million servers net, and is used by a few of the maximum traffic sites on the internet such as Dropbox and Netflix.

I recommend to use a minimal CentOS server setup as a basis for the tutorial, that can be a virtual or a root server image with an CentOS 7 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Nginx on CentOS 7

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

yum -y install epel-release
yum -y update

Step 2. Installing Nginx.

Install Nginx by running the following command:

sudo yum install nginx

Once the instillation is completed, you can make sure that Nginx service is running with the following command:

systemctl start nginx
systemctl enable nginx

Step 3. Configure firewall ports for Nginx.

Next, you will need to allow the default Nginx port 80 (HTTP) and 443 (HTTPS) using FirewallD. You can do this by running the following commands:

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp

Reload the firewall service for the changes to take effect:

sudo firewall-cmd –reload

Step 4. Test Nginx in your browser.

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “Welcome to nginx!” Web page which means Nginx Web server is running correctly.

Congratulation’s! You have successfully install Nginx on your CentOS 7 server. Thanks for using this tutorial for installing Nginx web server on CentOS 7 system.

How to Install Nginx on Ubuntu 16.04

Install Nginx on Ubuntu 16.04

In this tutorial we will show you how to install Nginx on Ubuntu 16.04. We will also install and configure its prerequisites. Nginx is one of the most common open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. The specialization of Nginx is its capability to operate on little resource and handle a high number of requests. Nginx works as an internet server, proxy, load balancer and Reverse Proxy. Today Nginx is installed on more afterward 450 million servers net, and is used by a few of the maximum traffic sites on the internet such as Dropbox and Netflix.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Nginx on Ubuntu 16.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Nginx.

Nginx is available in the Ubuntu package repositories simple. Follow this command to install Nginx:

apt-get install nginx

Once the instillation is completed, you can make sure that Nginx service is running with the following command:

systemctl start nginx
systemctl enable nginx

Step 3. Configure firewall ports for Nginx.

If you are using ufw you need to open HTTP port 80 and/or HTTPS port 433:

sudo ufw allow 'Nginx Full'

To verify the change run:

### sudo ufw status

Status: active

To Action From
-- ------ ----
Nginx Full ALLOW Anywhere
Nginx Full (v6) ALLOW Anywhere (v6)

Step 4. Test Nginx in your browser.

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “Welcome to nginx!” Web page which means Nginx Web server is running correctly.

Congratulation’s! You have successfully install and configured Nginx on your Ubuntu 16.04 server. Thanks for using this tutorial installing Nginx web server on Ubuntu 16.04 LTS (Xenial Xerus) system.

How to Enable HTTP/2 Support in Nginx

Enable HTTP/2 Support in Nginx

In this tutorial we’ll learn how to enable HTTP/2 support in Nginx. We will also install and configure its prerequisites. I’ve been a keen follower of these exciting changes to the HTTP specification over the past couple of years — with the launch of the HTTP/2 standard in 2015. If you are unfamiliar with http/2 there are a number of great videos available which outline just what it is and how it is different from http 1.1 — which we’ve been so accustomed to over the years.

In light of these changes and of the ever increasing browser support with this feature (over 70% when employing an SSL/TLS connection), I wanted to give it a try. I had a website on a Digital Ocean droplet, running on Ubuntu 16.04 (LTS) and the LAMP stack already with an SSL certificate setup (the perfect candidate for getting http/2 working). After some digging around on various blogs which were discussing the topic, I came across a few guides that helped, this guide is a combination of a few.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Enable HTTP/2 Support in Nginx

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing latest version of Nginx.

HTTP/2 protocol supported over nginx version >=1.9.5. So make sure that your nginx version is supporting HTTP/2 protocol or not. If you have not installed Nginx or older version use one of following method to install or upgrade it:

echo "deb http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list

Use the following commands to install it:

curl http://nginx.org/keys/nginx_signing.key | apt-key add -
sudo apt-get update
sudo apt-get install nginx

Verify Nginx version:

nginx -v

Step 3. Enable HTTP/2 module.

To enable HTTP/2 in Nginx on an Ubuntu you should edit the default Nginx server block:

sudo nano /etc/nginx/sites-available/default

Change:

listen 443 ssl;

To:

listen 443 ssl http2;

Restart Nginx to apply your changes:

systemctl restart nginx

Example server block config file:

server {
 listen 443 ssl http2;
 server_name exmple.com;

location / {
 root /var/www/example.com;
 index index.html index.htm;
 }

ssl on;
 ssl_certificate /etc/nginx/ssl/example.com.crt;
 ssl_certificate_key /etc/nginx/ssl/example.com.key;

}

Step 4. Verify if Website Support HTTP/2.

  • Manually – open IE/Chrome/Firefox and press F12 >> go to network tab and you should see protocol as HTTP/2
  • Online – you can use HTTP/2 Test tool

Congratulation’s! You have successfully enable HTTP/2 support in Nginx on your Ubuntu 16.04 server. Thanks for using this tutorial enable HTTP/2 on Ubuntu 16.04 LTS (Xenial Xerus) system.

Save

How to Fix 502 Bad Gateway Nginx Error

Fix 502 Bad Gateway Nginx Error

In this tutorial we’ll learn how to fix 502 Bad Gateway Nginx error. We will also install and configure its prerequisites. 502 Bad Gateway errors are surprisingly common, and the exact cause of this error code depends on your particular stack. This post is part of a series on troubleshooting NGINX 502 Bad Gateway errors.

Fix 502 Bad Gateway Nginx Error

Step 1. Increase buffer and timeouts inside http block:

http {
...
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
...
}

Step 2. Make sure, your php-fpm service is listening based on what you have configured in nginx, it may be either this 2 choices:

nano /etc/php-fpm.d/www.conf

Try with one of this two options:

listen = 127.0.0.1:9000

or

listen = /var/run/php5-fpm.sock

You’ll need to reload both PHP-FPM and NGINX afterwards from the command line:

systemctl restart nginx
systemctl restart php-fpm

Step 3. Now we can configure PHP-FPM in www.conf.

PHP-FPM has three options for process management: static, dynamic and ondemand. The default is dynamic and for the purpose of this article that’s what we’ll be configuring:

pm = dynamic

There are four key settings we want to change:

pm.max_children = 97
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 200

  • Pm.Max_children: that is actually the maximum number of procedures PHP-FPM will spool up. This is derived from our calculation above. Here really is the hard limit and total maximum number of processes which will ever be launched by PHP-FPM
  • Pm. Start_servers: this is actually the number of ‘spare’ procedures we want PHP-FPM to twist up as it begins  –  procedures ready and waiting to process requests. Each one will utilize memory and it takes time to spool up new processes, so I chose 20 as a beginning point
  • Pm. Min_spare_servers: this is the minimum number of spare processes we want available (where possible). I chose 10, so if all 20 of my first poem. start_servers are occupied the server will spool up yet another 10 significance 30 operating in complete.
  • Pm. max_spare_servers: this is the maximum number of spare procedures we need available.
  • Pm. max_asks: that one is very helpful. This number specifies how many requests each process should handle prior to is kills itself and re-spawns. In case your code (or even 3rd party libraries) has memory leaks, finally PHP-FPM will eat up your physical RAM and crash (bringing your website down with a 502). By setting this, PHP-FPM continually recreates itself and prevents the ‘memory leak creep’ problem bringing down your web server.

When you’re done you’ll need to restart php-fpm again:

systemctl restart php-fpm

Congratulation’s! You have successfully to fix 502 Bad Gateway Nginx error server on your CentOS 7 server. Thanks for using this tutorial how to fix 502 Bad Gateway Nginx on CentOS 7 system.

Save

Save

Save