Nginx – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 17 Dec 2022 04:42:24 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Enable CORS in Nginx https://tecadmin.net/how-to-enable-cors-in-nginx/ https://tecadmin.net/how-to-enable-cors-in-nginx/#respond Fri, 16 Dec 2022 13:26:13 +0000 https://tecadmin.net/?p=20129 Cross-Origin Resource Sharing (CORS) is an important security feature for web applications. It allows web applications to use resources from other domains while preventing malicious access. By enabling CORS in Nginx, we can ensure that our web applications are secure and that they can access resources from other domains. With the right configuration, we can [...]

The post How to Enable CORS in Nginx appeared first on TecAdmin.

]]>
Cross-Origin Resource Sharing (CORS) is an important security feature for web applications. It allows web applications to use resources from other domains while preventing malicious access. By enabling CORS in Nginx, we can ensure that our web applications are secure and that they can access resources from other domains. With the right configuration, we can make sure that malicious requests are blocked and that our applications are secure.

In this article, we’ll look at how to enable CORS in Nginx.

How to Enable CORS in Nginx

Nginx is an open-source web server that is often used to serve static content. It is also used to proxy requests to other web servers, such as Apache. In order to enable CORS in Nginx, we need to add a few configuration directives.

A simple configuration to enable CORS in Nginx looks like this:

location / { 
     add_header "Access-Control-Allow-Origin" *; 
     add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; 
     add_header "Access-Control-Allow-Headers" "Authorization"; 
}

The first directive adds a header to the response that allows all origins to access the resource. The second directive adds a header that specifies which methods are allowed. The third directive adds a header that allows for the authorization header to be sent with requests.

In addition to these directives, you also need to configure a Sub URL in your Nginx configuration. This block will specify which specific URLs are allowed to be accessed via CORS. For example:

location /api/ { 
     add_header "Access-Control-Allow-Origin" *; 
     add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; 
     add_header "Access-Control-Allow-Headers" "Authorization"; 
}

This configuration will allow any origin to access the URLs that begin with /api/. It is also possible to specify specific domains that are allowed to access the resource. For example:

location /api/ { 
     add_header "Access-Control-Allow-Origin" "https://example.com"; 
     add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; 
     add_header "Access-Control-Allow-Headers" "Authorization"; 
}

This will allow only requests from example.com to access the URLs that begin with /api/. You can allow multiple domains by repeating that line with other domains.

To allow Access-Control-Allow-Origin (CORS) authorization for specific files only. For example to allow CORS for fonts only uses the following example:

if ($filename ~* ^.*?\.(eot)|(otf)|(ttf)|(woff)$){
  add_header Access-Control-Allow-Origin *;
}

Once you have added the necessary configuration directives, you can restart Nginx and the changes will take effect.

Wide Open Nginx CORS Configuration

Here is the wide-open Nginx CORS configuration file, which you can use with the Nginx servers. With this configuration, you can enable CORS and other parameters based on the request types.

location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

Resource: https://michielkalkman.com/snippets/nginx-cors-open-configuration/

Testing Your CORS Configuration

Once you have enabled CORS in Nginx, you should test your configuration to make sure it is working properly. The easiest way to do this is to use a tool such as Postman or curl to make a request to the resource you want to test.

When making the request, you should add the Origin header. For example, if you are testing a URL that begins with /api/, you should add the header Origin: https://example.com. You should also add the Access-Control-Request-Method header with the method you want to test.

curl -v http://your_domain.com 

Once you have made the request, you should check the response. If CORS is enabled properly, you should see the Access-Control-Allow-Origin header with the value of the origin you specified in the request.

CORS Config for nginx

Understanding CORS Requests

In order to understand how CORS works, it is important to understand the different types of requests that can be sent. There are two types of requests: simple requests and preflight requests.

Simple requests are requests that do not require preflight checks. These requests are typically GET or POST requests that do not have any custom headers.

Preflight requests are more complex requests that require an additional step. These requests will typically have custom headers or a method other than GET or POST. Before the request can be sent, the browser will make an initial request, known as a preflight request, to determine if the request should be allowed.

