CentOS 下 Nginx 安装与配置。
文章目录
Nginx 安装。
本文是介绍使用源码编译安装。
CentOS 平台。
安装 make。
yum -y install gcc automake autoconf libtool make
安装 g++。
yum install gcc gcc-c++
一般需要先安装 pcre, zlib。
pcre 为了重写 rewrite。
zlib 为了 gzip 压缩。
The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own native API, as well as a set of wrapper functions that correspond to the POSIX regular expression API. The PCRE library is free, even for building proprietary software.
安装 pcre 库。
https://ftp.pcre.org/pub/pcre/
or
cd /usr/local/src
wget ftp://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz # 下载。
tar -zxvf pcre-8.37.tar.gz # 解压。
cd pcre-8.37 # 进入目录。
./configure
make
make install
安装 zlib 库。
cd /usr/local/src
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
安装 ssl。
如果在 vps 使用,某些 vps 默认没有装 ssl。
cd /usr/local/src
wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz
cd openssl-1.0.1t
./config
make
make install
安装 Nginx。
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.37 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.1t
make
make install
./configure 中以 – 开头的 —> 指定参数。
如果不配置参数,默认为
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/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"
安装完成后,nginx 目录在 /usr/local/nginx。
启动 Nginx。
确保系统的 80 端口没有被其他程序占用。
使用 /usr/local/nginx/nginx 命令来启动 Nginx。
netstat -ano | grep 80
如果查不到结果,证明 80 端口没有被占用。执行命令启动 Nginx。
sudo /usr/local/nginx/nginx
# 启动 Nginx。
打开浏览器访问此机器的 ip(如果不输入 :80
,默认端口为 80
)。
Enjoy Yourself,!
Appendix ~ 可能遇到的错误和一些帮助信息。
编译 pcre 错误。
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] Error 1
make[1]: Leaving directory `/usr/local/src/pcre-8.34'
make: *** [all] Error 2
==> resolution.
安装 g++
,并重新 ./configure。
yum -y install openssl openssl-devel
关于编译。
make 是用来编译的,ta 从 makefile 中读取指令,然后编译。
make install 是用来安装的,ta 从 makefile 中读取指令,安装到指定的位置。
configure 命令是用来检测安装平台的目标特征的。
包括 Nginx 被允许使用的连接处理的方法,eg. ta 会检测你是否有 CC 或 gcc。并不是需要 cc 或 gcc,ta 是个 shell 脚本,执行结束时,ta 会创建一个 makefile 文件。
https://www.nginx.cn/install
- –prefix=path -> 定义一个目录,存放服务器上的文件,也就是 nginx 的安装目录。默认使用 /usr/local/nginx。
- –sbin-path=path -> 设置 nginx 的可执行文件的路径,默认为 prefix/sbin/nginx。
- –conf-path=path -> 设置在 nginx.conf 配置文件的路径。nginx 允许使用不同的配置文件启动,通过命令行中的 -c 选项。默认为 prefix/conf/nginx.conf。
- –pid-path=path -> 设置 nginx.pid 文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名, 在 nginx.conf 配置文件中使用 PID 指令。默认情况下,文件名为 prefix/logs/nginx.pid。
- –error-log-path=path -> 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名,在 nginx.conf 配置文件中使用的 error_log 指令。默认情况下,文件名为 prefix/logs/error.log。
- –http-log-path=path -> 设置主请求的 HTTP 服务器的日志文件的名称。安装完成后,可以随时改变的文件名,在 nginx.conf 配置文件中使用的 access_log 指令。默认情况下,文件名为 prefix/logs/access.log。
- –user=name -> 设置 nginx 工作进程的用户。安装完成后,可以随时更改的名称在 nginx.conf 配置文件中使用的 user 指令。默认的用户名是 nobody。
- –group=name -> 设置 nginx 工作进程的用户组。安装完成后,可以随时更改的名称在 nginx.conf 配置文件中使用的 user 指令。默认的为非特权用户。
- –with-select_module --without-select_module -> 启用或禁用构建一个模块来允许服务器使用 select()方法。该模块将自动建立,如果平台不支持的 kqueue,epoll,rtsig 或 /dev/poll。
- –with-poll_module --without-poll_module -> 启用或禁用构建一个模块来允许服务器使用 poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig 或 /dev/poll。
- –without-http_gzip_module -> 不编译压缩的 HTTP 服务器的响应模块。编译并运行此模块需要 zlib 库。
- –without-http_rewrite_module -> 不编译重写模块。编译并运行此模块需要 PCRE 库支持。
- –without-http_proxy_module -> 不编译 http_proxy 模块。
- –with-http_ssl_module -> 使用 https 协议模块。默认情况下,该模块没有被构建。建立并运行此模块的 OpenSSL 库是必需的。
- –with-pcre=path -> 设置 PCRE 库的源码路径。PCRE 库的源码(版本4.4 - 8.30)需要从 PCRE 网站下载并解压。其余的工作是 Nginx 的 ./ configure 和 make 来完成。正则表达式使用在 location 指令和 ngx_http_rewrite_module 模块中。
- –with-pcre-jit -> 编译 PCRE 包含“just-in-time compilation”(1.1.12 中, pcre_jit 指令)。
- –with-zlib=path -> 设置的 zlib 库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是 Nginx 的 ./ configure 和 make 完成。ngx_http_gzip_module 模块需要使用 zlib 。
- –with-cc-opt=parameters -> 设置额外的参数将被添加到 CFLAGS 变量。例如,当你在 FreeBSD 上使用 PCRE 库时需要使用:–with-cc-opt=“-I /usr/local/include。如需要需要增加 select()支持的文件数量:–with-cc-opt=”-D FD_SETSIZE=2048"。
- –with-ld-opt=parameters -> 设置附加的参数,将用于在链接期间。例如,当在 FreeBSD 下使用该系统的 PCRE 库,应指定:–with-ld-opt=“-L /usr/local/lib”。