Category Archives: nginx

Install PHP + Nginx and PostgreSQL

Install PHP, Here if you want to install any specific version just add php version after php for example: if you want to install php version 7.1 then just do it by this command sudo apt install php7.1 or leave it only php to get latest version of php.

Step 1: Install php latest version

//example for php 7.1:  sudo apt install php7.1
sudo apt install php 
sudo apt install php-fpm 
sudo apt install php-apcu 
sudo apt install php-curl 
sudo apt install php-cli 
sudo apt install php-pgsql 
sudo apt install php-gd 
sudo apt install php-mcrypt 
sudo apt install php-mbstring 
sudo apt install php-intl

Step 2: Improve security
Improve security by editing /etc/php//fpm/php.ini and modifying pathinfo to 0

sudo nano /etc/php//fpm/php.ini

set it 0

cgi.fix_pathinfo=0  

Step 3: Restart PHP

sudo systemctl restart php7.1-fpm

Step 4: Installing the above PHP packages will pull in apache packages that are not required. Remove

sudo apt --purge remove *apache*

Step 5: Install Nginx

//Install NGINX
sudo apt install nginx-common
sudo apt install nginx                                                    
sudo systemctl start nginx

Step 6: Install Nginx
Installing the above PHP packages will pull in apache packages that are not required. Remove

sudo apt --purge remove *apache*
//Install NGINX
sudo apt install nginx-common
sudo apt install nginx                                                    
sudo systemctl start nginx

For More : php-nginx-postgresql

How To Configure Nginx VirtualHost In Ubuntu

Configuration of Nginx VirtualHost
The initial step to configure Nginx VirtualHost is to go to its web root directory and create two directories as follows.

jaforiqbal@jaforiqbal-X510UA:/home/user1# cd /var/www/html/
jaforiqbal@jaforiqbal-X510UA:/var/www/html# mkdir site1 site2

Once the directories are created, use the following command to create a file with text in the site1 directory. Repeat the same for site2 directory.

jaforiqbal@jaforiqbal-X510UA: /var/www/html/site1# nano index.html

Now, use the following commands to create server block files.

jaforiqbal@jaforiqbal-X510UA: cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site1.conf
jaforiqbal@jaforiqbal-X510UA: cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site2.conf

Configure the site1.conf file as follows.

jaforiqbal@jaforiqbal-X510UA: nano /etc/nginx/sites-available/site1.conf

server {
	listen 80 ;
	listen [::]:80;

	root /var/www/html/site1;


        index index.php index.html index.htm;

	server_name linuxhelp1.com  www.linuxhelp1.com;


      # pass the PHP scripts to FastCGI server listening on the php-fpm socket
       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
               index index.php index.html index.htm;
               fastcgi_index index.php;
	       #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
                
       }


}

Now configure the site2.conf as follows.

jaforiqbal@jaforiqbal-X510UA: nano /etc/nginx/sites-available/site2.conf

server {
	listen 80 ;
	listen [::]:80;

	root /var/www/html/site2;


        index index.php index.html index.htm;

	server_name linuxhelp2.com www.linuxhelp2.com;


      # pass the PHP scripts to FastCGI server listening on the php-fpm socket
       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
               index index.php index.html index.htm;
               fastcgi_index index.php;
	       #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
                
       }


}

You can remove the default server block with the following command.

jaforiqbal@jaforiqbal-X510UA: root@linuxhelp1:~# rm /etc/nginx/sites-enabled/default

Now enable the new server block by running the following command.

jaforiqbal@jaforiqbal-X510UA: ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/
jaforiqbal@jaforiqbal-X510UA: ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/

You can check the Nginx configuration with the help of below command.

jaforiqbal@jaforiqbal-X510UA: nginx -t

Output should be:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

After checking the Nginx configuration, restart the Nginx service by running following command.

jaforiqbal@jaforiqbal-X510UA: systemctl restart nginx

Now you have to edit your hosts file by entering your IP address and domain name as below.

jaforiqbal@jaforiqbal-X510UA: nano /etc/hosts

Output should be:
127.0.2.1 www.linuxhelp1.com
127.0.3.1 www.linuxhelp2.com

You can check the VirtualHost configuration by calling http://www.linuxhelp1.com or http://www.linuxhelp1.com on your browser’s address bar.

If it’s not working in any step, Just check the error log by entering the command below.

jaforiqbal@jaforiqbal-X510UA: sudo tail -30 /var/log/nginx/error.log

For more details: Linuxhelp

install Nginx, PHP7, Mysql in ubuntu

Steps to install Nginx, PHP 7, Mysql 5 in ubuntu 16.04:

Step 1: Install the Nginx Web Server

 sudo apt-get install nginx

check the ngnix is start

 http://localhost 

Note: Nginx default use the 80 port, hence if you apache install then change the port for ngnix

Step 2: Install MySQL

 sudo apt-get install mysql-server 

set password to the mysql

Step 3: Install PHP

 sudo apt-get install php-fpm php-mysql 

Configure the PHP processor

excute below command to edit php.ini

 sudo vim /etc/php/7.0/fpm/php.ini 

set parameter in PHP.ini

 cgi.fix_pathinfo=0 

Run below command to restart the php-fpm

 sudo service php7.0-fpm restart 

Step 4: Configure Nginx to Use the PHP

Edit below file:

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

add index.php file after index line and uncomment php code

 
 # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                
        }
 

And restart the nginx using

 sudo service nginx restart 

Step 5: Create a PHP File to Test Configuration
Create file in root directory

 sudo nano /var/www/html/info.php 

and put php tag and “phpinfo()” function

Run file with browser as below

 http://localhost/info.php