If the preflight request is allowed, the browser will then send the actual request. If the preflight request is not allowed, the browser will not send the actual request and the resource will not be accessed.

Cross-Origin Resource Sharing (CORS)
Cross-Origin Resource Sharing (CORS) Process

Conclusion

In this article, we looked at how to enable CORS in Nginx. We saw how to add the necessary configuration directives and location blocks to our Nginx configuration. We also looked at how to test our CORS configuration and how to understand CORS requests.

Thanks for reading! If you have any questions or comments, please leave them in the comments section below.

The post How to Enable CORS in Nginx appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-enable-cors-in-nginx/feed/ 0
How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/#comments Thu, 07 Apr 2022 01:31:43 +0000 https://tecadmin.net/?p=9770 The Linux operating system is a very popular and widely used OS for the server. It powers the majority of the world’s websites, including some of the most well-known ones such as Yahoo, Google, and Facebook. The logical acronym LAMP is commonly used to refer to the mixture of free and open-source software that is [...]

The post How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 appeared first on TecAdmin.

]]>
The Linux operating system is a very popular and widely used OS for the server. It powers the majority of the world’s websites, including some of the most well-known ones such as Yahoo, Google, and Facebook. The logical acronym LAMP is commonly used to refer to the mixture of free and open-source software that is frequently used together to create a server architecture that can handle dynamic websites, such as those built on PHP, MySQL, and Apache.

Each letter in the acronym refers to a separate software package: That being said, let’s see how we can install and setup LEMP Stack on Ubuntu.

Pre-Requisities

Assuming that you have a running Ubuntu 22.04 Linux system with sudo (or root) privileged access.

Access your system and open a terminal. It will be good to update the package manager cache and upgrade currently installed packages. To do this execute:

sudo apt update && sudo apt upgrade 

Let’s begin the LEMP (Linux, Nginx, MySQL, and PHP) stack installation on Ubuntu 22.04 Jammy Jellyfish Linux system.

Step 1 – Installing NGINX

First, we will install the Latest Nginx web server on our system. Use the following commands to add PPA for installing the latest Nginx version on your Ubuntu 22.04 Linux.

Use the following commands to install Nginx web server.

sudo apt install nginx 

This will install the Nginx web server and start the service.

Now, you need to allow webserver ports in the firewall. To allow ports 80 and 443 in the UFW firewall, execute the following commands.

sudo ufw allow 80/tcp 
sudo ufw allow 43/tcp 

Open a web browser on your system and type the server’s IP in the address bar. You will get the default Nginx server page

How to Install LEMP Stack on Ubuntu 22.04
Nginx Default Page

Step 2 – Installing PHP

First, you need to decide on the PHP version to install on your system. You can also install multiple PHP versions on a single system. Currently the repository contains PHP 5.6, PHP 7.1, 7.2, 7.3, 7.4 and PHP 8.0, 8.1. The below instruction will install PHP 8.1. Please change the version as per your requirements.

The ondrej/php ppa contains all PHP version’s for Ubuntu systems. So add this repository in your system with command below:

sudo add-apt-repository ppa:ondrej/php 

Now update the apt cache and install PHP 8.1.

sudo apt update 
sudo apt install php8.1 

This will install PHP on your Ubuntu system along with some useful PHP extensions.

Step 3 — Install and Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features. Nginx web server required php-fpm for processing the PHP scripts.

To install PHP-FPM, run the following command based on the installed PHP version:

sudo apt install php8.1-fpm 

Once the installation finished, check the service status:

sudo systemctl status php8.1-fpm 
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-04-07 06:26:55 UTC; 11min ago
       Docs: man:php-fpm8.1(8)
    Process: 108650 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCCESS)
   Main PID: 108647 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 2, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1034)
     Memory: 10.7M
        CPU: 88ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─108647 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
             ├─108648 "php-fpm: pool www
             └─108649 "php-fpm: pool www

Apr 07 06:26:55 ubuntu2204 systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Apr 07 06:26:55 ubuntu2204 systemd[1]: Started The PHP 8.1 FastCGI Process Manager.

