一、nginx安装环境
nginx是C语言开发,建议在linux上运行。
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
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
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
二、编译安装
tar格式安装(以nginx-1.8.0.tar.gz为例)
1)将nginx-1.8.0.tar.gz拷贝至linux上
2)解压
tar -zxvf nginx-1.8.0.tar.gz
3)进入nginx-1.8.0
cd nginx-1.8.0
4)进行预编译
./configure --prefix=/usr/nginx-1.8
另一种配置(复杂点)
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi 注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录 |
5)编译安装(nginx-1.8.0)
make
make install
6)启动nginx(/usr/local/nginx)
进入nginx下的目录sbin
输入命令 ./nginx (快速停止为./nginx -s stop,完整停止为./nginx -s quit,重启./nginx -s reload)
7)关闭防火墙
service iptables stop
8)测试访问
三、设置开机启动脚本
如下为nginx的开机自启shell脚本(参考)在/etc/init.d下面建立nginx文件,粘贴内容,修改路径
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # 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 $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL |
四、编译安装tengine(以tengine-2.1.0.tar.gz为例)
1.安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel
2.编译三步走./configure \
--prefix=/opt/sxt/soft/tengine-2.1.0/ \ //注意路径,可改
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
make && make install
3.注意:一定要从windows复制粘贴过来(如果直接复制nginx文件到linux下,会出错)
在/etc/init.d下面建立nginx文件,粘贴内容,修改路径(加入启动服务的shell,
可以直接通过service start stop 控制,而不需要通过杀死进程来杀死服务)
#!/bin/bash # # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve # 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/tengine-2.1/sbin/nginx" //修改为opt/sxt/soft/tengine-2.1.0/sbin/nginx prog=$(basename $nginx) NGINX_CONF_FILE="/usr/tengine-2.1/conf/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: " # -HUP是nginx平滑重启参数 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 755 nginx 或者 chmod a+x nginx
5.开启服务
service nginx start 如果出现/var/tmp/nginx/client/目录不存在,则需要手动创建
在/etc/init.d 下创建 mkdir -p /var/tmp/nginx/client/
6.配置chkconfig
chkconfig --add nginx //将nginx交由chkconfig管理
chkconfig --list nginx //可查询
chkconfig nginx on //设为开机自启
7. 如果要设置开机关闭防火墙
chkconfig iptables off
service nginx start|stop|reload