第3课:Nginx部署与配置
——DevOps基础培训
1. Nginx
1.1 什么是Nginx?
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
1.2 安装Nginx1.18.0版本
使用命令yum install nginx
发现无法找到nginx,那么可以在官网下载对应版本1.18.0;然后将其拷贝至/root
文件夹中;随后解压tar -zxvf nginx-1.18.0.tar.gz
、编译安装cd nginx-1.18.0
./configure
make
make install
、清除缓存make clean
;检验是否安装成功,使用命令/usr/local/nginx/sbin/nginx -v
查看版本号。
设置为开机自启动:首先创建并修改nginx.service文件vim /usr/lib/systemd/systen/nginx.service
随后开机并设置为自启动systemctl start nginx.service
systemctl enable nginx
1.3 将默认监听的80端口修改为8081并启动
首先启动nginx:
发现监听的80端口被禁用
修改nginx的配置文件,将端口改为8081:
然后再次启动即可
1.4 通过浏览器访问网站
首先关闭防火墙iptables和firewalld
service iptables stop
systemctl stop firewalld
然后更改nginx配置文件:
vim /usr/local/nginx/conf/nginx.conf
将第一行改为user root root
将server改为:
然后重载/usr/local/nginx/sbin/nginx -s reload
再使用外部浏览器访问:
2. html
2.1 html基本语法
由于之前学过,这里仅放链接
2.2 修改nginx默认页面内容
修改/usr/local/neginx/html/index.html
内容
再次打开网页
3. hosts
3.1 什么是hosts?
hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”。
当用户在浏览器中输入一个需要登录的网址时,系统会首先从hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。
3.2 修改hosts使域名指向IP地址
- 首先更改服务器的hosts文件:
vim /etc/hosts
在文件下方增加一行36.158.226.97 www.hanqihua.test.com
- 更改Windows的hosts文件:
C:\Windows\System32\drivers\etc\hosts
- 最后在windows下访问
www.hanqihua.test.com:8081
即可。
3.3此外
我自己也买了一个域名地址www.hanqihua.site
,通过域名解析,将域名与自己的服务器绑定,只需要输入www.hanqihua.site:8081
即可访问。
4. nginx日志
4.1 查看nginx日志
nginx日志分为访问日志与错误日志两种。通过访问日志我们可以得到用户IP地址、浏览器信息、请求的处理时间等信息;错误日志记录访问出错的信息,可以帮助我们定位错误的原因。
通过查看文件/usr/local/nginx/logs/access.log
查看日志。
4.2 配置日志信息
4.3 配置日志按日进行滚动
修改nginx配置文件vim /usr/local/nginx/conf/nginx.conf
为:
保存后刷新,再次进入网站,即可看到按时间排列的log文件。
5. 将nginx加入系统服务
在/etc/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
设置文件权限:cd /etc/init.d chmod 777 /etc/init.d/nginx
将nginx加入系统服务:chkconfig --add nginx
先关闭原来运行的systemctl stop nginx
nginx:fuser -k 8081/tcp
然后开启nginx:systemctl start nginx
查看nginx运行状态:systemctl status nginx
6. 修改iptables控制8081端口:
- 查看iptables状态:
systemctl status iptables
发现是关闭状态。 - 打开iptables:
systemctl start iptables
- 查看带编号的开放端口:
iptables -vnL --line-number
发现没有端口8081。此时用火狐浏览器访问网站,发现无法访问(使用火狐浏览器原因是,之前使用谷歌浏览器访问,留下了缓存,结果即是开启防火墙,依然能够显示网站内容) - 使用命令
iptables -I INPUT -p tcp --dport 8081 -j ACCEPT
,将8081端口添加到iptables允许访问端口。 - 保存规则:
service iptables save
- 重新访问网页,发现访问成功!