In Step 6, we will configure the Nginx virtual host with PHP-FPM to serve PHP applications.

Step 4 – Installing MySQL

The default Ubuntu repositories contain MySQL 8.0. Which can be directly installed using the package manager. To install the available MySQL server version, execute the following command.

sudo apt-get install mysql-server 

Once the installation is finished, you can secure the MySQL server by executing the following command.

sudo mysql_secure_installation 

This will ask for a few questions to secure the MySQL server.

  1. Press ‘y’ to enable validate password plugin. This will allow you to set a strict password policy for user accounts.
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    
    Press y|Y for Yes, any other key for No: y
    
  2. Chose the password complexity level. Read all 3 options and choose one. For production servers we recommend to choose STRONG policy.
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
    
  3. Enter a new password and re-enter it. Make sure it matches the complexity level as described above.
    New password: *************
    Re-enter new password: *************
    
  4. Press ‘y’ to continue with provided password.
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
    
  5. Remove default anonymous users from MySQL server:
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    
  6. Disable root login from remote systems
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    
  7. Remove test database form MySQL created by default during installation.
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    
  8. Reload all privileges to apply above changes immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    

You have secured the MySQL server in the LAMP stack on Ubuntu 22.04 Linux system.

Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal.

sudo mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.28-0ubuntu4 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Type ‘quit’ to exit from the MySQL shell and return to the system terminal.

Step 5 – Installing Additional Packages

You may also need to install modules like MySQL and other extensions for PHP based on the application requirements. Use the following command to find our available PHP extensions.

sudo apt search php8.1-* 

The above command will list all available PHP7 modules for installation, Let’s begin the installation of modules.

sudo apt install php8.1-mysql php8.1-curl php8.1-xml 

You may also need to install other required PHP extensions on your system.

Step 6 — Configure Nginx VirtualHost

Finally, do the configuration of the Nginx virtual host. For this example, we are editing the default configuration file.

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

and make changes as below.

server {
        listen   80;

        root /var/www/example.com;
		
        index index.php;
		
        server_name  example.com www.example.com;

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

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

}

In the above configuration file, look for location ~ \.php$ section. Which is required to service PHP scripts via Nginx server.

You have to do the same changes in all VirtualHosts configured.

Step 7 – Verify Setup

You have successfully completed the installation of Nginx, MySQL, and PHP on the Ubuntu 22.04 Linux system. To verify the PHP integration with Nginx, create a PHP script (example: info.php) on the website document root and write the below content.

<?php
   phpinfo();
?>

Now access this file in the web browser. It will so all the details about versions and installation.

http://server-ip-or-domain-name/info.php 
Running PHP Script with Nginx and PHP-FPM

Conclusion

This tutorial helped you to set up the LEMP (Linux, Nginx, MySQL, and PHP) stack on Ubuntu 22.04 LTS system. Now, you can host PHP-based web applications on your server.

The post How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/feed/ 4
How to Increase Request Timeout in NGINX https://tecadmin.net/increase-request-timeout-in-nginx/ https://tecadmin.net/increase-request-timeout-in-nginx/#respond Thu, 13 Jan 2022 07:20:01 +0000 https://tecadmin.net/?p=28400 Sometimes the long running requests failed with the error message “504: Gateway Timeout” in NGINX web server. To solve this issue, you need to increase request timeout in NGINX server configuration. The default, NGINX request timeout is 60 seconds. Which can be increased or decreased by updating the configuration files. In this quick FAQ, you [...]

The post How to Increase Request Timeout in NGINX appeared first on TecAdmin.

]]>
Sometimes the long running requests failed with the error message “504: Gateway Timeout” in NGINX web server. To solve this issue, you need to increase request timeout in NGINX server configuration. The default, NGINX request timeout is 60 seconds. Which can be increased or decreased by updating the configuration files.

In this quick FAQ, you will learn to change the request timeout in NGINX web server.

Increase Request Timeout in NGINX

