Difference between revisions of "Nginx"

From ElectroDragon
Jump to navigation Jump to search
(Full remove)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== install ==
 
== install ==
* In case for any error, stop apache first: sudo service apache2 stop
+
Remove apache2
 +
* In case for any error, stop apache first:  
 +
sudo service apache2 stop
 +
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
 +
sudo apt-get autoremove
 +
 
 
* apt-get install nginx
 
* apt-get install nginx
 
* allow gfw -> sudo ufw allow 'Nginx HTTP'
 
* allow gfw -> sudo ufw allow 'Nginx HTTP'
Line 16: Line 21:
 
* sudo apt-get purge nginx nginx-common # Removes everything.
 
* sudo apt-get purge nginx nginx-common # Removes everything.
 
* sudo apt-get autoremove
 
* sudo apt-get autoremove
 +
=== Upgrade ===
 +
# Add the sources
 +
<source lang="c">
 +
sudo echo "
 +
deb http://packages.dotdeb.org jessie all
 +
deb http://nginx.org/packages/mainline/debian/ jessie nginx" | sudo tee -a /etc/apt/sources.list
  
 +
# Add the keys to our system so we trust packages from the sources we added
 +
cd /tmp
 +
wget http://www.dotdeb.org/dotdeb.gpg
 +
sudo apt-key add dotdeb.gpg
 +
 +
wget http://nginx.org/keys/nginx_signing.key
 +
sudo apt-key add nginx_signing.key
 +
 +
# finally install nginx
 +
sudo apt update && sudo apt install nginx-full -y
 +
</source>
  
 
== Config ==
 
== Config ==
=== Basic ===
+
=== Config sites file ===
 +
backup site files in sites-available, activatived sites in sites-enabled, creat soft link from sites-available to sites-enabled.
 
* default config file:  nano /etc/nginx/sites-available/default
 
* default config file:  nano /etc/nginx/sites-available/default
 +
* create soft link to sites-enabled by ls -n /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
 +
 
* link enable config file: /etc/nginx/sites-enabled/default
 
* link enable config file: /etc/nginx/sites-enabled/default
 
* delete etc/nginx/sites-avaialble/default
 
* delete etc/nginx/sites-avaialble/default
Line 26: Line 51:
 
* set server_name www.example.com example.com;
 
* set server_name www.example.com example.com;
  
=== Furture Config ===
+
=== Futher Config ===
 
