规划:
安装目录 /opt/nginx/
一、创建目录
mkdir -pv /opt/nginx
二、下载安装文件,最新稳定版本(1.10.1)
1. cd /opt/nginx
2. wget -c http://nginx.org/download/nginx-1.10.1.tar.gz
三、工具和依赖(如果已经有了,就不用再安装)
1. yum -y install gcc gcc-c++ autoconf automake
2. yum -y install zlib zlib-devel gd-devel openssl openssl-devel pcre-devel
说明:pcre是用来作地址重写的功能
zlib是nginx的gzip模块,传输数据打包,省流量(但消耗资源)
openssl用来提供ssl加密协议
gd-devel是http_image_filter_module的依赖包
四、用户和组 (非必须步骤)
最好单独建立用户组和用户
1. groupadd -r nginx
2. sudo useradd -s /sbin/nologin -g nginx -r nginx-user
五、配置,安装
1. tar -zxvf nginx-1.10.1.tar.gz
2. ./configure --prefix=/opt/nginx --with-http_image_filter_module --with-stream
说明: --prefix 用来指定安装目录
--with-http_image_filter_module 图片过滤处理模块
--with-stream 带tcp udp协议代理功能模块
3. make
4. make install
六、将nginx添加为自定义系统服务
1. vi /usr/lib/systemd/system/nginx.service
输入如下内容:
[Unit] Description=nginx - High performance web server, proxy server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target |
2. systemctl enable nginx.service
七、 基本操作示例
1. 启动服务
systemctl start nginx.service
2. 重启服务
systemctl restart nginx.service
3. 重载服务
systemctl reload nginx.service
4. 停止服务
systemctl stop nginx.service
八、将nginx命令加入到path和建立软连接,这样就可以直接使用 nginx命令
1. ln -s /opt/nginx/sbin/nginx /usr/local/sbin/nginx
2. vi ~/.bash_profile
在export PATH 后面添加nginx命令的路径,例如
PATH=$HADOOP_PREFIX/bin:$PATH:$HOME/bin:$SPARK_HOME/bin:/opt/nginx/sbin/nginx
九、常用nginx 命令
1. 检查配置文件正确性
nginx -t
2. 重新加载配置文件
nginx -s reload
3. 查看nginx版本和模块信息
nginx -V