For example, you want to increase request timeout to 300 seconds. Then you need to add proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives to http or server block. Here the http block allows the changes in all server in NGINX.

To make changes for all servers, edit the NGINX main configuration file and add the following content under http block.

http{
   ...
   proxy_read_timeout 300;
   proxy_connect_timeout 300;
   proxy_send_timeout 300;
   ...
}

In case, you just want to increase request timeout for a specific server or subdomain, then add the directives for its server block only. Edit the specific server block configuration file and add the following settings:

server{
   ...
   proxy_read_timeout 300;
   proxy_connect_timeout 300;
   proxy_send_timeout 300; 
   ...
}

After making the changes, you must restart the NGINX service to apply changes. The systems running with Systemd can use the following command.

sudo systemctl restart nginx 

All done, With the above changes, you have successfully increased the request timeout in NGINX server.

Conclusion

This tutorial helps you to increase request timeout in NGINX web server.

The post How to Increase Request Timeout in NGINX appeared first on TecAdmin.

]]>
https://tecadmin.net/increase-request-timeout-in-nginx/feed/ 0
How to Install Nginx on Ubuntu 20.04 https://tecadmin.net/install-nginx-ubuntu-20-04/ https://tecadmin.net/install-nginx-ubuntu-20-04/#respond Thu, 21 May 2020 16:11:17 +0000 https://tecadmin.net/?p=21578 Nginx is an popular web server widely used by web hosting providers. It was created by Igor Sysoev and first public release in 2004. Nginx is also used as reverse proxy, load balancer, mail proxy and HTTP cache server. Nginx web server is highly useful for heavy traffic websites. This tutorial will help you to [...]

The post How to Install Nginx on Ubuntu 20.04 appeared first on TecAdmin.

]]>
Nginx is an popular web server widely used by web hosting providers. It was created by Igor Sysoev and first public release in 2004. Nginx is also used as reverse proxy, load balancer, mail proxy and HTTP cache server. Nginx web server is highly useful for heavy traffic websites.

This tutorial will help you to install and configure Nginx web server on Ubuntu 20.04 system.

Prerequisites

  • Running Ubuntu 20.04 system
  • Shell access with root or sudo privileged user

Step 1 – Installing Nginx

The Nginx latest packages are available under default repositories for all Ubuntu’s versions. You can directly install them by running following commands

sudo apt update
sudo apt install nginx

After installation of Nginx service, run below command to check the status of Nginx service.

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 Thu 2020-05-21 11:24:22 UTC; 37min ago
       Docs: man:nginx(8)
   Main PID: 56655 (nginx)
      Tasks: 2 (limit: 2283)
     Memory: 2.7M
     CGroup: /system.slice/nginx.service
             ├─56655 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─56656 nginx: worker process

May 21 11:24:22 tecadmin systemd[1]: Starting A high performance web server and a reverse proxy server...
May 21 11:24:22 tecadmin systemd[1]: Started A high performance web server and a reverse proxy server.

Step 2 – Nginx Configuration Files and Directories

Here is the list of configuration file and directories used by the Nginx web server.

  1. /etc/nginx/nginx.conf – is the main configuration file for Nginx server.
  2. /etc/nginx/conf.d – Directory contains configuration files for the applications configured with Nginx, like phpMyAdmin etc.
  3. /etc/nginx/sites-available – Contains all (active and inactive) the websites configuration files. We also known them as server blocks or virtual hosts.
  4. /etc/nginx/sites-enabled – Contains active websites configuration files linked to ../sites-available directory.
  5. /etc/nginx/modules-available – Contains all modules configuration files available on server
  6. /etc/nginx/modules-enabled – Contains active modules configuration files linked to ../modules-enabled directory.

Step 3 – Manage Nginx Service

The systemd users uses systemctl commands to manage services on Ubuntu system. You can also use the same for managing the Nginx service on your system. Use below commands will stop, start, and restart Nginx service with systemd.

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx

Step 4 – Adjust Firewall Rules

The FirewallD is the popular firewall application used on Ubuntu systems. Some of the Ubuntu users also preferred to use Uncomplicated Firewall (UFW) on their system.

