nginx
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,。其特点是占有内存少,并发能力强,处理静态请求的能力在众多的http service中算是佼佼者了,特别适用于访问量大,并发量高的网站。nginx本事只能处理静态的网页,如需要处理动态的网页需要借助其它的插件,或者应用容器:比如Tomcat,django...
在生产环境中,它一般不会作为主要的应用服务器,一般会作为用户访问的一个入口,接收用户的请求,通过反向代理把请求交给后端的应用服务器去处理。还可作动静分离,因为nginx处理静态网页的能力非常强,可以把静态的请求交给nginx,动态的请求交给处理动态网页的应用服务器。
一:nginx yum安装:
1、首先确保服务器连接网络:
[root@localhost ~]# ping bing.com -c 4 PING bing.com (204.79.197.200) 56(84) bytes of data. 64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_seq=1 ttl=111 time=39.9 ms 64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_seq=2 ttl=111 time=39.4 ms 64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_seq=3 ttl=111 time=39.9 ms 64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_seq=4 ttl=111 time=39.4 ms
2、设置yum源码:
使用官方的yum源: www.nginx.org
baseurl根据你的操作系统版本号写入:我的操作系统是centos/6版本
[root@localhost ~]# vi /etc/yum.repos.d/nginx.repo [root@localhost ~]# cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1
3、 yum安装nginx:
[root@localhost ~]# yum -y install nginx ##自动从nginx官网下载安装
4、nginx的配置文件:
nginx的主配置文件默认在/etc/nginx/nginx.conf
nginx的网页根目录默认在/usr/share/nginx/html/
[root@localhost ~]# rpm -qal |grep nginx ##查看所有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/rc.d/init.d/nginx /etc/rc.d/init.d/nginx-debug /etc/sysconfig/nginx /etc/sysconfig/nginx-debug /usr/lib64/nginx /usr/lib64/nginx/modules /usr/sbin/nginx /usr/sbin/nginx-debug /usr/share/doc/nginx-1.12.1 /usr/share/doc/nginx-1.12.1/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、启动nginx服务:
[root@localhost ~]# /etc/init.d/nginx start 正在启动 nginx: [确定] ##查看监听端口,默认监听80端口 [root@localhost ~]# netstat -utpln |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16720/nginx
6、访问测试:
linux下测试:
[root@localhost ~]# elinks --dump 192.168.100.150 Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to [1]nginx.org. Commercial support is available at [2]nginx.com. Thank you for using nginx. References Visible links 1. http://nginx.org/ 2. http://nginx.com/
windows下访问:
二:编译安装nginx:
1、获取nginx源码包:
在官网下载指定版本的nginx源码包 http://nginx.org/download/
[root@localhost ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz --2017-08-29 19:34:13-- http://nginx.org/download/nginx-1.6.3.tar.gz 正在解析主机 nginx.org... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ... 正在连接 nginx.org|95.211.80.227|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:805253 (786K) [application/octet-stream] 正在保存至: “nginx-1.6.3.tar.gz” 。。。。。。 2017-08-29 19:34:30 (47.5 KB/s) - 已保存 “nginx-1.6.3.tar.gz” [805253/805253])
[root@localhost ~]# ls anaconda-ks.cfg install.log install.log.syslog nginx-1.6.3.tar.gz
2、解压源码包:
[root@localhost ~]# tar zxf nginx-1.6.3.tar.gz #查看解药后的源码 [root@localhost ~]# ls anaconda-ks.cfg install.log install.log.syslog nginx-1.6.3 nginx-1.6.3.tar.gz [root@localhost ~]# cd nginx-1.6.3 [root@localhost nginx-1.6.3]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
3、创建用户:
[root@localhost ~]# useradd -M -s /sbin/nologin nginx [root@localhost ~]# id nginx uid=500(nginx) gid=500(nginx) 组=500(nginx)
4、安装依赖包:
yum -y install pcre-devel
5、编译安装:
编译时候可以加载模块实现对应的功能,模块功能:
以下介绍引用自:http://www.cnblogs.com/liangml/p/5972470.html
1.通用配置选项: --prefix= |
指定安装目录,指定运行nginx的用户和组,加载模块实现功能,我在这里就加载模块了
[root@localhost ~]# cd nginx-1.6.3 [root@localhost nginx-1.6.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.6.3]# make &&make install
nginx安装目录下分别是nginx的配置文件目录 网页根目录 日志目录 运行命令目录
[root@localhost ~]# ls /usr/local/nginx/ conf html logs sbin
6、优化nginx管理:
nginx的启动文件在/usr/local/nginx/sbin/下,
[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #给nginx命令目录作连接到系统变量里,这样在任何位置都可以执行nginx的命令了。 [root@localhost ~]# nginx -h ##nginx 命令用法帮助 nginx version: nginx/1.6.3 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
作以上操作就是为了实现将下面一条命令,简化为一个命令
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx
7、启动nginx并测试:
[root@localhost ~]# elinks --dump 192.168.100.150
Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to [1]nginx.org. Commercial support is available at [2]nginx.com. Thank you for using nginx. References Visible links 1. http://nginx.org/ 2. http://nginx.com/
8、关闭nginx:
nginx编译没有自带关闭的程序,
可以使用kill + nginx的pid号: 先查看nginx的pid号,然后kill 杀掉查到的nginx的主进程和工作进程的pid
[root@localhost ~]# ps aux |grep nginx root 22012 0.0 0.1 24260 784 ? Ss 20:11 0:00 nginx: master process nginx nginx 22013 0.0 0.2 24684 1368 ? S 20:11 0:00 nginx: worker process root 22015 0.0 0.1 103248 872 pts/0 S+ 20:11 0:00 grep nginx [root@localhost ~]# kill -9 22012 22013 [root@localhost ~]# ps aux |grep nginx root 22017 0.0 0.1 103248 872 pts/0 S+ 20:11 0:00 grep nginx
使用killall nginx 结束nginx所有进程
[root@localhost ~]# nginx [root@localhost ~]# ps aux |grep nginx root 22019 0.0 0.1 24260 784 ? Ss 20:12 0:00 nginx: master process nginx nginx 22020 0.0 0.2 24684 1368 ? S 20:12 0:00 nginx: worker process root 22022 0.0 0.1 103248 872 pts/0 S+ 20:12 0:00 grep nginx [root@localhost ~]# killall nginx [root@localhost ~]# ps aux |grep nginx root 22025 0.0 0.1 103248 868 pts/0 S+ 20:12 0:00 grep nginx
9、编写脚本,管理nginx启动、关闭、重启、重读配置、查看状态等操作
[root@localhost ~]# vi /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Server Control Script NP="/usr/local/nginx/sbin/nginx" NPF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $NP; if [ $? -eq 0 ] then echo "nginx is starting!! " fi ;; stop) kill -s QUIT $(cat $NPF) if [ $? -eq 0 ] then echo "nginx is stopping!! " fi ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $NPF) if [ $? -eq 0 ] then echo "nginx config file is reload! " fi ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
授与执行权限,添加到系统服务
[root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# chkconfig --add nginx
测试脚本。
(在使用脚本的时候,必须要合法,比如说nginx未启动才能启动,nginx启动了才能重启,要不然会报错误。)
[root@localhost ~]# /etc/init.d/nginx start nginx is starting!! [root@localhost ~]# /etc/init.d/nginx stop nginx is stopping!! [root@localhost ~]# /etc/init.d/nginx start nginx is starting!! [root@localhost ~]# /etc/init.d/nginx restart nginx is stopping!! nginx is starting!! [root@localhost ~]# /etc/init.d/nginx reload nginx config file is reload!
END