安装 NGINX
源码安装
- 下载 NGINX
- 解压文件
sudo tar -zxvf nginx-xxx.tar.gz
- 安装依赖
sudo apt-get install gcc zlib1g-dev libpcre3 libpcre3-dev openssl libssl-dev
- 运行
./configure & make & make install
yum 安装
1. 在 nginx 官网找到 yum 地址
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
2. 添加 nginx 地址
vim /etc/yum.repos.d/nginx.repo
3. 安装 nginx
yum install nginx -y
4. 查看安装了那些文件
[root@centos ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.2
/usr/share/doc/nginx-1.14.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
5. 安装目录
路径 | 类型 | 作用 |
---|---|---|
/etc/logrotate.d/nginx | 配置文件 | Nginx 日志轮转,用于 logrotate 服务的日志切割 |
/etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf |
目录 | Nginx 主配置文件 |
/etc/nginx/fastcgi_params | 配置文件 | fastcgi 配置 |
/etc/nginx/uwsgi_params | 配置文件 | uwsgi 配置 |
/etc/nginx/scgi_params | 配置文件 | cgi 配置 |
/etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf |
配置文件 | 编码转换映射转化文件 |
/etc/nginx/mime.types | 配置文件 | 设置 http 协议的 Content-Type 与扩展名对应关系 |
/usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service /etc/sysconfig/nginx /etc/sysconfig/nginx-debug |
配置文件 | 用于配置出系统守护进程管理器管理方式 |
/usr/lig64/nginx/modules /usr/nginx/modules |
目录 | Nginx 模块目录 |
/usr/sbin/nginx/ /usr/sbin/nginx-debug |
命令 | Nginx 服务的启动管理的终端命令 |
/var/cache/nginx | 目录 | Nginx 的缓存目录 |
/var/log/nginx | 目录 | Nginx 的日志目录 |
默认配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
参数 | 作用 |
---|---|
user | 设置 nginx 服务的系统使用用户 |
worker_processes | 工作进程数(和 cpu 核心数保持一致就行了) |
error_log | nginx 错误日志 |
pid | nginx 服务启动的 pid |
events 参数 | 作用 |
---|---|
use | 工作进程数 |
worker_connections | 每个进程允许最大连接数 |
http { //http 的服务配置
...
include mime.types;
default_type application/octet-stream;
// log 输出格式配置
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
// 超时时间配置 单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server { //每一个 server 站点配置
listen 8080; //监听端口
server_name localhost; //主机名、域名
location / { //控制访问路径 这里配置的是 / 路径
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; // 错误页面
location = /50x.html { // 错误页面的路径
root html;
}
}
server {
... ...
}
}
http
模块: http 的服务配置
server
模块: 每一个 server
站点配置
listen
: 监听端口
server_name
: 主机名、域名
location
模块 : 控制访问路径
error_page
: 配置错误页面
检查配置文件是否符合标准
nginx -t -c /usr/local/etc/nginx/nginx.conf
-t
是检查配置是否正确
-c
是指定路径
网友评论