Debian 10安装配置Nginx Light

Nginx Light是Nginx的轻量级版本,在低配置服务器上使用非常适合,例如开发板

安装Nginx Light:

apt install nginx-light

启动Nginx:

systemctl enable nginx

安装UFW(如果还没有安装的话):

apt install ufw

放行SSH端口:

ufw allow 22/tcp

放行HTTP端口:

ufw allow 'Nginx HTTP'

放行HTTPS端口:

ufw allow 'Nginx HTTPS'

也可以使用下面的命令同时放行HTTP和HTTPS端口:

ufw allow 'Nginx Full'

启用防火墙:

ufw enable

查看防火墙端口放行情况:

ufw status

Nginx的默认根目录是:

/var/www

当然,可以自定义根目录,但要注意站点配置文件中root的路径。

创建站点目录:

mkdir -p /var/www/example.com/

设置网站目录所属用户和用户组:

chown -R www-data:www-data /var/www/example.com/

创建站点配置文件:

vi /etc/nginx/sites-available/example.com.conf

HTTP访问的配置大致如下:

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

	root /var/www/example.com/;
	index index.html index.htm index.php;

	server_name example.com www.example.com;

	location / {
		try_files $uri $uri/ /index.php$is_args$args;
	}

    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

HTTPS访问需要配置SSL证书:

server {
	listen 443 ssl;
	listen [::]:443 ssl;

	root /var/www/example.com/;
	index index.html index.htm index.php;

	server_name example.com www.example.com;

	ssl_certificate /etc/ssl/certs/example.com.pem;
	ssl_certificate_key /etc/ssl/private/example.com-key.pem;
	ssl_session_timeout 5m;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        
	location / {
		try_files $uri $uri/ /index.php$is_args$args;
	}
    
    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

创建站点配置文件的链接:

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

还可以在Nginx配置文件中进行其他配置:

/etc/nginx/nginx.conf

重新加载配置:

/etc/init.d/nginx reload

也可以使用:

systemctl reload nginx

如果需要重启Nginx,可以使用以下命令:

/etc/init.d/nginx restart

也可以使用:

systemctl restart nginx

卸载Nginx

停用Nginx:

systemctl disable nginx --now

卸载Nginx:

apt remove nginx

删除配置文件:

rm -R /etc/nginx/
Typora