2、Nginx服务器的安装与配置
1、 Nginx的下载
可以从以下网络连接进行下载nginx的不同版本:
http://nginx.org/download/
2、 Nginx的安装
Nginx分为windows和linux下的安装,本文以linux版本Centos为例,其余linux发行版本都大致相同,进行Nginx的源码编译安装,假如有想理解windows安装的,请自行网上查询,也可以私聊我。
2.1、用上传工具将Nginx上传到Centos内。
2.2、安装Nginx的依赖软件
yum install gcc openssl-devel pcre-devel zlib-devel
2.3、创立nginx使用户
#groupadd –r nginx
#useradd -r -g nginx -s /bin/false -M nginx
2.4、编译安装

#./configure \
–prefix=/usr/local \ 指定安装路径
–sbin-path=/usr/sbin/nginx \ 指定Nginx可执行文件安装路径
–conf-path=/etc/nginx/nginx.conf \指定nginx.conf文件的路径
–error-log-path=/var/log/nginx/error.log \指定error.log文件的路径
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx/nginx.pid \指定pd指令的路径
–lock-path=/var/lock/nginx.lock \
–user=nginx \指定Nginx的使用户为nginx
–group=nginx \指定Nginx的所属组为nginx
–with-http_ssl_module \打开用HTTP SSL板块
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–http-client-body-temp-path=/var/tmp/nginx/client/ \ 指定http用户端请求缓存文件存放目录的路径。
–http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 指定http反向代理商缓存文件存放目录的路径
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \指定http fastCGI缓存文件存放目录的路径。
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
–http-scgi-temp-path=/var/tmp/nginx/scgi \
–with-pcre打开pcre库的用

表示编译成功
# make && make install
注:
关于配置选项的简单说明:
–prefix= – The path relative to which all other Nginx paths will resolve. If not specified, defaults to /usr/local/nginx.
–sbin-path= – The path to the nginx executable. Only used for installation. If not specified defaults to /sbin/nginx.
–conf-path= – The default location of nginx.conf if no -c parameter is provided. If not provided, defaults to /conf/nginx.conf.
–pid-path= – The path to nginx.pid, if not set via the “pid” directive in nginx.conf. If not provided, defaults to /logs/nginx.pid.
–error-log-path= – The location of the error log if not set via the “error_log” in nginx.conf. If not set, defaults to /logs/error.log.
–http-log-path= – The location of the access log if not set via the “access_log” directive in nginx.conf. If not set, defaults to /logs/access.log.
–user= – The default user that nginx will run as if not set in nginx.conf via the “user” directive. If not set, defaults to “nobody”.
–group= – The default group that nginx will run under if not set via the “user” directive in nginx.conf. If not set defaults to “nobody”.
–with-http_ssl_module – Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
–with-http_flv_module – Enable ngx_http_flv_module
–http-client-body-temp-path=PATH – Set path to the http client request body temporary files. If not set, defaults to /client_body_temp
–http-proxy-temp-path=PATH – Set path to the http proxy temporary files. If not set, defaults to /proxy_temp
–http-fastcgi-temp-path=PATH – Set path to the http fastcgi temporary files. If not set, defaults to /fastcgi_temp
–lock-path= – The path to the nginx.lock file. If not provided, defaults to /logs/nginx.lock.
3、 将以下存放在/etc/rc.d/init.d/目录下,并命名为nginx(或者者其余的,你自己记得住就可),即可以用servie服务进行Nginx开启、关闭、重启。
#!/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
4、 测试。
/usr/local/nginx/sbin启动nginx
Service nginx start ,而后执行netstat –tunlp ,查看能否有80端口开放,此时Nginx就编译安装成功。


注:需要关闭防火墙和selinux

5、 假如写的好,请继续关注我哦,下一章讲《Nginx的基本配置与优化》。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 2、Nginx服务器的安装与配置