安装nginx

Nginx版本分为主线版、稳定版和历史版本

在官方网站中

  • Mainline version表示目前主力在做的版本,可以说是开发版,开发版更新速度较快,从官网上看大约一个月更新1-2次
  • Stable version表示最新稳定版,也就是生产环境上建议使用的版本
  • Legacy versions表示遗留的历史稳定版

1、安装Nginx

Nginx可以通过源码方式、yum方式进行安装,根据线上环境部署经验,推荐采用源码方式进行安装。

目前Nginx最新稳定版本为Nginx1.14.0,下面的安装就使用这个版本进行介绍。

这里约定本章软件安装环境,如无特殊说明均使用Centos7.4操作系统。

在安装操作系统的安装软件配置部分,建议选择“Server with GUI”,并选择“Development Tools”和“Compatibility Libraries”两项附加软件。

确保gcc、libgcc、gcc-c++等编译器已经正确安装。

2、Nginx的依赖程序

在安装Nginx之前,需要安装一些Nginx的依赖程序,Nginx的主要依赖程序有zlib、pcre、openssl三个

其中,zlib用于支持gzip模块,pcre用于支持rewrite模块,openssl用于支持ssl功能

为了简单快捷,推荐通过yum安装zlib、pcre、openssl软件包

安装方式如下:

yum -y install zlib gcc gcc-c++ libgcc zlib-devel pcre pcre-devel openssl openssl-devel libxslt-devel gd gd-devel GeoIP GeoIP-devel libxml2 libxml2-dev

3、源码编译安装Nginx

创建nginx用户

创建一个nginx的系统运行用户,操作如下:

$ useradd -r -s /sbin/nologin nginx
$ id nginx
uid=988(nginx) gid=982(nginx) 组=982(nginx)

Nginx编译参数

Nginx有很多编译参数,这里仅列出常用的一些参数,configure过程如下:

$ cd /data/
$ tar -zxvf nginx-1.22.0.tar.gz
$ cd nginx-1.22.0/
$ mkdir -p /usr/local/nginx/conf/conf.d
$ ./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-pcre \
--with-http_v2_module
$ make&&make install

其中,每个编译参数的含义如下所示:

  • --user 指定启动程序所属用户
  • --group 指定启动程序所属组
  • --prefix 指定Nginx程序的安装路径
  • --sbin-path 设置Nginx二进制文件的路径名
  • --conf-path 指定Nginx配置文件路径
  • --error-log-path 指定Nginx错误日志文件路径
  • --http-log-path 指定Nginx访问日志文件路径
  • --pid-path 设置Nginx的pid文件nginx.pid的路径
  • --lock-path 设置Nginx的lock文件nginx.lock文件路径
  • --with-openssl 指定OpenSSL源码包的路径,如果编译的时候没有指定“--with-openssl”选项,那么默认会使用系统自带的openssl库
  • --with-pcre 设置Nginx启用正则表达式
  • --with-http_stub_status_module 安装用来监控Nginx状态的模块
  • --with-http_ssl_module 表示启用Nginx的SSL模块,此模块依赖“--with-openssl”这个选项,通常一起使用。
  • --with-http_gzip_static_module 表示启用Nginx的gzip压缩

4、查看安装目录

$ cd /usr/local/nginx/
$ tree 
.
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
|   ├── conf.d
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── html
│   ├── 50x.html
│   └── index.html
├── logs
└── sbin
    └── nginx

4 directories, 18 files
  • conf 是存放配置文件的目录
  • html 是nginx的工作目录
  • logs 是存放日志的目录,因为还没有运行,所有事空文件夹
  • sbin 是存放命令的目录
posted @ 2023-12-18 14:25  厚礼蝎  阅读(45)  评论(0编辑  收藏  举报