Ubuntu 16 Install Nginx Php


Ubuntu 16 Install Nginx Php

Step 1 – Installing Nginx and PHP

The first step of the process is to install Nginx and PHP. To do this, open the terminal and type:

sudo apt-get install nginx php7.0-fpm

This command will install both Nginx and PHP on your machine. It is important to note that Nginx is installed in the “/etc/nginx” folder and PHP-FPM is installed in the “/etc/php/7.0/fpm” folder.

Step 2 – Configuring Nginx

Once Nginx and PHP have been installed, the next step is to configure Nginx. To do this, open the Nginx configuration file located in “/etc/nginx/nginx.conf” and add the following lines to the “server” section:

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

This will configure Nginx to work with PHP-FPM. Now save and exit the configuration file.

Step 3 – Starting the Services

Now we need to start the Nginx and PHP-FPM services. To do this, open the terminal and type:

sudo service nginx start
sudo service php7.0-fpm start

This will start both Nginx and PHP-FPM, and now they are both running on your machine.

Step 4 – Testing the Setup

Now we need to test the setup to make sure everything is working properly. To do this, create a file called “test.php” in the “/usr/share/nginx/html” folder and add the following code:


echo "Testing...";
?>

Now open your browser and go to “http://localhost/test.php”. If the page shows “Testing…” then everything is working properly and Nginx and PHP-FPM are successfully installed and configured.

Step 5 – Securing the Setup

Now that the setup is working properly, it is important to secure it. To do this, open the Nginx configuration file located in “/etc/nginx/nginx.conf” and add the following lines to the “server” section:

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_param;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}

This will configure Nginx to require authentication for any PHP scripts. Now save and exit the configuration file and then open the terminal and type:

sudo htpasswd -c /etc/nginx/.htpasswd USERNAME

This will create a file in the “/etc/nginx” folder called “.htpasswd” and prompt you for a password for the specified username. Once you have entered the password, the setup is now secure.

Conclusion

In conclusion, installing Nginx and PHP-FPM on Ubuntu 16 is quite easy and straightforward. All you need to do is install the packages, configure Nginx, start the necessary services, test the setup, and secure the setup. After following these steps, you should have a working Nginx and PHP-FPM setup on your machine.

FAQs

Q: How do I install Nginx on Ubuntu 16?

A: To install Nginx on Ubuntu 16, open the terminal and type: sudo apt-get install nginx

Q: How do I install PHP-FPM on Ubuntu 16?

A: To install PHP-FPM on Ubuntu 16, open the terminal and type: sudo apt-get install php7.0-fpm

Q: How do I configure Nginx to work with PHP-FPM?

A: To configure Nginx to work with PHP-FPM, open the Nginx configuration file located in “/etc/nginx/nginx.conf” and add the following lines to the “server” section:

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Now save and exit the configuration file.

Q: How do I secure the setup?

A: To secure the Nginx and PHP-FPM setup, open the Nginx configuration file located in “/etc/nginx/nginx.conf” and add the following lines to the “server” section:

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}

Now save and exit the configuration file and then open the terminal and type: sudo htpasswd -c /etc/nginx/.htpasswd USERNAME. This will create a file in the “/etc/nginx” folder called “.htpasswd” and prompt you for a password for the specified username.

Thank you for reading this article. Please read our other helpful articles on similar topics.

Leave a Reply

Your email address will not be published. Required fields are marked *