nginx安装部署

系统环境:
os:centos6.5x64
hostname:test1.lxm.com
ip:10.0.10.11
nginx:nginx-1.6.1.tar.gz
openssl:openssl-1.0.1i.tar.gz
software path:/root/soft/
software install path: /usr/local


1.安装系统所需的编译软件(如果安装过了,则跳过,本人新装系统,所以需要安装)
#yum -y install gcc* compact-gcc* automake make autoconf

2.安装nginx依赖软件
#yum -y install pcre pcre-devel

 

 

注:pcre软件包提供了nginx正则表达式的支持,如果不想使用系统自带的pcre软件包或者想要使用更高版本的pcre软件包,可以自行编译安装,本人采用人rpm包安装
编译安装pcre:
#tar -zxvf pcre-8.35.tar.gz
#cd pcre-8.35
#mkdir /usr/local/pcre (创建pcre安装的目录,本人习惯将编译安装的软件放置于/usr/local/目录下.不过这里要注意的是,如果自行下载pcre包,nginx使用的是源码解压的目录,而不是安装后的目录,这跟apache不一样)
#./configure --prefix=/usr/local/pcre
#make && make install

3.安装nginx
#cd /root/soft
#tar -zxvf openssl-1.0.1i.tar.gz
#tar -zxvf nginx-1.6.1.tar.gz
#mkdir /usr/local/nginx
#mkdir -pv /usr/local/nginx/{client_body_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
#./configure --prefix=/usr/local/nginx/ --with-pcre --with-http_ssl_module  --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client_body_temp/ --http-proxy-temp-path=/usr/local/nginx/proxy_temp/ --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp/ --http-scgi-temp-path=/usr/local/nginx/scgi_temp/
#make && make install
#make clean

注:此处的nginx编译时加入了ssl的功能,但是不需要事先编译安装openssl,因为这里只需要调用openssl的源码包.此外temp几个目录文件必须手工创建,否则安装完后默认是没有的,这也是高版本和低版本的区别

4.启动并测试nginx
创建nginx的启动用户和组:
#groupadd -g 48 nginx
#useradd -g 48 -u 48 -s /sbin/nologin nginx -M
#cd /usr/local/nginx/conf/
#vim nginx.conf
将#user nobody; 修改为user nginx;

#/usr/local/nginx/sbin/nginx -t : 测试配置文件是否有语法错误
#/usr/local/nginx/sbin/nginx :启动nginx服务


#ps aux | grep nginx :查看是否有nginx进程
#netstat -nultp | grep 80 :nginx默认监听80端口,查看是否正常

4、创建nginx服务启动脚本

[root@localhost ~]# vim /etc/init.d/nginx

添加:

#!/bin/bash

#### welcome to nginx  ####

# chkconfig: - 99 20

# description: this is nginx server

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

        start)

           $PROG

           ;;

        stop)

           kill -s QUIT $(cat $PIDF)

           ;;

        restart)

           $0 stop

           $0 start

           ;;

        reload)

           kill -s HUP $(cat $PIDF)

           ;;

        *)

          echo "Usage: $0 {start|stop|restart|status}"

          exit 1

          ;;

        esac

        exit 0

[root@localhost ~]# chmod +x  /etc/init.d/nginx

[root@localhost ~]# service nginx stop

[root@localhost ~]# service nginx start

[root@localhost ~]# netstat -anpt | grep 80

[root@localhost ~]# chkconfig --add nginx

[root@localhost ~]# chkconfig nginx on

5、验证:

[root@localhost ~]# firefox http://192.168.1.1

如果检查一切正常,则使用浏览器进程访问测试:
http://10.0.10.11
正常情况下会返回nginx的欢迎界面:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 

注:这里的内容为复制,第一次写发现插入不了图片,有待研究,哈哈哈。。。。。

 


