Centos7.4下源码安装nginx并附shell安装脚本/配置

Nginx是一款轻量级的网页服务器、反向代理服务器。相较于Apache、lighttpd具有占有内存少,稳定性高等优势。它最常的用途是提供反向代理服务。
一、首先安装必要的库
nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库。选定/usr/local为安装目录,以下具体版本号根据实际改变,这里全yum安装
#yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
 源码下载地址如下:
1.安装PCRE库
$ cd /usr/local/
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
$ tar -zxvf pcre-8.36.tar.gz
$ cd pcre-8.36
$ ./configure
$ make
$ make install
2.安装zlib库
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.8.tar.gz
$ tar -zxvf zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ make install
3.安装ssl
$ cd /usr/local/
$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ ./config
$ make
$ make install
 
二、创建一个文件夹并下载nginx-1.15.4
cd /usr/local
mkdir nginx
Nginx cache purge模块(可选)(暂不选)
# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
#tar -xzf ngx_cache_purge-1.3.tar.gz
三、编译安装nginx
$ cd /usr/local/
$ wget http://nginx.org/download/nginx-1.15.4.tar.gz
$ tar -zxvf nginx-1.15.4.tar.gz
$ cd nginx-1.15.4
$  ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=../ngx_cache_purge-1.3[暂不选]

--prefix=/usr/local/nginx-1.0.6   # 安装路径

--with-http_stub_status_module # 启用nginx状态模块

--with-http_ssl_module # 启用SSL模块

--with-http_realip_module # 启用realip模块(将用户IP转发给后端服务器)

--add-module=../ngx_cache_purge-1.3 # 添加缓存清除扩展模块[暂不选]

前面库包源码安装的话,则在--prefix后面接以下命令:

--with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源码路径。

--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。

 # make

# make install

 
三、shell安装脚本
脚本中可以选定版本号,支持库包是以yum方式在线安装的
#!/bin/bash
#--------------------------------------------------------
# Function: Install nginx for CentOS7
# Date: 2018-1-06
# Author: Anwar Wong
#--------------------------------------------------------
#Print debug information
 
NGINX_VER="$?"
NGINX_SOFT="nginx-${NGINX_VER}.tar.gz"
NGINX_DIR="/usr/local/nginx"
NGINX_SRC=`echo $NGINX_SOFT| sed 's/.tar.*//g'`
NGINX_YUM="yum install -y"
NGINX_ARG="--user=www --group=www --with-http_stub_status_module --with-http_ssl_module"
 
 
if [$? -eq 0]; then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.2.3|1.12.2}\033[0m"
exit 0
fi
#Installing dependencies
$NGINX_YUM wget make tar gcc gcc-c++ glibc zlib zlib-devel
$NGINX_YUM perl perl-devel pcre pcre-devel openssl openssl-devel
 
#Downloading
wget -c $NGINX_URL/$NGINX_SOFT
tar -xzf $NGINX_SOFT
cd $NGINX_SRC
 
#Creating user and group
useradd -s /sbin/nologin www
 
#Starting install nginx
./configure --prefix=$NGINX_DIR/$NGINX_ARG
 
#Compile nginx
make -j4
make -j4 install
 
#Starting Nginx
$NGINX_DIR/sbin/nginx
 
#Show nginx status
ps -ef |grep nginx
netstat -tnlp |grep nginx
四、启动
$ /usr/local/nginx/sbin/nginx
检查是否启动成功:
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
部分命令如下:
重启:
$ /usr/local/nginx/sbin/nginx –s reload
停止:
$ /usr/local/nginx/sbin/nginx –s stop
测试配置文件是否正常:
$ /usr/local/nginx/sbin/nginx –t
强制关闭:
$ pkill nginx
 错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www,如下命令:
#/usr/sbin/groupadd -f www
#/usr/sbin/useradd -g www www
 
 
 
-----------未完待续-------------
posted on 2018-10-05 17:18  Anwar¢  阅读(433)  评论(0编辑  收藏  举报