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, MySQL & PHP (LEMP) on Ubuntu 20.04 https://tecadmin.net/install-lemp-stack-on-ubuntu-20-04/ https://tecadmin.net/install-lemp-stack-on-ubuntu-20-04/#respond Tue, 30 Jun 2020 07:51:31 +0000 https://tecadmin.net/?p=21307 A combination of Linux, Nginx, MySQL, and PHP is known as LEMP stack is the popular web hosting environment for the PHP based application. Here Linux is an operating system, Nginx is the popular web server, MySQL is relational database management system used for storing data and PHP is the widely used programming language. This [...]

The post How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04 appeared first on TecAdmin.

]]>
A combination of Linux, Nginx, MySQL, and PHP is known as LEMP stack is the popular web hosting environment for the PHP based application. Here Linux is an operating system, Nginx is the popular web server, MySQL is relational database management system used for storing data and PHP is the widely used programming language.

This article will help you to install Nginx, MySQL 8.0 and PHP 7.4 on Ubuntu Linux system. Let’s begin with the installation of LEMP stack your Ubuntu machine.

Prerequisites

Before beginning the LEMP installation on Ubuntu:

  • A running Ubuntu 20.04 system
  • Login as sudo proviledged account on your system. To create it follow initial server setup tutorial.
  • A domain/subdomain name pointed to your server

Installing Nginx Web Server

Next, you need to install Nginx web server on your system. The Nginx packages are are available under the default apt repositories.

Run the following commands to install it:

sudo apt update
sudo apt install nginx

Installing PHP with PHP-FPM

PHP 7.4 packages are available under the default repositories on Ubuntu 20.04 LTS. Use the following command to update apt cache and install PHP on your system.

sudo apt update
sudo apt install -y php7.4 php7.4-fpm

Also install additional PHP modules required for your application.

sudo apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml

You have installed PHP 7.4 with PHP FPM package on your system. Let’s check the status of the PHP FPM service by running below command:

sudo systemctl status php7.4-fpm

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-16 05:15:57 UTC; 34s ago
       Docs: man:php-fpm7.4(8)
    Process: 882716 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74>
   Main PID: 882699 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2283)
     Memory: 10.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─882699 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─882714 php-fpm: pool www
             └─882715 php-fpm: pool www

Jun 16 05:15:57 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Jun 16 05:15:57 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Installing MySQL

The default Ubuntu 20.04 apt repositories contains MySQL server 8.0. Finally, install mysql-server packages for the MySQL database. Also, install the php-mysql package to use MySQL support using PHP. Use the following command to install it.

sudo apt install mysql-server php7.4-mysql

The installer will prompt for the root password, This password will work for your MySQL root user. After installing MySQL execute the following command for initial settings of MySQL server. You will see that script will prompt for more settings than earlier MySQL versions like password validation policy etc.

sudo mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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

There are three levels of password validation 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: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Configure Nginx with PHP-FPM

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.4-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

Step 5 – Manage Services

We have done with the installation of LAMP stack on Ubuntu 20.04 LTS system. The below commands will help you to start/stop or restart Nginx and MySQL services running with systemd.

To restart Nginx and MySQL services, type:

sudo systemctl restart nginx
sudo systemctl restart mysql

To start Nginx and MySQL services, type:

sudo systemctl start nginx
sudo systemctl start mysql

To stop Nginx and MySQL services, type:

sudo systemctl stop nginx
sudo systemctl stop mysql

Step 6 – Adjust Firewall Rules

You can directly provide a service name like “http” or “https” to allow. The firewalld uses /etc/services file to determine the corresponding port of the service.

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

Step 7 – Verify the Setup

After completing all setup. Let’s create a info.php file website document root with following content.

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

Now access this file in web browser. You will see the screen like below with all details of PHP on server.

how to setup lemp stack on ubuntu 20.04

Congratulation’s! You have successfully configured LEMP server on your Ubuntu 20.04 LTS system.

The post How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-lemp-stack-on-ubuntu-20-04/feed/ 0
How To Install Nginx with PHP-FPM on Ubuntu 20.04 https://tecadmin.net/setup-nginx-php-fpm-on-ubuntu-20-04/ https://tecadmin.net/setup-nginx-php-fpm-on-ubuntu-20-04/#comments Wed, 17 Jun 2020 16:23:43 +0000 https://tecadmin.net/?p=21776 The PHP FPM (FastCGI Process Manager) is used for deploying PHP based websites over Nginx web server. FPM is a process manager to manage the FastCGI SAPI (Server API) in PHP. This tutorial will help you to install and configure Nginx with PHP-FPM on Ubuntu 20.04 system. Prerequisites Shell access with sudo privileged account to [...]