FirewallD users use the following command to open HTTP (80) and HTTPS (443) ports.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https

Reload the firewall changes by running the following command:

firewall-cmd --reload

Step 5 – Test Setup

Connect to your server using localhost or use IP address for remote systems in a web browser. This will show you the default Nginx page.

Setup Nginx on Ubuntu 20.04

Conclusion

In this tutorial, you have learned about the installation of Nginx web server.

The post How to Install Nginx on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-ubuntu-20-04/feed/ 0
How to Configure Nginx for WordPress Permalinks https://tecadmin.net/nginx-for-wordpress-permalinks/ https://tecadmin.net/nginx-for-wordpress-permalinks/#comments Thu, 02 Apr 2020 17:28:44 +0000 https://tecadmin.net/?p=20004 Permalinks is a URL structure that can link to a specific blog post. It is also used to create archives and pagination links. The permalink allows you to have an anchor point for any blog post, regardless of the path name. WordPress is a popular content management system (CMS) written in PHP. MySQL is the [...]

The post How to Configure Nginx for WordPress Permalinks appeared first on TecAdmin.

]]>
Permalinks is a URL structure that can link to a specific blog post. It is also used to create archives and pagination links. The permalink allows you to have an anchor point for any blog post, regardless of the path name. WordPress is a popular content management system (CMS) written in PHP. MySQL is the backend database server for WordPress. It maintains its URLs with a permalink, the full URL of any post, page, or other website content. Apache web server uses .htaccess to manage permalinks for WordPress. Nginx doesn’t follow .htaccess, So we need to make changes in the Nginx configuration to follow permalinks by the Nginx web server.

Setup Nginx for WordPress Permalinks

A WordPress website/blog can be run on the main domain or with a directory URL. For example, your blog is running with the main domain. Edit the Nginx configuration file and add the following line under the location section.

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}

If you are running your blog under subdirectory URL like /blog, use location a /blog block to your configuration file

location /blog {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}

Save your file and restart the Nginx service.

systemctl restart nginx.service 

That’s it. Now WordPress will be accessible with configured permalinks under the General settings.

Change WordPress Permalinks

The default WordPress permalinks are not SEO friendly, which is not good for the SEO of your application. You need to change permalink settings to make URLs SEO-friendly. To change permalinks, Login to admin panel >> Settings >> Permalinks option.

Default Permalinks:

  • https://tecadmin.net/?p=101

SEO Friendly Permalinks:

  • https://tecadmin.net/post-name
  • https://tecadmin.net/category/post-name
  • https://tecadmin.net/2020/01/22/post-name

Wordpress permalinks setting

Conclusion

URLs play a good role in website SEO. In WordPress, you can change the URL type by configuring the permalink settings. This tutorial helped you to configure the Nginx web server to handle custom permalinks of WordPress.

The post How to Configure Nginx for WordPress Permalinks appeared first on TecAdmin.

]]>
https://tecadmin.net/nginx-for-wordpress-permalinks/feed/ 3
How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS https://tecadmin.net/install-nginx-php-fpm-ubuntu-18-04/ https://tecadmin.net/install-nginx-php-fpm-ubuntu-18-04/#respond Thu, 14 Nov 2019 18:17:25 +0000 https://tecadmin.net/?p=19839 This tutorial will help you to install Nginx web server with PHP-FPM/FastCGI on Ubuntu 18.04 (Bionic) LTS system. In this tutorial, we are using PHP 7.3 and configure with Nginx using PHP-FPM and FastCGI. Nginx Installation Nginx packages are available under default repositories. SSH to your Ubuntu 18.04 LTS system with sudo privileges account and [...]

The post How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS appeared first on TecAdmin.

]]>
This tutorial will help you to install Nginx web server with PHP-FPM/FastCGI on Ubuntu 18.04 (Bionic) LTS system. In this tutorial, we are using PHP 7.3 and configure with Nginx using PHP-FPM and FastCGI.

Nginx Installation

