** ## Nginx源码安装 **
Nginx简介:Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。目前还有淘宝研发的nginx,如果有兴趣的可以去看下。
1.依赖安装,Nginx安装环境
# yum -y install gcc-c++ zlib zlib-devel openssl openssl--devel pcre pcre-devel
Nginx是C语言开发,建议在linux上运行,本教程使用Centos7.0作为安装环境.
1) gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc 需要执行的命令:
yum install gcc-c++
2) PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。 需要执行的命令:
yum install -y pcre pcre-devel
3) zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。 需要运行的命令:
yum install -y zlib zlib-devel
4) openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。 nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。需要运行的命令:
yum install -y openssl openssl-devel
2.下载源码包
www.nginx.org #官网地址,自己去找包
**
3.源码安装
// 确保系统安装了 pcre 软件包,因为Nginx 的 rewrite 模块要依赖此软件包
# yum -y install pcre pcre-devel openssl-devel libxml2-devel libxslt-devel gcc gcc-c++
// 到 Nginx 的官网 http://nginx.org/download/ , 下载源代码文件。
# wget http://nginx.org/download/nginx-1.9.9.tar.gz
# tar zxvf nginx-1.9.9.tar.gz
# cd nginx-1.9.9
#./configure \
--prefix=/usr/local/nginx \
--without-select_module \
--without-poll_module \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_xslt_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-cc=`which gcc`
# make
// 或者
# make -j 4 //加快编译速度, 有多少个CPU核心,就可以写多少。
// 经过make install 之后, nginx 程序就被安装在了/usr/local/nginx 这个目录中。
# make install
4.安装后配置
**
1.创建用户
useradd -r www -s /bin/false -M
2创建目录
mkdir -p /var/tmp/nginx/client/
3.修改nginx.conf
vi /etc/nginx/nginx.conf
#user nobody >> user www
3.创建启动脚本脚本
vi /etc/rc.d/init.d/nginx
#!/bin/bash
#
# 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: /etc/nginx/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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
chmod +x /etc/rc.d/init.d/nginx
把nginx加入chkconfig,并设置开机启动
chkconfig --add nginx
chkconfig nginx on
启动、停止、重新加载的命令如下
service nginx start
service nginx stop
service nginx reload
Ngin -V 查看有关参数
脚本获得方法:
#启动脚本
wget http://www.centos.bz/wp-content/uploads/2011/07/nginx
改里面的相应文件位置即可
5.安装常见问题
1.源码编译可能会出现
./configure: error: perl module ExtUtils::Embed is required
解决方法:
yum -y install perl-ExtUtils-Embed
2.使用nginx启动脚本可能出现nginx: [emerg] getpwnam(“www”) failed
解决方法:
1)
nginx.conf中去掉user nobody注释即可
2)
错误原因是没有www这个用户,加上就好了
useradd www
3.-bash: /etc/init.d/nginx: /bin/sh^M: bad interpreter: 没有那个文件或目录
解决方法:
查看脚本格式 :set ff
会显示 fileformat=doc
改一下 :set ff=unix
保存执行