The post How To Install Nginx with PHP-FPM on Ubuntu 20.04 appeared first on TecAdmin.

]]>
The PHP FPM (FastCGI Process Manager) is used for deploying PHP based websites over Nginx web server. FPM is a process manager to manage the FastCGI SAPI (Server API) in PHP. This tutorial will help you to install and configure Nginx with PHP-FPM on Ubuntu 20.04 system.

Prerequisites

Shell access with sudo privileged account to Ubuntu 20.04 system.

Step 1 – Installing Nginx

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

sudo apt update 
sudo apt install nginx

Step 2 – Installing PHP

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.4 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.4 php7.4-fpm
Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.4/fpm directory. You can read more about enable/disable PHP modules.

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

sudo systemctl status php7.4-fpm

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-16 05:15:57 UTC; 1 day 10h ago
       Docs: man:php-fpm7.4(8)
   Main PID: 882699 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 2, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2283)
     Memory: 12.1M
     CGroup: /system.slice/php7.4-fpm.service
             ├─882699 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─882714 php-fpm: pool www
             └─882715 php-fpm: pool www

Jun 16 05:15:57 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Jun 16 05:15:57 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Step 4 – Configuring NGINX with FPM

Next, create a Nginx server block configuration file to run PHP with FPM. Create and Edit a VirtualHost host configuration file in a 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.4-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

Step 4 – Testing the Setup

Your server setup is completed now. Let’s create a PHP script with phpinfo() function and place it to your server document root. Use below command to create php script:

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

Then access info.php viausing server IP address (for default VirtualHost) or configured domain in Nginx VirtualHost.

how to setup nginx php fpm ubuntu 20.04

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

Conclusion

In this tutorial, you have learned to configure Nginx web server with PHP-FPM on your Ubuntu 20.04 Linux system.

The post How To Install Nginx with PHP-FPM on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-nginx-php-fpm-on-ubuntu-20-04/feed/ 1
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 CentOS 8 https://tecadmin.net/install-nginx-php-fpm-centos-8/ https://tecadmin.net/install-nginx-php-fpm-centos-8/#comments Sat, 14 Dec 2019 04:09:32 +0000 https://tecadmin.net/?p=19977 The common way to run PHP with Nginx is the FastCGI module. The PHP-FPM (FastCGI Process Manager) dramatically increases the performance of your Nginx/PHP environment. So this is useful for high load websites. This tutorial will help you to configure PHP-FPM with Nginx on CentOS 8 and RHEL 8 Linux system. Prerequsities The newly installed [...]

The post How to Install Nginx with PHP-FPM on CentOS 8 appeared first on TecAdmin.

]]>
The common way to run PHP with Nginx is the FastCGI module. The PHP-FPM (FastCGI Process Manager) dramatically increases the performance of your Nginx/PHP environment. So this is useful for high load websites. This tutorial will help you to configure PHP-FPM with Nginx on CentOS 8 and RHEL 8 Linux system.

Prerequsities

Step 1 – Install Nginx

The Nginx packages are available under the default AppStream repository. You can simply update the DNF cache and install Nginx web server packages using the following commands.

sudo dnf update 
sudo dnf install nginx

After installation of packages starts Nginx service, Also enable Nginx service to auto-start on system boot.

sudo systemctl enable nginx
sudo systemctl start nginx

Step 2 – Install PHP with PHP-FPM

The Remi repository contains the latest PHP packages for the CentOS 8 Linux system. So first of all, you need to add the REMI repository to your system. Just execute the following command to add the repository.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Then enable the required DNF module for PHP installation. Here we are enabling the module for installing PHP 7.4. You can change this to PHP 7.3 or PHP 7.2 as per your requirements.

sudo dnf module reset php
sudo dnf module enable php:remi-7.4

Now, install the PHP on your system. As we are going to use FastCGI Process Manager (FPM) for this setup. So install the php-fpm package as well.

sudo dnf update
sudo dnf install php php-fpm  php-gd php-mysqlnd

You may also require some more PHP modules, So install them before going next. After completing PHP installation enable PHP-FPM service and start it.

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Make sure the php-fpm service is running.

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 2019-12-09 21:44:57 PST; 1h 24min ago
 Main PID: 29280 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 3, slow: 0, Traffic: 0req/sec"
    Tasks: 6 (limit: 10321)
   Memory: 24.6M
   CGroup: /system.slice/php-fpm.service
           ├─29280 php-fpm: master process (/etc/php-fpm.conf)
           ├─29281 php-fpm: pool www
           ├─29282 php-fpm: pool www
           ├─29283 php-fpm: pool www
           ├─29284 php-fpm: pool www
           └─29285 php-fpm: pool www

Dec 09 21:44:57 tecadmin.example.com systemd[1]: Starting The PHP FastCGI Process Manager...
Dec 09 21:44:57 tecadmin.example.com systemd[1]: Started The PHP FastCGI Process Manager.

Step 3 – Configure PHP-FPM

At this step, you have installed all the required packages. Let’s start the configuration process. First, edit PHP-FPM configuration file:

sudo vim /etc/php-fpm.d/www.conf

Make the changes like below. The latest versions of Nginx can connect to the socket using a proxy. So make sure listen is set to a socket file.

Then set user and group the same as Nginx server using. If you need to connect FPM from a remote system change listen.allowed_clients to LAN IP instead of 127.0.0.1.

; listen = 127.0.0.1:9000
listen = /run/php-fpm/www.sock

user = www-data
group = www-data

listen.allowed_clients = 127.0.0.1
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic

After making changes restart the php-fpm service to apply changes.

sudo systemctl restart php-fpm

Step 4 – Create Nginx Server Block

Now, create a server block in Nginx for your domain and configure it to use php-fpm for processing PHP files. Create a server block file and edit in a text editor:

sudo vim /etc/nginx/conf.d/example.com.conf

Now added proxy configuration using a socket file. Also configured all PHP script to use fpm handler for execution.

server {
	listen       80 default_server;
	server_name  example.com www.example.com;
	root         /var/www/html;

	# Load configuration files for the default server block.
	include /etc/nginx/default.d/*.conf;

	location / {
	}

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

Save the server block configuration file and restart the Nginx service to apply changes.

sudo systemctl restart nginx

Step 5 – Enable Firewall Rules

Your server is ready to serve the application. If there is a firewall enabled on your system, make sure HTTP ports are open to access from remote systems.

The following commands will open the required ports for you.

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

Step 6 – Test Setup

All done. To test the environment, create a PHP script with phpinfo() function. Place this file to your server document root. Use the below command to do this.

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

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

http://localhost/info.php

Slide down the page and check value of $_SERVER[‘SERVER_SOFTWARE’] under PHP Variables section. This will show you the Web server name running on.

Conclusion

You have successfully configured an Nginx with PHP-FPM on your CentOS 8 or RHEL 8 Linux system. You can now host a website from your server.

The post How to Install Nginx with PHP-FPM on CentOS 8 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-php-fpm-centos-8/feed/ 3
How to Install Nginx with PHP-FPM on Debian 10 https://tecadmin.net/install-nginx-php-fpm-debian-10/ https://tecadmin.net/install-nginx-php-fpm-debian-10/#comments Wed, 27 Nov 2019 18:09:38 +0000 https://tecadmin.net/?p=19910 The FastCGI Process Manager (FPM) is an alternative to the FastCGI configuration with multiple enhancements. This is useful for high load websites. This tutorial will help you with the installation and configuration of PHP-FPM with Nginx on your Debian 10 Buster Linux system. Prerequsities You must have SSH access to the Debian 10 system with [...]

The post How to Install Nginx with PHP-FPM on Debian 10 appeared first on TecAdmin.

]]>
The FastCGI Process Manager (FPM) is an alternative to the FastCGI configuration with multiple enhancements. This is useful for high load websites. This tutorial will help you with the installation and configuration of PHP-FPM with Nginx on your Debian 10 Buster Linux system.

Prerequsities

You must have SSH access to the Debian 10 system with Sudo privileges.

Nginx Installation

Nginx packages are available under default repositories. SSH to your Debian 10 system with sudo privileges accounts and install the Nginx web server from the official repository.

sudo apt update 
sudo apt install nginx 

PHP Installation

For the PHP installation we recommend to use Ondřej Surý‘s PPA, which provides latest PHP versions for Debian systems. Use the below couple of commands to add the PPA to your system.

wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
sudo echo "deb https://packages.sury.org/php/ buster main" | tee /etc/apt/sources.list.d/php.list

Then install PHP 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 php php-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 the 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 Tue 2019-12-03 10:01:54 UTC; 24min ago
     Docs: man:php-fpm7.3(8)
 Main PID: 9883 (php-fpm7.3)
   Status: "Processes active: 0, idle: 2, Requests: 3, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 3587)
   Memory: 14.2M
   CGroup: /system.slice/php7.3-fpm.service
           ├─9883 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
           ├─9884 php-fpm: pool www
           └─9885 php-fpm: pool www

Dec 03 10:01:54 tecadmin-debian10 systemd[1]: Starting The PHP 7.3 FastCGI Process Manager...
Dec 03 10:01:54 tecadmin-debian10 systemd[1]: Started The PHP 7.3 FastCGI Process Manager.

Nginx Configuration

Let’s create Nginx virtual host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in a 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:/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.

http://localhost/info.php

Nginx with PHP-FPM on Debian 10

Conclusion

All done. You have successfully configured Nginx web server with PHP-FPM on your Debian 10 (Buster) Linux. Your server is ready to host websites.

The post How to Install Nginx with PHP-FPM on Debian 10 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-nginx-php-fpm-debian-10/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