Centos安装nginx

本文提供了一份详细的CentOS环境下Nginx服务器安装指南,包括安装wget、配置编译环境、安装依赖库(如PCRE和zlib)、下载并安装Nginx源码等步骤,并介绍了如何设置防火墙及编写Nginx服务脚本来实现服务管理和自启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Centos安装nginx服务器:

1、我们使用wget命令出现如下错误:-bash: wget: command not found不用紧张整个错误表示我们的linux系统并没有安装wget命令,我们使用yum安装一下这个命令。输入yum -y install wget安装成功之后我们继续下一步。

2、安装编译环境:gcc gcc-c++ automake autoconf libtool make

	执行命令 yum -y install gcc gcc-c++ automake autoconf libtool make

3、安装相关依赖包(这里不采用yum安装先)

PCRE库(用于支持http rewrite)

cd /home
mkdir /soft
cd soft
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar zxvf pcre-8.40.tar.gz 
cd pcre-8.40
./configure
make
make install

zlib库(用于支持http gzip)

cd ..
wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

4、下载nginx源码:

wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --with-pcre=/usr/local/src/pcre-8.37
make
make install


使用wget http://localhost 我们能看到会下载一个index.html页面。
如果是局域网我们能使用ip访问到但是我们需要关系防火墙或者开放80端口。
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 的下面添加一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (如果不加在20的下面好像不起作用)
执行 /etc/init.d/iptables restart
好了我们在此在局域网输入ip地址ok了!!!出现如下页面

再加点干货,现在安装的nginx并不能使用service命令,我们在/etc/init.d/nginx编写一个sh脚本
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/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/nginx/sbin/nginx"
prog=$(basename $nginx)


NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"


lockfile=/var/lock/subsys/nginx


start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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
然后设置下nginx shell脚本的执行权限chmod +x /etc/init.d/nginx
服务自启动:
chkconfig nginx on
借鉴文章:
本文相关链接:http://www.cnblogs.com/ppoo24/p/4918288.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值