一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
三. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
四. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
官网下载
1.直接下载.tar.gz
安装包,地址:https://nginx.org/en/download.html
或者使用wget
命令下载(推荐)。
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
解压
依然是直接命令:
tar -zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1
配置
其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置
./configure
2.自定义配置(不推荐)
./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/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目录
编译安装
make make install
查找安装路径:
whereis nginx
启动、停止nginx
cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload./nginx -s quit
:此方式停止步骤是待nginx进程处理任务完毕进行停止。./nginx -s stop
:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
查询nginx进程:
ps aux|grep nginx
重启 nginx
1.先停止再启动(推荐):
对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:
./nginx -s quit
./nginx
2.重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload
不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
./nginx -s reload
3、强制关闭:
pkill nginx
到这里,nginx就安装完毕了,启动、停止、重启操作也都完成了。
用rpm命令安装pcre报错:
缺少C++编译环境:
1、安装g++:
(1)下载gcc-c++rpm包
rpm -ivh gcc-c++
缺少:libstdc++-4.8.5-16.el7_4.2.x86_64.rpm和libstdc++-devl-4.8.5-16.el7_4.2.x86_64.rpm
(2)下载libstdc++-4.8.5-16.el7_4.2.x86_64.rpm和libstdc++-devl-4.8.5-16.el7_4.2.x86_64.rpm
rpm -ivh libstdc++-4.8.5-16.el7_4.2.x86_64.rpm报错:文件冲突
解决办法:rpm -ivh --force --nodeps libstdc++-4.8.5-16.el7_4.2.x86_64.rpm
(3)安装libstdc++-devl
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nginx配置参数:
Nginx Users and Groups:
命令参数
|
描述
|
--user=USER
|
指定Nginx运行的用户
|
--group=GROUP
|
指定Nginx运行的组
|
Nginx 配置目录设置:
命令参数
|
描述
|
--prefix=PATH
|
Nginx服务器路径,除配置和库以外的所有文件。默认路径是/usr/local/nginx,而基于软件包管理器的安装使用/etc/nginx目录。
|
--sbin-path=PATH
|
Nginx可执行文件路径,这个路径托管nginx可执行文件。
默认路径是/usr/local /nginx/sbin/nginx,而基于软件包管理器的安装使用/usr/sbin /nginx目录。
|
--conf-path=PATH
|
Nginx配置路径,这个路径将主机nginx.conf,mime.type和其他各种配置文件。
默认情况下,该文件位于/usr /local/nginx/conf /中,软件包管理器安装将文件托管在/ etc/nginx/中。
|
--pid-path=PATH
|
Nginx进程nginx.pid文件,这个文件将存储Nginx进程的进程ID。
默认情况下,该文件位于/usr/local/nginx/logs/,软件包管理器安装的使用路径是/var/run/。
|
--lock-path=PATH
|
Nginx锁定文件nginx.lock文件,该文件包含特定进程已经使用的资源的锁定信息。
默认情况下,该文件位于/usr/local/nginx/logs,软件包管理器使用路径/var /run/。
|
Nginx日志目录选项:
命令参数
|
描述
|
--error-log-path=PATH
|
Nginx服务器错误日志路径,该文件将包含将包含错误,警告和诊断错误输出的文件名。默认目录:/usr/loacl/nginx/logs/error.log。
|
--http-log-path=PATH
|
Nginx服务器访问日志路径,该文件将包含Nginx托管网站的HTTP请求日志详细信息。默认情况下,HTTP访问和错误日志文件目录是/usr /local /nginx /logs/,软件包管理器在/var/log /nginx/上安装日志文件。
|
--http-client-body-temp-path=PATH
|
HTTP请求的临时文件位置。 它拥有客户端请求的报文体。
|
--http-fastcgi-temp-path=PATH
|
HTTP FastCGI,uWSGI,SCGI应用程序的临时文件位置。 该位置存储从FastCGI,uWSGI和SCGI服务器接收到的数据的临时文件。
默认情况下,临时路径文件夹在/usr/ local / nginx / logs /下创建,软件包管理器配置/ var / cache / nginx /目录。
|
--http-uwsgi-temp-path=PATH
| |
--http-scgi-temp-path=PATH
|
安装Nginx时可以启用的所有http模块:
Command Parameter
|
Description
|
--with-http_ssl_module
|
This module provides support for HTTPS websites; this module requires OpenSSL to be enabled.
|
--with-http_realip_module
|
This module is used to get the real IP address of the client that is specified in the HTTP header.
|
--with-http_addition_module
|
This module is a filter module that is used to add text before and after a response.
|
--with-http_sub_module
|
This module is a substitution filter, and it is used to replace one specified string by another.
|
--with-http_dav_module
|
This module is used to enable WebDAV feature for file management using PUT, DELETE, MKCOL, COPY, and MOVE methods.
|
--with-http_flv_module
|
This module enables pseudo-streaming functionality for flash video file.
|
--with-http_mp4_module
|
This module enables pseudo-streaming functionality for .mp4, .m4v, or .m4a files.
|
--with-http_gunzip_module
|
This module is used to decompress content for clients that do not a support gzip encoding method.
|
--with-http_gzip_static_module
|
This module allows sending precompressed files with the .gz filename extension.
|
--with-http_random_index_module
|
This module picks a random file in a directory to serve as an index file.
|
--with-http_secure_link_module
|
This module is used to authenticate the requested link using the hash to protect resource from unauthorized access.
|
--with-http_stub_status_module
|
This module provides access to basic status information.
|
--with-http_auth_request_module
|
This module implements client authorization based on response code.
|
--with-http_spdy_module
|
This module provides support for Googles SPDY protocol. This is now a deprecated and is replaced by the newer HTTP/2 module.
|
--with-http_xslt_module
|
This is a filter module that transforms XML responses using XSLT stylesheets.
|
--with-http_image_filter_module
|
This is a filter module that is used to process JPEG, GIF and PNG images.
|
--with-http_geoip_module
|
This module is used for geo-targeting, it looks for client IP and compares it with the precompiled MaxMind database.
|
--with-http_degradation_module
|
This module is used to serve a particular error message when the server faces a low memory issue.
|
Command Parameter
|
Description
|
--without-http_charset_module
|
Disables re-encoding Charset to another.
|
--without-http_gzip_module
|
Disables gzip compression module.
|
--without-http_ssi_module
|
Disable Server Side Include module.
|
--without-http_userid_module
|
Disables cookie-based client identification .
|
--without-http_access_module
|
Disables client IP address based access filtering.
|
--without-http_auth_basic_module
|
Disables HTTP Basic Authentication module that is used to validate user access.
|
--without-http_autoindex_module
|
Disables directory listing of the website.
|
--without-http_geo_module
|
Disables geo-module allowing you to define variables with values depending on the client IP address.
|
--without-http_map_module
|
Disables map module that is used to define map blocks.
|
--without-http_split_clients_module
|
Disables split testing module.
|
--without-http_referer_module
|
Disables blocking access to sites for requests with invalid “Referer” header.
|
--without-http_rewrite_module
|
Disables HTTP rewrite module.
|
--without-http_proxy_module
|
Disables HTTP proxy module used to redirect to another server.
|
--without-http_fastcgi_module
|
Disables FastCGI module.
|
--without-http_uwsgi_module
|
Disables uWSGI module.
|
--without-http_scgi_module
|
Disables SCGI module.
|
--without-http_memcached_module
|
Disables memcached module that is used to obtain responses from memcached server.
|
--without-http_limit_conn_module
|
Disables connection limit set as per the defined key rule.
|
--without-http_limit_req_module
|
Disables request processing limit set as per the defined key.
|
--without-http_empty_gif_module
|
Disables transferring single pixel transparent gif.
|
--without-http_browser_module
|
Disables identifying User Agent from request header field.
|
--without-http_upstream_hash_module
|
Disables HTTP load balancing method for server group where the client-server mapping is based on the hashed key value.
|
--without-http_upstream_ip_hash_module
|
Disables HTTP load balancing where requests are distributed across group of servers based on client IP address.
|
--without-http_upstream_least_conn_module
|
Disables HTTP load balancing where a request is passed to the server with the least number of active connections.
|
--without-http_upstream_keepalive_module
|
Disables cache for connections to upstream servers.
|
--without-mail_pop3_module
|
Disables POP3 module for mail server proxy.
|
--without-mail_imap_module
|
Disables IMAP module for mail server proxy.
|
--without-mail_smtp_module
|
Disabled SMTP module for mail server proxy .
|
优化模块,Nginx可用的不同优化参数:
Command Parameter
|
Description
|
--with-cc=PATH
|
Used to specify alternate location for C compiler.
|
--with-cpp=PATH
|
Used to specify alternate location for C preprocessor.
|
--with-cc-opt=OPTIONS
|
Used to add parameters that will be added to the CFLAGS variable.
|
--with-ld-opt=OPTIONS
|
Used to define additional parameters that will be used during linking.
|
--with-cpu-opt=CPU
|
Used to specify different processor architecture.
|
Command Parameter
|
Description
|
--with-md5=DIR
|
Specifies the path to md5 library sources.
|
--with-md5-opt=OPTIONS
|
Used to specify additional build options for md5.
|
--with-md5-asm
|
Uses md5 assembler sources.
|
SHA1选项
Command Parameter
|
Description
|
--with-sha1=DIR
|
Specifies the path to sha1 library sources.
|
--with-sha1-opt=OPTIONS
|
Used to specify additional build options for sha1.
|
--with-sha1-asm
|
Uses sha1 assembler sources .
|
zlib选项
Command Parameter
|
Description
|
--with-zlib=DIR
|
Specifies path of the zlib libraries, used when using specific version of zlib libraries.
|
--with-zlib-opt=OPTIONS
|
Used to specify additional build options for zlib.
|
--with-zlib-asm=CPU
|
Uses zlib assembler sources that are optimized for Pentium or Pentiumpro CPU architecture.
|
OpenSSL选项
Command Parameter
|
Description
|
--with-openssl=DIR
|
Specifies path of the OpenSSL libraries, used when using specific version of OpenSSL libraries.
|
--with-openssl-opt=OPTIONS
|
Used to specify additional build options for OpenSSL.
|
Libatomic选项
Command Parameter
|
Description
|
--with-libatomic
|
Forces libatomic libraries.
|
--with-libatomic=DIR
|
Specifies path of the libatomic libraries, used when using specific version of libatomic libraries .
|
Other 选项
Command Parameter
|
Description
|
--without-http
|
Disables HTTP server.
|
--without-http-cache
|
Disables HTTP cache.
|
--with-threads
|
Enables thread pool support.
|
--with-file-aio
|
Enables support for asynchronous disk IO operations.
|
--with-ipv6
|
Enables ipv6 support.
|
--build=NAME
|
Sets the build name.
|
--builddir=DIR
|
Sets the build location.
|
--add-module=PATH
|
Used to add third-party modules during compiling Nginx. You will specify the path of the module in this parameter.
|
--with-debug
|
Enables debug logging.
|
3.安装前配置:
./configure 后面加上述你想要的参数,如果什么都加就是全部默认。