{| class="wikitable sortable"
 
{| class="wikitable sortable"
 
|-
 
|-
Line 87: Line 112:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== PHP Configure ==
 
====  use this latest config ====
 
* File at /etc/nginx/sites-available/default
 
<syntaxhighlight lang="c">
 
server {
 
    listen 80 default_server;
 
    listen [::]:80 default_server;
 
 
    root /var/www/html;
 
    index index.php index.html index.htm index.nginx-debian.html;
 
 
    server_name your_server_ip;
 
 
    location / {
 
        try_files $uri $uri/ =404;
 
    }
 
 
    location ~ \.php$ {
 
        include snippets/fastcgi-php.conf;
 
        fastcgi_pass unix:/var/run/php5-fpm.sock;
 
    }
 
 
    location ~ /\.ht {
 
        deny all;
 
    }
 
}
 
</syntaxhighlight>
 
 
==== Old ====
 
<syntaxhighlight lang="c">
 
server {
 
    listen 80 default_server;
 
    listen [::]:80 default_server ipv6only=on;
 
 
    root /usr/share/nginx/html;
 
    index index.php index.html index.htm;
 
 
    server_name server_domain_name_or_IP;
 
 
    location / {
 
        try_files $uri $uri/ =404;
 
    }
 
 
    error_page 404 /404.html;
 
    error_page 500 502 503 504 /50x.html;
 
    location = /50x.html {
 
        root /usr/share/nginx/html;
 
    }
 
 
    location ~ \.php$ {
 
        try_files $uri =404;
 
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
 
        fastcgi_pass unix:/var/run/php5-fpm.sock;
 
        fastcgi_index index.php;
 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 
        include fastcgi_params;
 
    }
 
}
 
</syntaxhighlight>
 
 
Let's copy the follow code into the default to enable php, compare to the code at [[Nginx on pcduino]], it's php-enabled, you can read it on this part "location ~ \.php$".
 
 
<syntaxhighlight lang="php">
 
server {
 
        listen  80; ## listen for ipv4; this line is default and implied
 
        listen  [::]:80 default ipv6only=on; ## listen for ipv6
 
 
        root /usr/share/nginx/www;
 
        index index.php index.html index.htm;
 
 
        # Make site accessible from http://localhost/
 
        server_name _;
 
 
        location / {
 
                # First attempt to serve request as file, then
 
                # as directory, then fall back to index.html
 
                try_files $uri $uri/ /index.html;
 
                # Uncomment to enable naxsi on this location
 
                # include /etc/nginx/naxsi.rules
 
        }
 
 
        location /doc/ {
 
                alias /usr/share/doc/;
 
                autoindex on;
 
                allow 127.0.0.1;
 
                deny all;
 
        }
 
 
        # Only for nginx-naxsi : process denied requests
 
        #location /RequestDenied {
 
                # For example, return an error code
 
                #return 418;
 
        #}
 
 
        #error_page 404 /404.html;
 
 
        # redirect server error pages to the static page /50x.html
 
        #
 
        error_page 500 502 503 504 /50x.html;
 
        location = /50x.html {
 
                root /usr/share/nginx/www;
 
        }
 
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 
        #
 
        location ~ \.php$ {
 
                try_files $uri =404;
 
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
 
                fastcgi_pass 127.0.0.1:9000;
 
                fastcgi_index index.php;
 
                include fastcgi_params;
 
        }
 
 
        # deny access to .htaccess files, if Apache's document root
 
        # concurs with nginx's one
 
        #
 
        location ~ /\.ht {
 
                deny all;
 
        }
 
}
 
</syntaxhighlight>
 
 
And now when we navigate to 127.0.0.1/index.php (or info.php), we will see the info like below:<br />
 
[[File:Phpinfo.png|400px]]
 
 
 
* Create PHP test file  -> sudo nano /usr/share/nginx/html/info.php
 
<syntaxhighlight lang="c">
 
<?php
 
phpinfo();
 
?>
 
</syntaxhighlight>
 
 
=== Configuration ===
 
We're going to start moving about in Linux now. Remember that Linux still has a folder structure - just like any graphical operating system. If you want to learn more about the folder structure of a Linux system - there are some great tutorials and images that can be found through a quick Google search.<br />
 
 
Let's move to where nginx stores the sites which it hosts, by using the command:
 
cd /etc/nginx/sites-enabled
 
or try at /etc/nginx/sites-available
 
 
And let's edit the file "default", delete everything in it, and copy the following code into it, you can do this with editor like [[nano]] or GUI editor:
 
server {
 
listen 80; ## listen for ipv4; this line is default and implied
 
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
 
 
root /usr/share/www/example;
 
index index.php index.html index.htm;
 
 
location / {
 
error_page 404 = 404.html;
 
}
 
 
# Make site accessible from http://localhost/
 
server_name example.com *.example.com;
 
 
location /doc/ {
 
alias /usr/share/doc/;
 
autoindex on;
 
allow 127.0.0.1;
 
allow ::1;
 
deny all;
 
}
 
}
 
 
=== Build the website ===
 
Now, we're actually going to make a little website in the /usr/share/nginx/www/example folder we made earlier.
 
 
Let's move to the folder by using the command:
 
 
cd /usr/share/nginx/www/example
 
 
We're going to make two files - an index page (homepage), and a 404 page (a page to display if the page a user is trying to access doesn't exist).
 
 
Let's first make the index page by using the command:
 
 
pico index.html
 
 
If you're familiar with html, at this point type in whatever you want. If you're not, then paste in the following, save and exit (as you did earlier with the nginx configuration file):
 
 
<html>
 
Hello world, this is my first website!
 
</html>
 
 
Next, do the same for the 404 page by using the command:
 
 
pico 404.html
 
 
Paste in the following if unsure:
 
<html>
 
Sorry, the page you are looking for doesn't exist!
 
</html>
 
 
The final step is to make sure that our pages are readable for others by changing the permissions on the files. If we didn't change the permissions, someone accessing your website may be given the 'You are not allowed to view this page' (or equivalent) error generated by nginx.
 
 
Use the command:
 
 
chmod -R 775 /usr/share/nginx/www/example
 
 
=== Detailed Configuring nginx ===
 
The nginx configuration is in /etc/nginx/nginx.conf which we open now:
 
vi /etc/nginx/nginx.conf
 
 
The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here: http://wiki.nginx.org/NginxFullExample2)
 
 
First (this is optional) adjust the number of worker processes and set the keepalive_timeout to a reasonable value:
 
[...]
 
worker_processes  4;
 
[...]
 
    keepalive_timeout  2;
 
[...]
 
=== Advanced ===
 
* basedir
 
#防跨站设置
 
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/data/tmp/php/upload/:/proc/:/";
 
* rewrite root to /k, add line in location / {  }
 
#return 301 /k;
 
 
== Log ==
 
== Log ==
 
* /var/log/nginx
 
* /var/log/nginx
Line 308: Line 117:
 
upstream prematurely closed connection while reading response header from upstream
 
upstream prematurely closed connection while reading response header from upstream
 
* Reference - https://stackoverflow.com/questions/27396248/uwsgi-nginx-flask-upstream-prematurely-closed
 
* Reference - https://stackoverflow.com/questions/27396248/uwsgi-nginx-flask-upstream-prematurely-closed
[[category: website]]
+
[[category: web]]

Latest revision as of 00:56, 2 December 2019

install

Remove apache2

  • In case for any error, stop apache first:
sudo service apache2 stop
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo apt-get autoremove
  • apt-get install nginx
  • allow gfw -> sudo ufw allow 'Nginx HTTP'
  • Test Welcome to Nginx.png

Restart

  • service nginx start
  • check start status -> systemctl status nginx.service
  • systemctl restart nginx
  • Default root folder /var/www/html


Full remove

  • sudo apt-get remove nginx nginx-common # Removes all but config files.
  • sudo apt-get purge nginx nginx-common # Removes everything.
  • sudo apt-get autoremove

Upgrade

  1. Add the sources
sudo echo "
deb http://packages.dotdeb.org jessie all
deb http://nginx.org/packages/mainline/debian/ jessie nginx" | sudo tee -a /etc/apt/sources.list

# Add the keys to our system so we trust packages from the sources we added
cd /tmp
wget http://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

# finally install nginx
sudo apt update && sudo apt install nginx-full -y

Config

Config sites file

backup site files in sites-available, activatived sites in sites-enabled, creat soft link from sites-available to sites-enabled.

  • default config file: nano /etc/nginx/sites-available/default
  • create soft link to sites-enabled by ls -n /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
  • link enable config file: /etc/nginx/sites-enabled/default
  • delete etc/nginx/sites-avaialble/default
  • edit /etc/nginx/sites-enabled/default
  • set server_name www.example.com example.com;

Futher Config

Steps Header text Header text
Nginx config /etc/nginx/nginx.conf
Default site config /var/www/html -> defined at /etc/nginx/sites-enabled/default -
  • create file e.g. /etc/ningx/sites-enabled/iot.electrodragon.com.conf
  • Optimize -> delete other conf file? FastCGI cache?, gzip missing?

Optimized

NGINX Config

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

	root /usr/share/nginx/www;  # root of user file
	index index.html index.htm; # file name

	server_name localhost; # e.g. www.electrodragon.com

	location / {
		try_files $uri $uri/ /index.html;
	}

	location /doc/ {
		alias /usr/share/doc/;
		autoindex on;
		allow 127.0.0.1;
		deny all;
	}
}

Setup for uwsgi

server {
        listen 80;

        server_name www.portf.ltd portf.ltd;

        # root /var/www/html;
        # index index.html;

        location / {
                # try_files $uri $uri/ =404;
                include uwsgi_params; 
                uwsgi_pass unix:/var/www/html/flask-test/sock.sock;
        }
}

Log

  • /var/log/nginx

Error Fix

upstream prematurely closed connection while reading response header from upstream