linux系统为Centos6.9 64位
一、创建nginx安装目录
mkdir /usr/local/nginx
二、下载nginx压缩包并解压
从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)
上传到opt文件夹内
三、配置nginx安装所需的环境
1. g安装cc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。安装指令如下:
yum install gcc-c++
2. 安装PCRE pcre-devel
Nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。这里需要安装两个安装包pcre和pcre-devel。第一个安装包提供编译版本的库,而第二个提供开发阶段的头文件和编译项目的源代码。安装指令如下:
yum install -y pcre pcre-devel
3.安装zlib
zlib库提供了开发人员的压缩算法,在Nginx的各种模块中需要使用gzip压缩。安装指令如下:
yum install -y zlib zlib-devel
4.安装Open SSL
nginx不仅支持 http协议,还支持 https(即在 ssl 协议上传输 http),如果使用了 https,需要安装 OpenSSL 库。安装指令如下:
yum install -y openssl openssl-devel
四、解压并安装nginx
tar -zxvf nginx-1.8.1.tar.gz #进入压缩包存放目录解压
cd /opt/nginx-1.8.1 #解压后进入文件夹
设置Nginx安装路径,如果没有指定,默认为/usr/local/nginx
./configure --prefix=/usr/local/nginx
安装完成后如下图:
五、编译安装nginx
在/usr/local/nginx-1.8.1文件夹进行编译
make #make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件
然后安装
make install #make install是把这些编译出来的可执行文件和库文件复制到合适的地方
编译成功之后会发现 nginx的安装目录 /usr/local/nginx内多了几个文件夹,入下图:
六、启动nginx
1、参数 -c 指定了配置文件的路径,如果不加的话就是使用默认的配置文件
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2、或者进入/usr/local/nginx/sbin文件夹
./nginx
七、停止nginx
查询nginx主进程号
ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
强制停止Nginx
pkill -9 进程号 #杀进程
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型 '/usr/nginx/logs/nginx.pid'
八、平滑重启
/usr/local/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。
九、判断Nginx配置是否正确命令
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
/usr/local/nginx/sbin/nginx -t
如下图:
十、访问
在浏览器中输入IP:端口号(默认80),出现如下图所示,说明安装成功。
十一、加了中文注解的nginx.conf
user www www;
# 工作进程个数,可配置多个
worker_processes auto;
error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
# 单个进程最大连接数
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;
#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
######################## default ############################
# 服务器集群名称 和下面的location地址对应
upstream myServer {
# weigth参数表示权值,权值越高被分配到的几率越大
# server 127.0.0.1:8080 weight=1;
# server 127.0.0.1:8060 weight=1;
server 47.93.10.184:8080;
server 47.93.10.184:8081;
}
# 每一个server相当于一个代理服务器
server {
# 监听端口,默认80
listen 8848;
# 当前服务的域名,可以有多个,用空格分隔(我们是本地所以是localhost) www.kolbe.cn
server_name localhost;
#server_name _;
access_log /data/wwwlogs/access_nginx.log combined;
root /data/wwwroot/default;
# 当没有指定主页时,默认会选择这个指定的文件,可多个,空格分隔
index index.html index.htm index.php;
# 表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
location / {
# 请求转向自定义的服务器列表
proxy_pass http://myServer;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
########################## vhost #############################
include vhost/*.conf;
}