centos下安装nginx

Nginx只能处理80端口和25端口的负载均衡,既Nginx只能做邮件和web服务的负载均衡
1、下载稳定版本的nginx. http://nginx.org/en/download.html

2、按照如下命令安装
复制代码

检查系统路径

[root@localhost usr]# pwd /usr

解压到当前路径

[root@localhost usr]# tar -zxv -f nginx-1.6.2.tar.gz

删除压缩包

[root@localhost usr]# rm -rf nginx-1.6.2.tar.gz

进入到解压包下

[root@localhost usr]# cd nginx-1.6.2/

[root@localhost nginx-1.6.2]#

查看安装时的功能模块信息 [root@localhost nginx-1.6.2]# ./configure –help

指定安装路径

[root@localhost nginx-1.6.2]# ./configure –prefix=/usr/local/nginx

编译

[root@localhost nginx-1.6.2]# make

安装

[root@localhost nginx-1.6.2]# make install

回退到解压缩包上级目录

[root@localhost usr]# cd ../

解除解压缩包

[root@localhost usr]# rm nginx-1.6.2 -rf

上面安装使用默认参数进行配置,如果要自定义安装模块和安装位置可以进行如下参数配置:

[root@localhost nginx-1.6.2]# ./configure \

–prefix=/usr/local/nginx \
–sbin-path=/usr/local/nginx/sbin/nginx \
–conf-path=/usr/local/nginx/conf/nginx.conf \
–http-log-path=/usr/local/nginx/logs/access.log \
–error-log-path=/usr/local/nginx/logs/error.log \
–pid-path=/usr/local/nginx/logs/nginx.pid \
–lock-path=/usr/local/nginx/lock/nginx.lock \
–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 \
–user=root \
–group=root \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_mp4_module \
–with-http_gzip_static_module \
–with-http_stub_status_module

3、安装缺少包提示

错误提示 ./configure: error: the HTTP rewrite module requires the PCRE
library. You can either disable the module by using
–without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with
nginx by using –with-pcre= option.

解决方案

[root@localhost nginx-1.6.2]# yum -y install pcre-devel

错误提示 ./configure: error: the HTTP gzip module requires the zlib
library. You can either disable the module by using
–without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with
nginx by using –with-zlib={path} option.

解决方案 [root@localhost nginx-1.6.2]# yum install -y zlib-devel

上面出现的问题是因为没有安装Nginx相应的编译工具,所以在安装时可以先执行如下命令进行安装

[root@localhost ~]# yum -y install gcc gcc-c++ autoconf automake

[root@localhost ~]# yum -y install zlib zlib-devel openssl
openssl-devel pcre-devel

说明:

zlib:Nginx提供gzip模块,需要zlib库支持

openssl:Nginx提供ssl功能

pcre:支持抵制重写rewrite功能

4、修改防火墙配置:

修改防火墙配置:

[root@localhost nginx]# vi + /etc/sysconfig/iptables

添加配置项

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

重启防火墙

[root@localhost nginx]# service iptables restart

5、启动nginx

方法1

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c
/usr/local/nginx/conf/nginx.conf

方法2

[root@localhost nginx]# cd /usr/local/nginx/sbin [root@admin sbin]#
./nginx

6、监察Nginx配置文件语法

[root@localhost sbin]# ./nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax
is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test
is successful

7、查看nginx是否启动

[root@localhost nginx]# netstat -ntlp
这里写图片描述

或者

测试端口

[root@localhost nginx]# netstat –na|grep 80

浏览器中测试

http://ip:80

附录:

查询nginx主进程号

[root@localhost sbin]# ps -ef | grep nginx

停止进程

[root@localhost sbin]# kill -QUIT 主进程号

快速停止

[root@localhost sbin]# kill -TERM 主进程号

强制停止

[root@localhost sbin]# pkill -9 nginx

Nginx服务脚本
注:下边脚本中的‘//’在nginx配置文件中改为‘#’

[root@localhost init.d]# service nginx Usage: /etc/init.d/nginx
{start|stop|reload|restart|configtest} [root@localhost init.d]# cat
nginx

// !/bin/bash

// chkconfig: - 85 15 // description: nginx is a World Wide Web
server. It is used to serve // Source Function Library .
/etc/init.d/functions

// Nginx Settings NGINX_SBIN=”/usr/local/nginx/sbin/nginx”
NGINX_CONF=”/usr/local/nginx/conf/nginx.conf”
NGINX_PID=”/usr/local/nginx/logs/nginx.pid”

RETVAL=0 prog=”Nginx”

start() {
echo -n "Starting prog: ”
mkdir -p /dev/shm/nginx_temp
daemon NGINXSBINc NGINX_CONF
RETVAL= ?echoreturn RETVAL }

stop() {
echo -n "Stopping prog: ”
killproc -p NGINXPID NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL= ?echoreturn RETVAL }

reload(){
echo -n "Reloading prog: ”
killproc -p NGINXPID NGINX_SBIN -HUP
RETVAL= ?echoreturn RETVAL }

restart(){
stop
start }

configtest(){
NGINXSBINc NGINX_CONF -t
return 0 }

case “ 1instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;)echo "Usage: $0 {start|stop|reload|restart|configtest}”
RETVAL=1 esac

exit $RETVAL

转载请注明出处: 【http://www.cnblogs.com/dennisit/p/4069521.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值