nginx服务部署,实战

nginx服务的部署,实战

##1. 常见web服务器

httpd|IIS 政府类,银行用的居多
nginx|tomcat|tengine 社区,电商用的多
nginx 京东、腾讯网、新浪、it猿网
httpd 中国人民政府网站、建设银行…
IIS 招商银行、中国银行、工商银行、农业银行…
tengine 简书、csdn、淘宝…

2. nginx的安装方式:

  • YUM安装部署
  • 源码安装部署

2.1YUM安装部署
2.1.1配置yum仓库:

  • 登录http://nginx.org官网,点击右侧的download:

在这里插入图片描述

  • 点击 当前页最下方的stable and mainline:

在这里插入图片描述

  • 然后选择RHEL/CentOS:

在这里插入图片描述

  • 复制配置文件,创建nginx.repo文件:
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
  • 现在就可以直接安装了,默认情况是安装稳定版的:

2.1.2 安装稳定版本:

[root@localhost ~]# yum install nginx -y
[root@localhost ~]# nginx -v
nginx version: nginx/1.20.1

2.1.3安装主线版本(最新版本):

 先安装yum的扩展包,开启nginx的主线版本仓库:
yum install yum-utils -y
yum-config-manager --enable nginx-mainline
yum install nginx -y
这个时候nginx 1.16的版本会直接被升级。
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.1

查看安装的模块:nginx -V
查看nginx安装目录:rpm -ql nginx
查看nginx配置文件目录:rpm -qc nginx

[root@localhost sbin]# rpm -ql  nginx 
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.21.1
/usr/share/doc/nginx-1.21.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
[root@localhost sbin]# rpm -qc  nginx 
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params

2.2 源码安装:

2.2.1登录http://nginx.org官网,点击右侧的download:
在这里插入图片描述

  • 主线版本:也叫开发版本,目前最新但是还没有经过大量测试的版本。
  • 稳定版本:稳定版通常是经过大量测试的,相对比较稳定的版本,企业
    中我们也会使用稳定版。
  • 历史版本:通常是往期的稳定版本。
    3.2.1 下载包:
#选择想要下载的版本,(这里演示1.20.1)直接单击右键复制地址下载:
wget http://nginx.org/download/nginx-1.20.1.tar.gz

3.2.2 安装依赖:

yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel
openssl openssl-devel -y

3.2.3 解压包:

tar xf nginx-1.20.1.tar.gz
cd nginx-1.20.1

3.2.4 预编译:
预编译主要是用来检查系统环境是否满足安装软件包的条件,
并生成Makefile文件,该文件为编译、安装、升级nginx指明了相应参数。
./configure --help 可以查看预编译参数
–prefix 指定nginx编译安装的目录;
–user=*** 指定nginx的属主
–group=*** 指定nginx的属主与属组
–with-*** 指定编译某模块
–without-** 指定不编译某模块
–add-module 编译第三方模块

