日期:2014-05-16  浏览次数:20673 次

nginx在linux下安装
下载解压略过

1 安装nginx需安装c编译工具 有了就不想要安装了
我使用fedora系统,直接yum install gcc进行安装
为了安装更加全面以下的也全装上,我这个搞Java的暂时不知道他们详细的作用 总之c c++之类的可能编译需要的东西不太一样吧
yum install gcc-gfortran
yum install gcc-c++

2 安装pcre (如果提示需要pcre包到情况下)
yum install pcre
yum install pcre-devel
有可能还需要zlib,因为gzip压缩需要这个 需要安装 yum install zlib

pcre和zlib都可以自己下载最新版本,我就模仿网上高人全都解压在/opt/software中
这里只是解压缩,而不是安装,nginx安装的时候会对他们解压目录中的内容进行编译

不建议使用root在正式环境运行nginx,防止被黑。所以还是建立单独的用户的和组。
以下参考其他网上高人的方案:
(1)创建用户和组
groupadd -f www 
useradd -s /sbin/nologin -g www nginx 
(2)创建单独的日志目录
mkdir /var/log/nginx 
chown nginx.www /var/log/nginx  #赋予权限。命令原来可以这么用,我第一次知道

3 设置安装
去除多余的模块 指定运行的用户和组 指定运行nginx的pid
设置日志的记录位置 指定pcre和zlib的路径 如果使用系统默认的不写就行 可以选择自己的解压缩版本 自己可以找最新版 效率或许更好

./configure --prefix=/opt/servers/nginx \
--user=nginx \
--group=www \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre=/opt/software/pcre-8.10 \
--with-zlib=/opt/software/zlib-1.2.5 \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--without-http_fastcgi_module \
--without-http_memcached_module \
--without-http_map_module \
--without-http_geo_module \
--without-http_autoindex_module \
--with-poll_module

Last Step 安装 make
make install

4 将nginx设置为系统服务
抄袭前人的代码
vim /etc/init.d/nginx   创建服务文件
#!/bin/bash  
# v.0.0.1  
# create by jackbillow at 2007.10.15  
# nginx - This shell script takes care of starting and stopping nginx.  
#  
# chkconfig: - 60 50  
# description: nginx [engine x] is light http web/proxy server  
# that answers incoming ftp service requests.  
# processname: nginx  
# config: /usr/local/nginx/conf/nginx.conf  
nginx_path="/opt/servers/nginx"  
nginx_pid="/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  
[ -x $nginx_path/sbin/nginx ] || exit 0  
RETVAL=0  
prog="nginx"  
  
# Start daemons.  
start() {  
if [ -e $nginx_pid -a ! -z $nginx_pid ];then  
echo "nginx already running...."  
exit 1  
fi  
if [ -e $nginx_path/conf/nginx.conf ];then  
echo -n $"Starting $prog: "  
$nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf &  
RETVAL=$?  
[ $RETVAL -eq 0 ] && {  
touch /var/lock/subsys/$prog  
success $"$prog"  
}  
echo  
else  
RETVAL=1  
fi  
return $RETVAL  
}  
  
# Stop daemons.  
stop() {  
echo -n $"Stopping $prog: "  
killproc -d 10 $nigx_path/sbin/nginx  
RETVAL=$?  
echo  
[ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog  
}  
  
# See how we were called.  
case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;  
reconfigure)  
stop  
start  
;;  
status)  
status $prog  
RETVAL=$?  
;;  
*)  
echo $"Usage: $0 {start|stop|reconfigure|status}"  
exit 1  
esac  
exit $RETVAL  

注意里面一些的路径,如果调整了,请注意调整。然后给服务赋予执行权限
chmod +x /etc/init.d/nginx 

这样就可以当成服务来启动了,感谢上帝,感谢原作者和转载作者。

另外要注意,如果nginx读取的文件跨了自己掌握的目录,那么nginx需要拥有那个目录所在全部的层级读和执行的权限才行。