一. 下载Nginx
通常是在网站上直接找到需要用的包的。
网址:http://nginx.org/
包有几种:
mainline:开发版
stable :稳定版
Legacy: 旧版的稳定版
根据部署需求选择合适的版本,如果是生产环境,就建议选择stable。
CentOS 下载 :
下载 nginx-1.14.2 的stable版
通过终端命令:
sudo yum install nginx
通过命令安装的nginx,都会被安装到标准配置下。
下载完成后,是一个压缩包,这个是源代码,不能直接使用,因此还需要编译、并安装编译后的文件。
上图里的nginx-1.14.2.tar.gz 就是下载下来的包文件。
进入下载的文件夹,解压nginx-1.14.2.tar.gz
# tar zxvf nginx-1.14.2.tar.gz
- zxvf
-z 使用gzip来解压/压缩文件的,包括http信息
-x 解开一个包文件
-v 显示详细信息
-f 必须,表示使用归档文件
解压后的文件夹如上图所示。
此时,可以进行nginx的配置,如进行指定安装目录等操作:
cd nginx-1.14.2
./configure
make
make install
At the end it creates a Makefile.
后台部分输出:
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"
命令:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.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=/opt/app/openet/oetal1/chenhe/pcre-8.37 \
--with-zlib=/opt/app/openet/oetal1/chenhe/zlib-1.2.8 \
--with-openssl=${BUILD_DIR}/openssl-1.0.1t
- --with-pcre=path — 设置PCRE库的源码路径。PCRE库的源码,需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
- --with-zlib=path —设置的zlib库的源码路径。要下载从 zlib并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。
以上命令执行时,需要同一行。
${BUILD_DIR}:源码目录
添加第三方模块
- 下载,解压
- 使用/configure-add_moudle=<path>完成。
二:安装需要使用的lib
CentOS 编译需要用到c++编译器,因此,一会需要安装。
下载C++编译器
yum install gcc-c++
nginx 依赖几个lib,我们在编译前,还要安装它们。
nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库
CentOS 内置yum包管理器,我们使用它来分别下载依赖库:
nginx又依赖pcre-devel,所以一起下载。
yum install -y pcre pcre-devel
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 CentOS上安装 zlib 库。
yum install -y zlib zlib-devel
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
以上依赖全部下载完成后,就可以make nginx了。
make
编译提示完成后,开始安装ngxin程序
make install
nginx会根据config文件,自动进行安装。待安装提示完成后,输入
whereis nginx
系统会返回nginx的安装目录
[root@localhost nginx-1.14.2]# whereis nginx
nginx: /usr/local/nginx
进入到/usr/local/nginx/sbin目录,启动nginx,在浏览器输入localhost,如果有欢迎页面,则nginx就运行起来了。
cd /usr/local/nginx/sbin
./nginx
关闭、立即关闭、重启、重新开始log文件:
./nginx -quit
./nginx -stop
./nginx -reload
./nginx -reopen
修改安装目录的config文件,来配置nginx:
server
{
listen 80;
server_name _;
index index.php;
root /data/httpd/;
}