如果发现不能访问,测试可能有两个问题:
1)查看selinux是否关闭
#sestatus
如果selinux开启,则临时允许:
#setenforce 0
如果想永久关闭,则修改配置文件,之后在重启系统即可;
#sed -i '/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
如果不会使用sed命令,则使用vim打开/etc/sysconfig/selinux文件,将其中的SELINUX=enforcing改为SELINUX=disabled即可,之后重启系统生效。

2)查看iptables是否开启
系统默认是开启iptables来保护系统的安全,默认情况下只允许ssh的远程连接和已经建立连接以及相关联的进程进行交互访问,因此默认开启情况下是阻止80端口访问的。此时有两种常用的解决办法:
(1):清空规则,因为iptables链的默认规则是ACCEPT
#iptables -F
(2):添加一条允许访问80端口的规则
#iptables -t filter -I INPUT 5 -m state --state NEW -m tcp -p tcp -s 0/0 --dport 80 -j ACCEPT
注意:我这里使用的是-I(插入)选项,该选项默认是在匹配该number规则的前面插入一条规则。

 

 

附:
1.nginx 编译选项解释
nginx的编译选项虽然不是很多,但是也不少,因此这里不做一一解释,也没有必要,只对一些常见的选项进行解释
查看编译的选项:
#cd /root/soft/nginx-1.6.1 //即nginx的解压目录下
#./configure --help | less

注:在编译软件的时候,新手通常会问,到底这个软件默认开启了那些功能,哪些功能又是没开启的。这里提供大家一个简单的判断方法,当我们查看编译选项的时候,--with开头的通常是默认没有开启的功能,如果要开启该功能,则需要编译的时候手动添加该选项开启该功能,如果是已--without开头的,则默认是开启该功能的,不做任何设置,编译安装好的软件就可以使用该功能

--with-rtsig_module              
--with-select_module            
--without-select_module          
--with-poll_module               
--without-poll_module           
以上几个模块都是web站点底层并发连接的信号处理机制,对nginx来说,默认采用的epoll机制,能够轻松的应付c10K的问题
--with-file-aio               
开启对内核文件异步io传输的支持,能够提供nginx的性能,但是在使用前,建议大家对于气底层的原理要熟悉
--with-ipv6                      
开启对ipv6的支持
--with-http_ssl_module            
开启对ssl的支持,支持证书的功能
--with-http_spdy_module          
开启对spdy的支持,spdy是google开发的一种tcp应用层协议,可以降低网络延迟,提升网络性能
--with-http_realip_module        
开启该模块,可以记录真实的源ip地址
--with-http_p_w_picpath_filter_module   
开启该模块可用用来对图片内容进行过来,但是该模块需要gd库的支持
--with-http_dav_module          
开启该模块可以用来针对性的设置文件或者目录的权限
--with-http_flv_module
该选项用来开启nginx对flv视频流的支持
--with-http_mp4_module          
开启该模块支持mp4视频流
--with-http_gunzip_module         
开启该模块支持对gzip压缩文件的解压功能
--with-http_gzip_static_module     enable ngx_http_gzip_static_module
开启该模块可以对文件进行检查是否已经被gzip压缩,因为使用nginx时,为了降低带宽的使用,加快网络传输速度,通常会采用gzip压缩一定格式的文件。该模块就可以防止已经被压缩的文件再次压缩
--with-http_auth_request_module   
该模块用来设置nginx的基于子请求的认证功能
--with-http_random_index_module  
该选项可以让nginx支持随机选项一个索引页面返回给客户端
--with-http_secure_link_module    
   这个模块用于为所需的安全性“令牌”计算和检查请求URL