Nginx packages are available under default repositories. SSH to your Ubuntu 18.04 LTS system with sudo privileges account and install Nginx web server from the official repository.

sudo apt update 
sudo apt install nginx

PHP Installation

For the PHP installation we recommend to use ppa:ondrej/php PPA, which provides latest PHP versions for Ubuntu systems. Use the below couple of commands to add the PPA to your system.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then install PHP 7.3 the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.

apt update
sudo apt install php7.3 php7.3-fpm
Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.3/fpm directory. You can read more about enable/disable PHP modules.

After installing above packages php7.3-fpm service will automatically be started. You can make sure by typing below command on terminal.

sudo systemctl status php7.3-fpm

● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-11-21 03:36:08 UTC; 36s ago
     Docs: man:php-fpm7.3(8)
 Main PID: 9054 (php-fpm7.3)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 4704)
   CGroup: /system.slice/php7.3-fpm.service
           ├─9054 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
           ├─9069 php-fpm: pool www
           └─9070 php-fpm: pool www

Nov 21 03:36:08 tecadmin systemd[1]: Starting The PHP 7.3 FastCGI Process Manager...
Nov 21 03:36:08 tecadmin systemd[1]: Started The PHP 7.3 FastCGI Process Manager.

Nginx Configuration

Let’s create a Nginx virtual host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in text editor. You can create new VirtualHost as per your requirements, so make sure to enable any new VirtualHost.

sudo vim /etc/nginx/sites-available/example.com

Use the below basic Nginx Virtual host configuration with php fpm settings. Update the configuration as followings.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm;
        server_name example.com;

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

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

Save your changes to the configuration file and create a link to site enabled directory.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com 

Then restart Nginx service to reload the changes.

sudo systemctl restart nginx.service

Test Setup

Create a PHP script with phpinfo() function and place it to your server document root. Use below command to do it.

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Now access the info.php using server IP address (for default VirtualHost) for your configured domain in Nginx VirtualHost.

Nginx PHP-FPM Ubuntu 18.04

Slide down the page and see the value of $_SERVER[‘SERVER_SOFTWARE’]. This will the show the web server details.

Nginx PHP Ubuntu 18.04

Conclusion

You have successfully configured Nginx web server with PHP-FPM on your Ubuntu 18.04 (Bionic) LTS. You can now host a website from your server.

The post How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-php-fpm-ubuntu-18-04/feed/ 0
How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04 https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/#comments Sat, 16 Jun 2018 11:08:39 +0000 https://tecadmin.net/?p=16280 Generally, web hosting manager used a separate server for each PHP version application deployment. Which increases the hosting cost. Alternatively, you can run multiple Docker containers for multiple PHP versions. This tutorial helps you with the installation and configuration of two VirtualHost on the Nginx web server with different PHP versions. First VirtualHost will work [...]

The post How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04 appeared first on TecAdmin.

]]>
Generally, web hosting manager used a separate server for each PHP version application deployment. Which increases the hosting cost. Alternatively, you can run multiple Docker containers for multiple PHP versions.

This tutorial helps you with the installation and configuration of two VirtualHost on the Nginx web server with different PHP versions. First VirtualHost will work with PHP 5.6 and another VirtualHost will run with PHP 7.2. So just go through this tutorial. You can also use more than two PHP versions with Nginx as required but this tutorial covers two only.

PHP Installation

For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.

sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

For this tutorial, I used PHP 5.6 and PHP 7.2 to configure it with the Nginx web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system.

apt update
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.2 php7.2-fpm

You may also need to install additional PHP modules. For the tutorial, only the above packages are required.

After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

sudo systemctl status php5.6-fpm
sudo systemctl status php7.2-fpm

Nginx Installation

The Nginx web server is packages are available in the official Ubuntu repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of the Nginx web server.

sudo apt update 
sudo apt install nginx

Nginx Configuration

Get ready for the configuration of websites in the Nginx server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

sudo mkdir /var/www/php56
sudo mkdir /var/www/php72

Now, create and index.php containing the phpinfo() function.

echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/php72/index.php

