0
点赞
收藏
分享

微信扫一扫

Ubuntu20.04安装配置Nginx nginx/1.18.0

nginx版本: nginx/1.18.0 (Ubuntu)

1. 安装nginx

1.1 安装及常用命令

# 更新apt-get源
sudo apt-get update
# 安装
sudo apt-get install nginx
# 安装后将自动开启nginx服务,打开浏览器输入ip即可查看初始页面

# 查看安装版本
nginx -v
# 输出:nginx version: nginx/1.18.0 (Ubuntu)


nginx默认安装目录为 /etc/nginx/

#进入目录
cd /etc/nginx/

# systemctl命令
# 查看状态
sudo systemctl status nginx
# 启动
sudo systemctl start nginx
# 停止
sudo systemctl stop nginx
# 重启
sudo systemctl restart nginx

注意:对nginx配置文件修改之后,都要重启nginx服务,加载修改后的配置文件

1.2 文件结构

# 查看文件结构
安装命令
sudo apt  install tree
查看树形机构
tree /etc/nginx

/etc/nginx
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│   ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│   ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│   ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│   └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

Ubuntu20.04安装配置Nginx nginx/1.18.0_配置文件

1.3 配置文件内容

nginx.conf (为了方便看,我删掉了初始内容中所有带注释的代码)

进入nginx安装目录
cd /etc/nginx/
查看配置文件
vim /etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
}

http {

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;

	include /etc/nginx/conf.d/*.conf;
	#include /etc/nginx/sites-enabled/*;
}

最关键的是下面两行引入

include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;

1.修改主配置文件
vim /etc/nginx/nginx.conf

2.nginx.conf中注释掉 include /etc/nginx/conf.d/*.conf;不变
#include /etc/nginx/sites-enabled/*;

这两行的意思是:从conf.d中加载所有后缀为conf的文件,从sites-enabled中加载所有文件,均作为配置文件

sites-enabled文件我用不习惯,因此我注释掉了这行,使用conf.d做配置文件


1.4配置一个静态网站的配置文件

使用conf.d文件目录做为放置配置文件的地方

在conf.d中添加static.con

vim /etc/nginx/conf.d/static.conf
如下内容
server {
    listen       80;
    server_name  localhost;

    charset utf-8; # 防止中文显示出现乱码

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html; # 你的静态资源路径
        index  index.html index.htm;# 访问的文件为html, htm
    }
}

# 检查配置文件是否有误
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启服务
sudo systemctl restart nginx

Ubuntu20.04安装配置Nginx nginx/1.18.0_配置文件_02



举报

相关推荐

0 条评论