CentOS7 离线安装 Nginx 1

4、Nginx安装

tar zxvf nginx-1.6.2.tar.gz -C /usr/local/

cd /usr/local/nginx-1.6.2/ && ./configure --prefix=/usr/local/nginx

make && make install

cd /usr/local/nginx && ls

5、启动Nginx

/usr/local/nginx/sbin/nginx

ps -ef | grep nginx

6、防火墙端口通行

firewall-cmd --add-port=80/tcp --permanent

firewall-cmd --reload

firewall-cmd --list-ports

7、web访问Nginx

如果访问出现 403 Forbiden ,需要修改配置文件

vim /usr/local/nginx/conf/nginx.conf

在默认配置 #user  nobody; 下添加

user root;

赋予 /usr/local/nginx/html/index.html 文件 644权限

chmod 644 /usr/local/nginx/html/index.html

8、Nginx服务配置

vim /etc/init.d/nginx

添加一下内容

#!/bin/sh

nginx - this script starts and stops the nginx daemon

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

config: /etc/sysconfig/nginx

pidfile: /var/run/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”

nginx=“/usr/local/nginx/sbin/nginx”

prog=$(basename $nginx)

NGINX_CONF_FILE=“/etc/nginx/nginx.conf”

NGINX_CONF_FILE=“/usr/local/nginx/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -

if [ -n “$user” ]; then

if [ -z “grep $user /etc/passwd” ]; then

useradd -M -s /bin/nologin $user

fi

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

fi

}

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: "

killproc $prog -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

给 /etc/init.d/nginx 文件赋予执行权限

chmod +x /etc/init.d/nginx

创建pid文件

echo 10000 > /var/run/nginx.pid

将启动脚本配置到chkconfig服务

chkconfig --add /etc/init.d/nginx

chkconfig --list

开机自启

chkconfig nginx on

或者在/etc/rc.d/rc.local文件下添加

/usr/local/nginx/sbin/nginx

启动和关闭命令

/usr/local/nginx/sbin/nginx

/usr/local/nginx/sbin/nginx -s stop

/usr/local/nginx/sbin/nginx -s reload

service nginx status

service nginx start

service nginx stop

service nginx restart

systemctl status nginx

systemctl start nginx

systemctl stop nginx

systemctl restart nginx

9、常见报错

./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= option.

解决:以下依赖,如果需要其他依赖则使用 --nodeps 不依靠依赖安装

rpm -Uvh pcre-devel-8.32-17.el7.x86_64.rpm

rpm -Uvh pcre2-10.23-2.el7.x86_64.rpm

rpm -Uvh zlib-devel-1.2.7-18.el7.x86_64.rpm

rpm -Uvh openssl-devel-1.0.2k-8.el7.x86_64.rpm

10、nginx代理

正向代理(爬虫代理):客户端(浏览器)通过代理服务器(配置代理服务器)进行互联网访问

反向代理(服务代理):客户端将请求发送给反向代理服务器,反向代理再获取指定目标服务器数据再返回给客户端(客户端只认识代理服务器ip)

正向代理隐藏真实客户端,反向代理隐藏真实服务器

正向代理 代理用户,反向代理 代理服务器

反向代理示例一:

如下修改nginx配置文件 nginx/conf/nginx.conf

server模块修改代理服务器ip:

server_name  192.168.0.130;

server模块下的location模块添加代理ip:

proxy_pass  http://192.168.0.180:8080;

nginx服务器ip:192.168.0.130  监听端口:80

tomcat服务器:192.168.0.180   监听端口:8080

http://192.168.0.130/     http://192.168.0.180:8080/

此时,访问tomcat服务器可正常访问tomcat页面,访问nginx服务器ip可代理转发到tomcat页面

反向代理示例二:

使用nginx监听两个tomato不同端口,对外开放一个端口,通过ip名称正则表达式区分

修改nginx配置文件 nginx/conf/nginx.conf

你可能感兴趣的:(nginx,运维)