CentOS7编译安装nginx

安装好的操作系统是CentOS-7-x86_64-Minimal-1511,ngixn版本是nginx-1.10.1.tar.gz,见附件。

第一步
CentOS-7安装完后,神马也不用想,连接上服务器后,先用Xshell或者SecureCRT执行如下几个命令,后面在编译能nginx的时候系统会去检测这些,都是坑啊~~~
文件上传与下载:yum -y install lrzsz
程序包下载命令:yum -y install wget
perl语言的运行环境:yum -y install perl
C语言编译环境:yum -y install gcc gcc-c++(大多出需要编译的程序,或c语言的可执行程序都要需要这个环境)

第二步下载软件包
使用rz命令上传nginx-1.10.1.tar.gz包到/usr/local/src/目录并解压(命令:tar zxvf nginx-1.10.1.tar.gz),通常对于这类需要编译安装的程序建议放在此目录底下。下载各类所需软件包,见如下:
cd /usr/local/src/ &&
wget -ct 5 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz &&
wget -ct 5 http://www.openssl.org/source/openssl-1.0.1i.tar.gz &&
wget -ct 5 http://zlib.net/zlib-1.2.10.tar.gz &&
wget -ct 5 http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz &&
wget -ct 5 http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz &&
wget -ct 5 http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz &&
wget -ct 5 https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz &&
wget -ct 5 http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz &&
wget -ct 5 ftp://ftp.simplesystems.org/pub/png/src/libpng16/libpng-1.6.28.tar.gz &&
wget -ct 5 http://www.ijg.org/files/jpegsrc.v9a.tar.gz &&
echo "下载完毕!


解压openssl-1.0.1i、pcre-8.39、zlib-1.2.10三个包,其他包暂时不用解压。
说明:第一行cd /usr/local/src/ 指的是这些包下载后的存放目录;上述文件里有两个ftp的文件下载,建议直接复制地址到浏览器下载后上传到/usr/local/src/目录,然后把上面带有ftp的两行删掉,因为在Xshell里执行这段命令的时候,ftp的文件下载不下来。

第三步编译nginx安装包
cd /usr/local/src &&
tar -zxvf  nginx-1.10.1.tar.gz &&
cd nginx-1.10.1 &&
./configure --prefix=/usr/local/mysoft/nginx \
--without-http_memcached_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-openssl=/usr/local/src/openssl-1.0.1i \
--with-zlib=/usr/local/src/zlib-1.2.10 \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-http_stub_status_module \
--with-http_sub_module \

说明:
使用Xshell连接服务器根目录后,上述命令行中
cd /usr/local/src &&
tar -zxvf  nginx-1.10.1.tar.gz &&
cd nginx-1.10.1 &&
这一段用来指定到你待编译的nginx包目录,当然也可以先执行 cd usr/local/src/nginx-1.10.1直接定位到nginx解压包的目录,那么上述的编译代码变成如下简单形式:

./configure --prefix=/usr/local/mysoft/nginx \
--without-http_memcached_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-openssl=/usr/local/src/openssl-1.0.1i \
--with-zlib=/usr/local/src/zlib-1.2.10 \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-http_stub_status_module \
--with-http_sub_module \

说明:
--prefix=/usr/local/mysoft/nginx  用来指定nginx需要安装到目录,和刚才解压的目录分开,通常情况自定义安装的程序包建议放在/usr/local/ 底下,这里我在该目录底下又建立了mysoft目录,再然后建立了nginx目录,用来和系统程序区分。
--with-openssl=/usr/local/src/openssl-1.0.1i \ 就是刚才第二步里面解压后的目录
--with-zlib=/usr/local/src/zlib-1.2.10 \同上
--with-pcre=/usr/local/src/pcre-8.39 \ 同上
这三个文件必须要,不然后面会出现坑的,你懂的~~~

回车执行玩命令之后,看下Xshell里有没有错,按照上述步骤下来应该不会有错~~。到此nginx编译检测已完成,接着就在刚才目录底下执行如下安装命令,网上也有把编译和安装的命令写在一起执行的,个人习惯分开来执行,清楚明了。
make && make install &&
echo "安装NGINX完毕!"

等待控制台显示“安装NGINX完毕”后,执行cd usr/local/mysoft/nginx 命令,去检查下该目录底下是否有文件,正常有4个目录文件夹,分别是conf、html、logs、sbin,到此nginx安装成功。

第四步设置启动脚本、启动Nginx:
Xshell进入系统根目录,执行如下命令 vi /etc/init.d/nginx,创建脚本,目录需指定到/etc/init.d/底下。脚本命令:
#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/mysoft/nginx/conf/nginx.conf
# pidfile: /usr/local/mysoft/nginx/logs/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/mysoft/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/mysoft/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx.lock
 
make_dirs() {
    # make required directories
    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
    if [ -z "`grep $user /etc/passwd`" ]; then
    useradd -M -s /bin/nologin $user
    fi
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    for opt in $options; do
    if [ `echo $opt | grep '.*-temp-path'` ]; then
    value=`echo $opt | cut -d "=" -f 2`
    if [ ! -d "$value" ]; then
    # echo "creating" $value
    mkdir -p $value && chown -R $user $value
    fi
    fi
    done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    #configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
 
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

说明:
#号开头的行只有三行是有用的,分别是:
#!/bin/sh
# checkconfig : 开头的行
# discription : 开头的行

# config: /usr/local/mysoft/nginx/conf/nginx.conf
# pidfile: /usr/local/mysoft/nginx/logs/nginx.pid
这两个配置指定到你nginx安装成功的目录文件夹位置,nginx.pid文件程序运行后产生

nginx="/usr/local/mysoft/nginx/sbin/nginx"
此配置指定到你nginx安装成功的目录文件夹位置

NGINX_CONF_FILE="/usr/local/mysoft/nginx/conf/nginx.conf"
此配置指定到你nginx安装成功的目录文件所在位置

lockfile=/var/lock/subsys/nginx.lock
此配置指定到系统的/var/lock/subsys/底下,nginx.lock自定义为这个

执行完之后,按ESC退出编辑环境,执行:wq  保存文件并退出vi。



第五步添加到服务
依次执行如下命令:
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
/etc/init.d/nginx start

全部设置完后建议重启一下服务器或者虚拟机。

最后使用命令:
$ service nginx start 
$ service nginx stop 
$ service nginx restart 
$ service nginx reload 
 
$ /etc/init.d/nginx start 
$ /etc/init.d/nginx stop 
$ /etc/init.d/nginx restart 
$ /etc/init.d/nginx reload

检验一下设置是否成功:
查询nginx进程:ps aux |grep nginx
查询服务器ip:ifcfg
linux的防火墙策略:iptables -L(iptables是防火墙,-L 是列出策略)
查询到服务器ip之后,复制ip地址到导航栏访问,若出现“Welcome to nginx!”的欢迎页面,表示nginx服务设置成功。



猜你喜欢

转载自assen.iteye.com/blog/2352416