CentOS编译安装nginx-1.8.0

首先去http://nginx.org/en/download.html下载nginx-1.8.0,这里我下载到opt目录

后面我要设置nginx使用nginx用户和nginx用户组,所以执行下面命令

groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx

接下来安装编译环境

yum -y install pcre-devel
yum -y install gcc gcc-c++ ncurses-devel perl
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel

然后解压文件准备编译

cd /opt
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0

开始编译和安装

./configure --user=nginx --group=nginx --prefix=/opt/nginx --with-http_stub_status_module  --with-http_ssl_module  --with-http_flv_module --with-http_gzip_static_module
make
make install

编译完成之后,测试安装效果,出现版本号为安装成功

cd /opt/nginx/sbin
./nginx -v

QQ截图20150720002918

将以下脚本保存为nginx文件就在/etc/init.d目录下,注意如果你用外部编辑器请确认换行符为LINUX的,编码正确,其中/opt/nginx/请替换成你的安装目录

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by jackbillow at 2007.10.15.
# it is v.0.0.2 version.
# if you find any errors on this scripts,please contact jackbillow.
# and send mail to jackbillow at gmail dot com.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
 
nginxd=/opt/nginx/sbin/nginx
nginx_config=/opt/nginx/conf/nginx.conf
nginx_pid=/opt/nginx/logs/nginx.pid
 
RETVAL=0
prog="nginx"
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
 
[ -x $nginxd ] || exit 0
 
 
# Start nginx daemons functions.
start() {
 
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
 
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
 
}
 
 
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
 
 
# reload nginx service functions.
reload() {
 
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
 
}
 
# See how we were called.
case "$1" in
start)
        start
        ;;
 
stop)
        stop
        ;;
 
reload)
        reload
        ;;
 
restart)
        stop
        start
        ;;
 
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
 
exit $RETVAL

然后执行下列命令,把nginx加入系统服务并设置成开机启动。如果你的配置跟我不一样请注意目录,用户名和用户组的替换

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
chown -R nginx:nginx /opt/nginx

最后启动nginx就可以了,别忘了开放80端口

service nginx start
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
chkconfig iptables on

《CentOS编译安装nginx-1.8.0》上有2条评论

  1. 在最后一步设置了半天iptables,最后发现centos 7已经不用iptables,改用firewall-cmd了
    这三句
    iptables -I INPUT -p tcp –dport 80 -j ACCEPT
    service iptables save
    chkconfig iptables on
    应该改为
    firewall-cmd –zone=public –add-port=http/tcp –permanent
    firewall-cmd –reload

    最近看防火墙就胀。

发表回复

您的电子邮箱地址不会被公开。