--with-http_degradation_module
开启该选项允许nginx在内层不足的情况下返回204或者444状态码  
--with-http_stub_status_module   
使用该选项,可以是nginx支持使用web界面查看nginx的实时连接状态,也就是一些状态信息
--without-http_gzip_module       
该选项是用来设置gizp压缩功能的设置       
--without-http_userid_module   
该选项是用来获取客户端cookie功能的设置  
--without-http_access_module    
该选项是用来设置nginx对访问权限的设置 
--without-http_auth_basic_module 
该选项用来设置nginx是否支持给予用户和密码认证的访问权限控制
--without-http_autoindex_module
该选项用来设置nginx对自动所以的支持,也就是web节目访问时,可以返回一个文件列表           
--without-http_referer_module     
该选项用来设置nginx的防盗链功能
--without-http_rewrite_module 
该选项用来设置nginx支持重定向转发规则   
--without-http_proxy_module       
该选项用来设置nginx支持proxy功能
--without-http_fastcgi_module  
该选项用来设置nginx支持fastcgi,通常用来和php进行整合 
--without-http_uwsgi_module      
该选项用来设置nginx是否支持uwsgi的程序
--without-http_scgi_module
该选项用来设置nginx是否支持scgi的程序
--without-http_memcached_module  
该选项用来设置nginx是否支持memcached
--without-http_limit_conn_module  
该选项用来设置nginx是否支持并发连接数的限制,通常用来防止攻击
--without-http_limit_req_module   
该选项用来设置nginx是否支持请求数的限制
--without-http_browser_module     
该选项用来设置nginx是否支持识别客户段的浏览器功能,可以针对不同的客户端浏览器来返回不同的内容
--without-http_upstream_ip_hash_module 
--without-http_upstream_least_conn_module
--without-http_upstream_keepalive_module
以上几个选项是针对nginx作为反向代理时的设置

--with-http_perl_module           
--with-perl_modules_path=PATH     
--with-perl=PATH      
以上几个选项用来开启对perl正则表达式的支持,通常我们会使用pcre来开启对perl正则表达式的支持,在编译的时候使用--with-pcre            
--http-log-path=PATH    
指定nginx的日志存放路径
        
--http-client-body-temp-path=PATH
--http-proxy-temp-path=PATH      
--http-fastcgi-temp-path=PATH     
--http-uwsgi-temp-path=PATH      
--http-scgi-temp-path=PATH  
以上几个选项都是针对nginx运行中的一些临时文件的存放目录的设置,在nginx的新版本中,必须手动指定,否则编译安装后,不存在此类目录
--without-http              
nginx默认也是一个web软件,支持大件web站点,如果要关闭此功能,则使用该选项     
--without-http-cache              
nginx默认支持http的缓存功能,如果要关闭缓存功能,则使用该选项

--with-mail                      
--with-mail_ssl_module            
--without-mail_pop3_module       
--without-mail_imap_module        
--without-mail_smtp_module        
以上几个选项都是跟mail有关,nginx也支持main反向代理
--with-google_perftools_module    
该选项用来开启支持google perftools工具,该工具可以完成对nginx实时性能的检测和调优
--add-module=PATH  
该选项特别重要,nginx默认自带的模块,并不能完成需要的所有功能,此时如果要使用额外的模块来实现相应的功能,就需要使用该选项来添加额外的模块到nginx中

2.nginx LSB启动脚步
#vim /etc/rc.d/init.d/nginx
复制下面的内容到该文件中:
#!/bin/sh
#
# 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: /usr/local/nginx/conf/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/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 a+x /etc/rc.d/init.d/nginx
#dos2unix /etc/rc.d/init.d/nginx :将该脚步的格式转换为linux下的格式,主要区别在于行结束符,往往一些复制的脚步看起来内容没有任何错误,但在linux下执行就是报错,通常都是格式没转换的原因
#service nginx restart :重启一下nginx,如果重启成功,则表示该脚步可以正常使用
#chkconfig --add nginx
#chkconfig --level 3 nginx on
以上两行可以设置nginx开机时在3级别上自动启动


注:使用该脚步时,要确保脚步中可执行文件和配置文件的路径是正确的,否则会报错.其次,所谓LSB脚步是linux/unix的一种脚步编程规范,反正在/etc/rc.d/init.d/目录下的启动脚步,都必须支持LSB的格式

到此为止,nginx的安装部署就说到这里。更多nginx技术文档请看下回分解。。。。。。。

结束!!!

原文地址:https://www.cnblogs.com/guarderming/p/11022235.html