Nginx Linux部署
以往多次部署过程中总会遇到一些问题,每次遇到问题会浪费时间查询解决方案,此次在新服务器部署中,把部署步骤记录,方便以后查看
1.环境准备
部署之前服务器中已经安装 gcc、make、wget,g++
yum -y install gcc-c++
nginx依赖以下模块:gzip模块需要zlib
库、rewrite模块需要pcre
库、ssl 功能需要openssl
库,一键安装
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
或者直接下载指定版本,新建存放目录
下载openssl
主要用于ssl模块加密,支持htps
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
下载pcre
来实现对地址重定向,地址重写功能和localtion指令以及正则表达式的支持
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
下载zlib
gzip压缩模块
wget https://zlib.net/zlib-1.2.11.tar.gz
下载Nginx 或者官网下载 http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.17.1.tar.gz
在存放目录使用tar 解压所有的文件到
ls *.tar.gz | xargs -n1 tar xzvf
2.使用configure配置Nginx
可选择配置信息,不选择按照默认配置安装,
--prefix //安装路径
--sbin-path //可执行文件安装路径
--conf-path //配置文件路径
--pid-path //放置pid路径
--lock-path //nginx.lock放置路径
--error-log-path //错误日志放置路径
--http-log-path //访问日志路径
--user //启动用户
--group //启动组
--builddir //指定编译的目录
--with-rtsig_module //启动rtsig模块
--with-select-module //允许或不允开启SELECT模式
--with-poll_moduel //允许或不允许开启poll模式
--with-http_ssl_module //开启http_ssl模块
--with-http_realip_module
--with-http_addition_module
--with-http_dav_module
--with-http_flv_module
--with-stub_status_module
--without-http_charset_module
--without-http_gzip_module
--without-http_ssl_module
--without-http_userid_module
--without-http_access_module
--without-http_auth_basic_module
--without-http_autoindex_module
--without-http_geo_module
--without-http_map_module
--without-http_referer_module
--without-http_rewrite_module
--without-http_proxy_module
--without-http_fastcgi_module
--without-http_memcached_module
--without-http_limit_zone_module
--without-http_empty_gif_module
--without-http_browser_module
--without-upstream_ip_hash_module
--with-http_pcel_module
--with-perl=PATH //指定PERL模块路径
--with-log-path //日志访问路径
--http-client-body-temp-path //请求缓存文件
--http-proxy-temp-path //反向代理缓存文件
--http-fastcgi-temp-path //fastcgi缓存文件存放路径
--without-http //禁用httpserver
--with-mail //启用IMAP4/POP3/SMTP代理模式
--with-mail_ssl_module
--with-cc 指定c编译器的路径
--with-cpp 指定c预处理器的路径
--with-cpu-opt 为特定的CPu编译
--without-pcre 禁止PCRE
--with-pcre 指定PCRE库的源代码路径
--with-pcre-opt 设置PCRE的额外编译选项
--with-md5 设置MD5的源代码路径
--with-md5-opt 设置MD5库的额外编译选项
--with-md5-asm 使用MD5汇编源代码
--with-shal 设置shal库的源代码路径
--with-shal-asm 使用shal汇编源码
--with-zlib 设置ZLIB库的源代码路径
--with-zlib-opt 设置zlib库选项
--with-zlib-asm zlib针对CPU的优化
--with-openssl 设置openssl源代码路径
--with-openssl-option 设置openssl库的源代码路径
--with-debug 启动调试日志
--add-module 添加一个指定路径,能找到第三方模块
按照自定义安装
./configure \
--with-openssl=../openssl-1.0.2s \ //自定义安装的openssl ,因为模块都在同一目录
--with-pcre=../pcre-8.43 \ //自定义安装的pcre
--with-zlib=../zlib-1.2.11 \ //自定义安装的zlib
--with-pcre-jit --user=admin \ //admin启动
--prefix=/home/admin/nginx \ //安装在本地的目录
--with-http_ssl_module \ //https
--with-http_v2_module //http
配置执行出现如下,代表配置没问题配置正常
Configuration summary
+ using PCRE library: ../pcre-8.43
+ using OpenSSL library: ../openssl-1.0.2s
+ using zlib library: ../zlib-1.2.11
nginx path prefix: "/usr/local/src/nginx/nginx"
nginx binary file: "/usr/local/src/nginx/nginx/sbin/nginx"
nginx modules path: "/usr/local/src/nginx/nginx/modules"
nginx configuration prefix: "/usr/local/src/nginx/nginx/conf"
nginx configuration file: "/usr/local/src/nginx/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/src/nginx/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/src/nginx/nginx/logs/error.log"
nginx http access log file: "/usr/local/src/nginx/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
3.编译安装
编译
make
安装
make install
配置权限
因为Linux设置普通用户,不能占用1024一下的端口,直接启动nginx会出现权限不足的错误。将nginx分配给root用户,在分配特殊权限
sudo chown root nginx
sudo chmod u+s nginx
或者在配置文件放开第一行注释
user nobody;
4.安装问题
查看安装环境是否完整
5.常用命令
1.指定配置文件启动
//nginx sbin目录 ==》 配置文件位置
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2.重新加载
//nginx sbin目录
/usr/local/nginx/sbin/nginx -s reload
3.指定配置文件重新加载
//nginx sbin目录 ==》 配置文件位置
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/conf/nginx.conf
4.停止
//nginx sbin目录
/usr/local/nginx/sbin/nginx -s stop
5.检查配置是否正确
//nginx sbin目录
/usr/local/nginx/sbin/nginx -t
5.暴力停止
//nginx sbin目录
/usr/local/nginx/sbin/nginx -s stop
6.优雅停止
//nginx sbin目录
/usr/local/nginx/sbin/nginx -s quit