nginx的官方网站:http://www.nginx.org/
- Mainline version: 主线版,即开发版
- Stable version: 最新稳定版,生产环境上建议使用的版本
- Legacy versions: 遗留的老版本的稳定版
一、编译安装
1.安装编译环境
[root@localhost ~]#yum -y install gcc gcc-c++
2.安装pcre软件包(使nginx支持http rewrite模块)
[root@localhost ~]#yum install -y pcre pcre-devel
3.安装openssl-devel(使nginx支持ssl)
[root@localhost ~]#yum install -y openssl openssl-devel
4.安装zlib
[root@localhost ~]#yum install -y zlib zlib-devel
5.创建用户nginx
[root@localhost ~]#useradd nginx
[root@localhost ~]#passwd nginx
可以忽略。
6.nginx安装
把对应的安装包放在/usr/local/下面,列如:nginx-1.18.0.tar.gz
[root@localhost ~]#cd /usr/local/
[root@localhost ~]# tar xzf nginx-1.18.0.tar.gz
[root@localhost ~]# cd /usr/local/nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@localhost nginx-1.18.0]# make && make install
# 查看 nginx 安装的模块
[root@localhost ~]# cd /root
[root@localhost ~]#/usr/local/nginx/sbin/nginx -V
二、nginx配置
Nginx配置文件为/etc/nginx/nginx.conf,nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
# 全局参数设置
user nginx; #设置nginx使用的用户
worker_processes 4; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
error_log logs/error.log; #指定错误日志
worker_rlimit_nofile 1024; #设置一个nginx进程能打开的最大文件数
pid /var/run/nginx.pid;
events {
worker_connections 1024; #设置一个进程的最大并发连接数
}
# http 服务相关设置
http {
include mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main; #设置访问日志的位置和格式
sendfile on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
gzip on; #是否开启gzip压缩,将注释去掉开启
keepalive_timeout 65; #设置长连接的超时时间
# 虚拟服务器的相关设置
server {
listen 80; #设置监听的端口
server_name localhost; #设置绑定的主机名、域名或ip地址
charset koi8-r; # 设置编码字符
location / {
root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建
index index.html index.htm; #设置默认打开的文档
}
error_page 500 502 503 504 /50x.html; #设置错误信息返回页面
location = /50x.html {
root html; #这里的绝对位置是/usr/local/nginx/html
}
}
}
检测nginx配置文件是否正确
[root@localhost ~]#mkdir -p /usr/local/nginx/logs
[root@localhost ~]# mkdir -p /var/www/nginx
[root@localhost ~]# mkdir -p /tmp/nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@localhost ~]#chown -R nginx:nginx /usr/local
[root@localhost ~]#chown -R nginx:nginx /etc/nginx
[root@localhost ~]#chown -R nginx:nginx /var/log/nginx
[root@localhost ~]#chown -R nginx:nginx /tmp/nginx
三、nginx服务启停及常用命令
nginx服务启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
nginx -s reload # 修改配置后重新加载生效
nginx -s stop # 快速停止nginx
nginx -s quit # 完整有序的停止nginx
nginx -t # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
注意:
nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件
1. Nginx的master进程检查配置文件的正确性,若是错误则返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)
2. Nginx启动新的worker进程,采用新的配置文件
3. Nginx将新的请求分配新的worker进程
4. Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程
5. 重复上面过程,知道全部旧的worker进程都被关闭掉
把nginx添加到服务
vim /etc/init.d/nginx
添加下面脚本内容
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
授权并添加服务
chmod +x /etc/init.d/nginx
chkconfig --add nginx
常用命令
# 开机配置
systemctl enable nginx # 开机自动启动
systemctl disable nginx # 关闭开机自动启动
# 启动Nginx
systemctl start nginx # 启动Nginx成功后,可以直接访问主机IP,此时会展示Nginx默认页面
# 停止Nginx
systemctl stop nginx
# 重启Nginx
systemctl restart nginx
# 重新加载Nginx
systemctl reload nginx
# 查看 Nginx 运行状态
systemctl status nginx
# 查看Nginx进程
ps -ef | grep nginx
# 杀死Nginx进程
kill -9 pid # 根据上面查看到的Nginx进程号,杀死Nginx进程,-9 表示强制结束进程