Nginx是一款高性能的http服务器/反向代理服务器。官方测试可以支持5万的并发
http服务器
虚拟主机。可以实现在一台服务器虚拟出多个网站。
反向代理,负载均衡。
官网:http://nginx.org
nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。
gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++
PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,
所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能
及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),
所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
将nginx-1.8.0.tar.gz拷贝至linux服务器。
解压:
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
1.configure
./configure –help查询详细参数(参考本教程附录部分:nginx编译参数)
参数设置如下:
[root@localhost nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下先创建temp及nginx目录
2.编译安装
make
make install
安装成功查看安装目录 :
[root@localhost nginx-1.8.0]# cd /usr/local
[root@localhost nginx-1.8.0]# ll
[root@localhost nginx-1.8.0]# cd nginx/
3.启动nginx
[root@localhost nginx-1.8.0]# cd /usr/local/nginx/sbin/
[root@localhost nginx-1.8.0]# ./nginx
查询nginx进程:
[root@localhost nginx-1.8.0]# ps aux|grep nginx
15098是nginx主进程的进程id,15099是nginx工作进程的进程id
注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(–conf-path= 指向配置文件(nginx.conf))
4.停止nginx
方式1,快速停止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
方式2,完整停止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
5.重启nginx
方式1,先停止再启动(建议使用):
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:
./nginx -s quit
./nginx
方式2,重新加载配置文件:
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
./nginx -s reload
6.测试
nginx安装成功,启动nginx,即可访问虚拟机上的nginx:
到这说明nginx上安装成功。
7.开机自启动nginx
7.1编写shell脚本
这里使用的是编写shell脚本的方式来处理
vi /etc/init.d/nginx (输入下面的代码)
:wq 保存并退出
7.2 设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
7.3加入到rc.local文件中
vi /etc/rc.local
加入一行
/etc/init.d/nginx start
保存并退出,下次重启会生效。
[jianyiwei@192 ~]$ cd /usr/local/
[jianyiwei@192 local]$ ll
total 44
drwxr-xr-x. 2 root root 4096 Oct 28 18:01 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 3 root root 4096 Oct 28 18:01 lib64
drwxr-xr-x. 3 root root 4096 Oct 28 18:01 libexec
drwxr-xr-x. 11 root root 4096 Oct 28 13:51 nginx
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 11 root root 4096 Oct 28 18:01 share
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
[jianyiwei@192 local]$ cd nginx/
[jianyiwei@192 nginx]$ ll
total 36
drwx------. 2 nobody root 4096 Oct 28 13:51 client_body_temp
drwxr-xr-x. 3 root root 4096 Jan 8 21:16 conf
drwx------. 2 nobody root 4096 Oct 28 13:51 fastcgi_temp
drwxr-xr-x. 2 root root 4096 Oct 28 13:49 html
drwxr-xr-x. 2 root root 4096 Jan 8 20:52 logs
drwx------. 2 nobody root 4096 Oct 28 13:51 proxy_temp
drwxr-xr-x. 2 root root 4096 Jan 8 21:30 sbin
drwx------. 2 nobody root 4096 Oct 28 13:51 scgi_temp
drwx------. 2 nobody root 4096 Oct 28 13:51 uwsgi_temp
[jianyiwei@192 nginx]$ cd conf/
[jianyiwei@192 conf]$ ll
total 64
-rw-r--r--. 1 root root 1077 Oct 28 13:49 fastcgi.conf
-rw-r--r--. 1 root root 1077 Jan 8 20:50 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Oct 28 13:49 fastcgi_params
-rw-r--r--. 1 root root 1007 Jan 8 20:50 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Jan 8 20:50 koi-utf
-rw-r--r--. 1 root root 2223 Jan 8 20:50 koi-win
-rw-r--r--. 1 root root 3957 Oct 28 13:49 mime.types
-rw-r--r--. 1 root root 3957 Jan 8 20:50 mime.types.default
-rw-r--r--. 1 root root 2686 Jan 8 21:16 nginx.conf
-rw-r--r--. 1 root root 2656 Jan 8 20:50 nginx.conf.default
-rw-r--r--. 1 root root 636 Oct 28 13:49 scgi_params
-rw-r--r--. 1 root root 636 Jan 8 20:50 scgi_params.default
-rw-r--r--. 1 root root 664 Oct 28 13:49 uwsgi_params
-rw-r--r--. 1 root root 664 Jan 8 20:50 uwsgi_params.default
drwxr-xr-x. 2 root root 4096 Jan 8 21:24 vhost
-rw-r--r--. 1 root root 3610 Jan 8 20:50 win-utf
[jianyiwei@192 conf]$ cd ..
[jianyiwei@192 nginx]$ cd html/
[jianyiwei@192 html]$ ll
total 8
-rw-r--r--. 1 root root 537 Oct 28 13:49 50x.html
-rw-r--r--. 1 root root 612 Oct 28 13:49 index.html
[jianyiwei@192 html]$
nginx.conf文件
#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;
}
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"';
23,5 Top
vi环境快复制:
shift+v 进入行选模式
上下箭头选要复制的行
y复制
p粘贴
u撤销