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