适用于工作环境中的Nginx
什么是Nginx
Nginx ,是一个 Web 服务器和反向代理服务器,用于 HTTP、HTTPS、SMTP、POP3 和 IMAP 协议。
目前使用的最多的 Web 服务器或者代理服务器,像淘宝、新浪、网易、迅雷等都在使用。
Nginx 的主要功能如下:
作为 http server (代替 Apache ,对 PHP 需要 FastCGI 处理器支持)
FastCGI:Nginx 本身不支持 PHP 等语言,但是它可以通过 FastCGI 来将请求扔给某些语言或框架处理。
反向代理服务器
实现负载均衡
虚拟主机
Nginx 常用命令
启动 nginx
停止 nginx -s stop 或 nginx -s quit
重载配置 ./sbin/nginx -s reload(平滑重启) 或 service nginx reload
重载指定配置文件 .nginx -c /usr/local/nginx/conf/nginx.conf
查看 nginx 版本 nginx -v
检查配置文件是否正确 nginx -t
显示帮助信息 nginx -h
编译安装Nginx服务
[root@192 ~]# cd /opt
[root@192 opt]# rz -E
rz waiting to receive. #软件包提前下载好的
[root@192 opt]# yum -y install gcc gcc-c++ pcre-devel zlib-devel make
[root@192 opt]# tar zxvf nginx-1.12.2.tar.gz
[root@192 opt]# cd nginx-1.12.2/
[root@192 opt]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--with-http_stub_status_module
[root@192 nginx-1.12.2]# make && make install
[root@192 nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@192 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@192 nginx-1.12.2]# vim /usr/lib/systemd/system/nginx.service #添加配置文件
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@192 nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service #给权限
[root@192 nginx-1.12.2]# systemctl start nginx.service #开启
[root@192 nginx-1.12.2]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@192 nginx-1.12.2]# echo "192.168.8.151 www.zsx.com" >> /etc/hosts
使用systemctl管理
修改配置文件
[root@192 nginx-1.12.2]# vim /usr/lib/systemd/system/nginx.service
在配置文件中加入
[root@192 nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service #给予754的权限
[root@192 nginx-1.12.2]# systemctl stop nginx
[root@192 nginx-1.12.2]# systemctl start nginx ##在这里如果服务起不来查看一下是不是进程占用的问题
[root@192 init.d]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19262/nginx: master
[root@192 init.d]# ulimit -n 65535 ##最大文件数
[root@192 init.d]# ulimit -a
修改文件 加入id和网址
[root@192 init.d]# vim /etc/hosts
验证一下
访问状态统计
[root@192 ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
root@192 ~]# cd /usr/local/nginx/conf/
[root@192 conf]# vim /usr/local/nginx/conf/nginx.conf
[root@192 conf]# systemctl restart nginx #重启服务
[root@192 conf]# nginx -t ##查看语法是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 conf]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 75623/nginx: master
验证
访问控制
[root@192 conf]# rpm -q httpd-tools
未安装软件包 httpd-tools
[root@192 conf]# yum -y install httpd-tools
[root@192 conf]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
[root@192 conf]# chown nginx /usr/local/nginx/passwd.db
[root@192 conf]# chmod 400 /usr/local/nginx/passwd.db
[root@192 conf]# cd /usr/local/nginx/
[root@192 nginx]# vim /usr/local/nginx/conf/nginx.conf #修改配置文件
[root@192 nginx]# nginx -t #查看语法是否错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 nginx]# systemctl stop nginx
[root@192 nginx]# systemctl start nginx #重启服务
验证
虚拟主机
基于域名的Nginx虚拟主机
[root@192 nginx]# vim /etc/hosts
添加域名
创建虚拟站点网页文档
[root@192 nginx]# mkdir -p /var/www/html/xumin
[root@192 nginx]# mkdir -p /var/www/html/liqinmei
[root@192 nginx]# echo "<h1> www.xumin.com </h1>" > /var/www/html/xumin/index.html
[root@192 nginx]# echo "<h1> www.liqinmei.com </h1>" > /var/www/html/liqinmei/index.html
[root@192 nginx]# vim /usr/local/nginx/conf/nginx.conf
[root@192 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 nginx]# systemctl restart nginx
验证
基于端口
[root@192 nginx]# mkdir -p /var/www/html/liqinmei8080
[root@192 nginx]# echo "<h1>www.liqinmei8080.com</h1>" >/var/www/html/liqinmei8080/index.html
[root@192 nginx]# vim /usr/local/nginx/conf/nginx.conf
[root@192 nginx]# nginx -t
[root@192 nginx]# systemctl restart nginx
配置文件的修改
基于ip
[root@192 ~]# mkdir -p /var/www/html/liqinmei100
[root@192 ~]# echo "<h1>www.liqinmei100.com</h1>" >/var/www/html/liqinmei100/index.html
[root@192 ~]# vim /etc/hosts
[root@192 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@192 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 ~]# systemctl restart nginx
验证