After that create server blocks for both sites on Nginx. The latest version of Nginx keeps the server block configuration files under /etc/nginx/sites-available directory. Create a file for the first virtual host and edit in your favorite text editor.

sudo vim /etc/nginx/sites-available/php56.example.com

Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

# Application with PHP 5.6
#
server {
	listen 80;

	root /var/www/php56;
	index index.php;
	server_name php56.example.com;

	location ~* \.php$ {
		# With php-fpm unix sockets
		fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
		include         fastcgi_params;
		fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
	}
}

Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/nginx/sites-available/php72.example.com

Add the following content to file with proper ServerName and DocumentRoot.

# Application with PHP 7.2
#
server {
	listen 80;

	root /var/www/php72;
	index index.php;
	server_name php72.example.com;

	location ~* \.php$ {
		# With php-fpm unix sockets
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		include         fastcgi_params;
		fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
	}
}

All right, You have created both websites in your Nginx. But they are still not active. Nginx keeps active sites under /etc/nignx/sites-enabled directory. You can simply create a symbolic link for both config files to this directory or use the below command to do the same.

sudo ln -s /etc/nginx/sites-available/php56.example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/

After making all the changes restart Nginx to reload new settings changes.

sudo systemctl restart nginx.service

Your setup has been completed now. Go to the next step to test your setup.

Test Setup

All done. You can access both sites in your favirote web browser . You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.

Multiple PHP Version with Nginx & PHP 7.2

Multiple PHP Version with Nginx & PHP 5.6

Congratulations, You system is ready to host websites with different PHP versions. Happy hosting.

The post How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/feed/ 6
How To Install Nginx on Ubuntu 18.04 & 16.04 https://tecadmin.net/install-nginx-on-ubuntu/ https://tecadmin.net/install-nginx-on-ubuntu/#respond Sat, 26 May 2018 07:34:29 +0000 https://tecadmin.net/?p=15970 Nginx is another most popular web server than Apache HTTP Server. It is used by a large number of popular websites worldwide. This web server is highly useful for heavy traffic websites. This tutorial will help you to install Nginx on Ubuntu 18.04 and 16.04 using apt-get. 1. Prerequisites In order to install Nginx on [...]

The post How To Install Nginx on Ubuntu 18.04 & 16.04 appeared first on TecAdmin.

]]>
Nginx is another most popular web server than Apache HTTP Server. It is used by a large number of popular websites worldwide. This web server is highly useful for heavy traffic websites. This tutorial will help you to install Nginx on Ubuntu 18.04 and 16.04 using apt-get.

1. Prerequisites

In order to install Nginx on Ubuntu, you must have root or sudo privileged user access to your system. For the remote systems can use ssh command or putty for Windows user to login.

ssh ubuntu@remote

2. Install Nginx on Ubuntu

The Nginx latest packages are available under default repositories for all Ubuntu’s versions, You can directly install them by running following commands

sudo apt-get update
sudo apt-get install nginx

After installation of Nginx service, run below command to check the status of Nginx service.

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 Fri 2018-04-27 15:38:31 IST; 13min ago
     Docs: man:nginx(8)
  Process: 3406 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 3405 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 3407 (nginx)
    Tasks: 2 (limit: 2323)
   CGroup: /system.slice/nginx.service
           ├─3407 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─3408 nginx: worker process

3. Configure Firewall

You need to open the firewall port to access Nginx server from remote systems. The following command will allow port 80 (HTTP) and port 443 (HTTPS) on your system.

sudo ufw allow 'Nginx FULL'

You don’t need to open both ports. Use one of the following commands to open specific ports.

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'

4. Manage Nginx Service

The systemd users use systemctl commands to manage their Nginx service on Ubuntu system. The below commands will stop, star, and restart nginx service.

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx

The post How To Install Nginx on Ubuntu 18.04 & 16.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-on-ubuntu/feed/ 0
How To Enable TLS 1.2 Only In Nginx Web Server https://tecadmin.net/enable-tls-with-nginx/ https://tecadmin.net/enable-tls-with-nginx/#comments Sun, 29 Apr 2018 01:25:46 +0000 https://tecadmin.net/?p=15991 SSL 2.0 and SSL 3.0 having lots of known vulnerabilities like POODLE (CVE-2014-3566), That’s why latest browsers have removed support for these vulnerable protocols. We also recommend moving your server to use TLS versions and specifically to TLS 1.2. This tutorial will help you to enable TLS 1.2 with Nginx web server. Install and Use [...]

The post How To Enable TLS 1.2 Only In Nginx Web Server appeared first on TecAdmin.

]]>
SSL 2.0 and SSL 3.0 having lots of known vulnerabilities like POODLE (CVE-2014-3566), That’s why latest browsers have removed support for these vulnerable protocols. We also recommend moving your server to use TLS versions and specifically to TLS 1.2. This tutorial will help you to enable TLS 1.2 with Nginx web server.

Enable TLS 1.2 Only in Nginx

Edit your Nginx server block section for your domain in configuration file on your server and add set the ssl_protocols as followings. This enables TLSv1.2 only protocol in your Nginx server block.

 ssl_protocols TLSv1.2;

The simplest Nginx server block with SSL looks like below


server {
    listen 443 ssl;
    server_name example.com;

    ssl_protocols TLSv1.2;
    ssl_certificate /etc/pki/tls/cert.pem;
    ssl_certificate_key /etc/pki/tls/private/privkey.pem;

Enable TLS 1.1 and 1.2 Both

As per article written here POODLE vulnerability expands beyond SSLv3 to TLS 1.0 and 1.1. So we don’t recommend to use this for production server but if you want to enable this for your development. You can do following configuration.

 ssl_protocols TLSv1.2 TLSv1.1;

After making changes in your configuration file, restart Nginx service to apply new settings.

The post How To Enable TLS 1.2 Only In Nginx Web Server appeared first on TecAdmin.

]]>
https://tecadmin.net/enable-tls-with-nginx/feed/ 1
How To Install Nginx on Debian 8 (Jessie) https://tecadmin.net/install-nginx-on-debian-8-jessie/ https://tecadmin.net/install-nginx-on-debian-8-jessie/#respond Sat, 28 Apr 2018 04:03:20 +0000 https://tecadmin.net/?p=16036 Nginx is another most popular web server than Apache HTTP Server. It is used by a large number of popular websites word wide. This web server is highly useful for heavy traffic websites. This tutorial will help you to install Nginx on Debian 8 Jessie using apt-get. 1. Prerequisites To install Nginx on Debian 8, [...]

The post How To Install Nginx on Debian 8 (Jessie) appeared first on TecAdmin.

]]>
Nginx is another most popular web server than Apache HTTP Server. It is used by a large number of popular websites word wide. This web server is highly useful for heavy traffic websites. This tutorial will help you to install Nginx on Debian 8 Jessie using apt-get.

1. Prerequisites

To install Nginx on Debian 8, you must have root or sudo privileged user access to your system. For the remote systems can use ssh command or putty for Windows user to login.

ssh root@debian9

2. Install Nginx on Debian 8

The Nginx latest packages are available under default repositories for all Ubuntu’s versions, You can directly install them by running following commands

sudo apt-get update
sudo apt-get install nginx

After installation of Nginx service, run below command to check the status of Nginx service.

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 2018-04-30 03:54:27 UTC; 2s ago
     Docs: man:nginx(8)
  Process: 5396 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 5394 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 5398 (nginx)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─5398 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─5399 nginx: worker process

3. Configure Firewall

Most probably you are using IPTABLES for Debian system. The following command will allow port 80 (HTTP).

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

You can use the same for port 443 (HTTPS). If you somehow used ufw firewall use following:

sudo ufw allow 80/tcp

4. Manage Nginx Service

The systemd users use systemctl commands to manage their Nginx service on Debian 8 Jessie. The below commands will stop, star, and restart nginx service.

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx

The post How To Install Nginx on Debian 8 (Jessie) appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-on-debian-8-jessie/feed/ 0