**开始预编译:**
./configure --prefix=/usr/local/nginx
[root@www nginx-1.20.1]# cat Makefile
default: build
clean:
rm -rf Makefile objs
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat
/usr/local/nginx/logs/nginx.pid.oldbin`
make clean : 重新预编译时,通常执行这条命令删除上次的编译文件
make build : 编译,默认参数,可省略build参数
make install : 安装
make modules : 编译模块
make upgrade : 在线升级

3.2.5编译并安装

make && make install

查看版本

/usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.1

启动nginx:

/usr/local/nginx/sbin/nginx
检查进程及端口:

查看进程:

[root@localhost ~]# ps -ef|grep nginx
root 6853 1 0 20:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 7839 6853 0 21:35 ? 00:00:00 nginx: worker process

查看端口

[root@localhost ~]# netstat -ntlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6853/nginx: master

可以看到端口及进程表示nginx WEB服务已经搭建成功!

nginx的常用命令:
使用/usr/local/nginx/sbin/nginx -h命令查看可用参数:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.20.1
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
-v 可查看nginx的版本。
-V 可查看nginx的详细信息,包括编译的参数。
-t 可用来测试nginx的配置文件的语法错误。
-T 可用来测试nginx的配置文件语法错误,同时还可以通过重定向备份
nginx的配置文件。
-q 如果配置文件没有错误信息时,不会有任何提示,如果有错误,则提
示错误信息,与-t配合使用。
-s 发送信号给master处理:
stop 立刻停止nginx服务,不管请求是否处理完
quit 优雅的退出服务,处理完当前的请求退出
reopen 重新打开日志文件,原日志文件要提前备份改名。
reload 重载配置文件
-p 设置nginx家目录路径,默认是编译时的安装路径
-c 设置nginx的配置文件,默认是家目录下的配置文件
-g 设置nginx的全局变量,这个变量会覆盖配置文件中的变量。

命令演示:
如果觉得每次都需要输入绝对路径执行命令麻烦,可以通过以下几种方法
实现直接使用nginx命令。
1、做软连接:

ln -s /usr/local/nginx/sbin/* /usr/local/sbin

然后重新读取下配置文件

. /etc/profile

ps:软连接做在PATH路径是第一位,因为yum安装的在/usr/sbin/目录下,
which安装PATH的顺序找到第一个,就不找了。
2、配置环境变量:

echo "export PATH=/usr/local/nginx/sbin:$PATH" >/etc/profile.d/nginx.sh
然后重新读取下配置文件
source /etc/profile

ps:最好写在$PATH前面,否则,如果安装了yum版的nginx,
直接执行nginx会启动yum版的nginx,因为which nginx,会先找
到/usr/sbin/nginx文件
3、设置别名:

alias nginx='/usr/local/nginx/sbin/nginx'
ps:which优先找别名

启动nginx:

nginx

立即停止nginx

nginx -s stop

优雅停止nginx:

[root@node1 ]# nginx -s quit
[root@node1 ]# ps -ef | grep nginx
root 1547 1 0 14:48 ? 00:00:00 nginx: master process nginx
nginx 1548 1547 1 14:48 ? 00:00:09 nginx: worker process is shutting down
root 1576 1225 0 14:58 pts/0 00:00:00 grep --color=auto nginx

重新打开日志文件
该命令可用于日志切割,定期执行。

[root@localhost logs]# ls
access.log error.log nginx.pid
[root@localhost logs]# mv access.log{,.bak}
[root@localhost logs]# ls
access.log.bak error.log nginx.pid
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reopen
[root@localhost logs]# ls
access.log access.log.bak error.log nginx.pid

重载配置文件:
修改工作进程数,重启服务,对比前后进程数

[root@localhost logs]# ps -ef|grep nginx
root 2685 1 0 23:56 ? 00:00:00 nginx:master process /usr/local/nginx/sbin/nginx
nginx 2686 2685 0 23:56 ? 00:00:00 nginx:worker process
root 2691 2532 0 23:57 pts/1 00:00:00 grep --color=auto nginx
[root@localhost logs]# vim
/usr/local/nginx/conf/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost logs]# ps -ef|grep nginx
root 2685 1 0 23:56 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 2694 2685 0 23:58 ? 00:00:00 nginx: worker process
nginx 2695 2685 0 23:58 ? 00:00:00 nginx: worker process
nginx 2696 2685 0 23:58 ? 00:00:00 nginx: worker process
root 2698 2532 0 23:58 pts/1 00:00:00 grep --color=auto nginx

启动指定的配置文件:
在/data/目录下拷贝一份nginx的配置文件,然后修改用户名为www。
注意:配置文件中引用的其他配置文件路径也要做一个修改。

[root@localhost logs]# cp /usr/local/nginx/conf/nginx.conf /data/
[root@localhost logs]# vim /data/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx -c /data/nginx.conf
[root@localhost logs]# ps -ef|grep nginx
root 2736 1 0 00:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c
/data/nginx.conf
www 2737 2736 0 00:05 ? 00:00:00 nginx: worker process
www 2738 2736 0 00:05 ? 00:00:00 nginx: worker process
www 2739 2736 0 00:05 ? 00:00:00 nginx: worker process
root 2741 2532 0 00:05 pts/1 00:00:00 grep --color=auto nginx

设置全局变量
通过设置全局变量,让nginx在前端运行。

[root@localhost logs]# /usr/local/nginx/sbin/nginx -g "daemon off;"

现在当前nginx在前端运行,
输入ctrl +c,则nginx就退出了。
可以使用ctrl +z放置后台运行。

实现配置文件语法高亮:

cp -a contrib/vim/* /usr/share/vim/vimfiles/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值