您的位置:首页 > 运维架构 > Nginx

centos中安装nginx

2017-02-03 00:00 295 查看
用yum安装软件发现报错
解决This system is not registered with RHN
1.使用命令 cd /etc/yum.repos.d/ 进入yum的配置目录。
2.在终端中输入 wget http://docs.linuxtone.org/soft/lemp/CentOS-Base.repo 命令,下载CentOS- Base.repo文件。
3.然后将原有的rhel-debuginfo.repo备份一下,使用命令mv CentOS-Base.repo rhel-debuginfo.repo,将CentOS- Base.repo重命名成rhel-debuginfo.repo。
4.成功以后,使用yum install build-essential安装成功。

安装nginx

1、准备工作
选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。
Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;
用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。
否则,就需要用命令“# yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。
安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。
同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思):

# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel

2、下载并安装
创建目录(nginx-src)并进去;然后,从官方地址(http://nginx.org/)下载,解压,配置,编译,安装:

# mkdir nginx-src && cd nginx-src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz # tar xzf nginx-1.10.3.tar.gz
# cd nginx-1.10.3
# ./configure
# make
# make install
# whereis nginx
nginx: /usr/local/nginx

默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.10.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h         : this help
-v            : show version and exit
-V            : show version and configure options then exit
-t            : test configuration and exit
-T            : test configuration, dump it and exit
-q            : suppress non-error messages during configuration testing
-s signal     : send signal to a master process: stop, quit, reopen, reload
-p prefix     : set prefix path (default: /usr/local/nginx/)
-c filename   : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

启动:./nginx
停止:./nginx -s stop

3、添加到系统服务
使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

#!/bin/sh
# chkconfig: 2345 85 15
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server.
# It is used to serve HTML files and CGI.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx deamon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

test -x $DAEMON || exit 0

d_start(){
$DAEMON || echo -n "already running"
}

d_stop(){
$DAEMON -s quit || echo -n "not running"
}

d_reload(){
$DAEMON -s reload || echo -n "can not reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC conf..."
d_reload
echo "reload ."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac

exit 0

保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。
可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on/off
# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

nginx配置

upstream boss {
server 127.0.0.1:8080;
}

server {
listen       80;
server_name  boss.example.com;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location /admin {
proxy_pass http://boss/admin; #下面两条如果不加的话,会导致静态文件解析出异常
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}

upstream backend {

#ip_hash;

server 192.168.1.251;

server 192.168.1.252;

server 192.168.1.247;

}

server {

listen       80;

server_name  2;

location / {

#设置主机头和客户端真实地址,以便服务器获取客户端真实IP

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#禁用缓存

proxy_buffering off;

#反向代理的地址

proxy_pass http://backend; 
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: