您的位置:首页 > 运维架构 > Nginx

nginx在centos系统下安装以及nginx的伪静态安装配置

2013-06-07 15:45 639 查看
网上有详细的,不过不全,而且有些会容易忽略一些东西。以下是纯操作性的演示:

1. 安装gcc(编译用)

yum install gcc gcc-c++ autoconf make

2. 安装pcre
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar zxvf pcre-8.21.tar.gz

进入目录,然后三部曲

./configure --prefix=/usr/local/pcre

make

make install

cp /usr/local/pcre/lib/libpcre.a /usr/local/pcre/libpcre.a

cp /usr/local/pcre/lib/libpcre.la /usr/local/pcre/libpcre.la

cp /usr/local/pcre/include/pcre.h /usr/local/pcre/pcre.h

mkdir /usr/local/pcre/.libs

cp /usr/local/pcre/lib/libpcre.a /usr/local/pcre/.libs/libpcre.a

cp /usr/local/pcre/lib/libpcre.la /usr/local/pcre/.libs/libpcre.la

cp /usr/local/pcre/include/pcre.h /usr/local/pcre/.libs/pcre.h

(说明一下,这里最好是指定一下要安装的目录,除非你知道它装到哪里了)。

3. 安装openssl

yum install openssl openssl-devel

准备工作差不多了,开始安装nginx。

以root用户在CentOS的终端中输入以下命令,开始安装Nginx1.0.4。

1.
要建立 user:nginx 并加入:group=nginx ,为啥?因为后面编译的时候指定了用户和组。

命令:

#useradd -G nginx nginx

-g 和-G 的区别是-g是只分配一个组。

2.

#cd /usr/local/src/
#tar zxvf nginx-1.0.4.tar.gz
#cd nginx-1.0.4/
#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --without-http_rewrite_module --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx/nginx.pid --with-pcre
#make
#make install

这一段是网上的段子,有两点错误:
首先,--without-http_rewrite_module 不可以有,加了这个意味着没有rewrite了。其次要指定with-pcre = pcre的安装目录,不是源码目录

所以上面那一段要改成:

#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx/nginx.pid --with-pcre=/usr/local/pcre

成功后,make的时候会报错。所以需要在nginx-1.0.4目录下面修改一个文件:

#vi objs/Makefile

查找configure --disable-shared,在1089行,删除./configure --disable-shared, 保存

至此,nginx安装工作就完毕了,接下来配置:

添加下面的文件,让nginx 支持service 启动关闭。

vim /etc/init.d/nginxd/ 内容如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=₩([^ ]*₩).*/₩1/g'-`
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

结束符esac是case的倒装,哈哈~

以root用户在CentOS的终端中输入以下命令

#chmod +x /etc/init.d/nginxd
#service nginxd start
#chkconfig --add nginxd
#chkconfig nginxd on
#chkconfig --list nginxd

通过浏览器访问服务器,出现welcome to nginx!表示成功。

最后:Nginx的启动和随机启动

service nginxd restart

把这句话加入到/etc/rc.local最后.

nginx优化以及如果抵扣小流量攻击后面贴出 .....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: