前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于centos7安装部署nginx

基于centos7安装部署nginx

作者头像
运维朱工
发布2021-11-28 11:31:17
1.2K2
发布2021-11-28 11:31:17
举报
文章被收录于专栏:云计算教程云计算教程
web服务器简介:

WEB服务器主要功能是提供网上信息浏览服务。目前我们上网浏览信息,都是由不同的web服务器提供的,企业通常都会有自己的门户网站,所以对于web服务器的掌握也是至关重用。下面我们可以熟悉下常见的web服务器有哪些。

1. 常见web服务器:
代码语言:txt
复制
常见web服务器举例(不限于这些):nginx、tengine、httpd、tomcat、IIS、lighttpd

# web服务器分布:(哪些企业喜欢用什么web服务器)
httpd|IIS		政务机关,银行用的居多
nginx|tomcat|tengine		社区,电商用的多

nginx		京东、腾讯网、新浪、it猿网,镜像站 
httpd		中国人民政府网站、建设银行..
IIS			招商银行、中国银行、工商银行、农业银行...	
tengine     简书、csdn、淘宝...
2. nginx 的发展:
  • 第一个公开版本0.1.0发布于2004年10月4日。
  • Nginx 的1.4.0稳定版已经于2013年4月24日。
  • Nginx目前最新的稳定版本是1.20.2,于 2021-11-16号发布。
3. nginx的安装方式:
  • YUM安装部署
  • 源码安装部署
3.1 yum安装:
3.1.1 配置nginx的yum源:
  • 登录nginx官网,点击右侧的download
image.png
image.png
  • 点击 当前页最下方的stable and mainline
image.png
image.png
  • 然后选择RHEL/CentOS
image.png
image.png
  • 复制配置文件,在/etc/yum.repos.d/目录下创建nginx.repo文件:
代码语言:txt
复制
[root@www.lutixia.cn ~]# 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

[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
  • 现在就可以直接安装了,默认情况是安装稳定版的
3.1.1 安装稳定版本:
代码语言:txt
复制
[root@www.lutixia.cn ~]# yum install nginx -y
[root@www.lutixia.cn ~]# nginx -v
nginx version: nginx/1.20.2
3.1.2 安装主线版本:
代码语言:txt
复制
# 先安装yum的扩展包,开启nginx的主线版本仓库:
[root@www.lutixia.cn ~]# yum  install yum-utils -y
[root@www.lutixia.cn ~]# yum-config-manager --enable nginx-mainline
[root@www.lutixia.cn ~]# yum install nginx -y

这个时候nginx 1.20.2的版本会直接被升级。

代码语言:txt
复制
[root@www.lutixia.cn ~]# nginx -v
nginx version: nginx/1.21.4
3.2 源码安装:

登录nginx官网,点击右侧的download

image.png
image.png
  • 主线版本:也叫开发版本,目前最新但是还没有经过大量测试的版本。
  • 稳定版本:稳定版通常是经过大量测试的,相对比较稳定的版本,企业中我们也会使用稳定版。
  • 历史版本:通常是往期的稳定版本。
3.2.1 下载包:
代码语言:txt
复制
# 选择想要下载的版本,直接单击右键复制地址下载:
[root@www.lutixia.cn ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
3.2.2 安装依赖:
代码语言:txt
复制
[root@www.lutixia.cn ~]# yum install gcc  gcc-c++  pcre-devel zlib-devel  openssl-devel -y
3.2.3 解压包:
代码语言:txt
复制
[root@www.lutixia.cn ~]# tar xf nginx-1.20.2.tar.gz
[root@www.lutixia.cn ~]# cd nginx-1.20.2
3.2.4 预编译:
代码语言:txt
复制
预编译主要是用来检查系统环境是否满足安装软件包的条件,
并生成Makefile文件,该文件为编译、安装、升级nginx指明了相应参数。

./configure --help 可以查看预编译参数
--prefix       指定nginx编译安装的目录;
--user=***     指定nginx的属主
--group=***    指定nginx的属主与属组
--with-***     指定编译某模块
--without-**   指定不编译某模块
--add-module   编译第三方模块

开始预编译:

代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# ./configure --prefix=/usr/local/nginx
代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# 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 编译并安装
代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# make  &&  make install
3.2.6 查看版本
代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2
3.2.7 启动nginx:
代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# /usr/local/nginx/sbin/nginx
3.2.8 检查进程及端口:
代码语言:txt
复制
# 查看进程:
[root@www.lutixia.cn nginx-1.20.2]# 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@www.lutixia.cn nginx-1.20.2]# netstat -ntlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6853/nginx: master

可以看到端口及进程表示nginx WEB服务已经搭建成功!这时可以访问部署nginx服务的服务器ip,可以看到welcome欢迎信息。

4. nginx的常用命令:

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

代码语言:txt
复制
[root@www.lutixia.cn nginx-1.20.2]# /usr/local/nginx/sbin/nginx  -h
nginx version: nginx/1.20.2
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/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
4.1 命令解读:
代码语言:txt
复制
-v      可查看nginx的版本。
-V      可查看nginx的详细信息,包括编译的参数。
-t      可用来测试nginx的配置文件的语法错误。
-T      可用来测试nginx的配置文件语法错误,同时还可以通过重定向备份nginx的配置文件。
-q      如果配置文件没有错误信息时,不会有任何提示,如果有错误,则提示错误信息,与-t配合使用。
-s      发送信号给master处理:
        stop    立刻停止nginx服务,不管请求是否处理完
        quit    优雅的退出服务,处理完当前的请求退出
        reopen  重新打开日志文件,原日志文件要提前备份改名。
        reload  重载配置文件
-p      设置nginx家目录路径,默认是编译时的安装路径
-e 		指定错误日志路径
-c      设置nginx的配置文件,默认是家目录下的配置文件
-g      设置nginx的全局变量,这个变量会覆盖配置文件中的变量。
4.2 命令演示:

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

代码语言:txt
复制
1、做软连接:
[root@www.lutixia.cn ~]# ln  -s /usr/local/nginx/sbin/* /usr/local/sbin
然后重新读取下配置文件
[root@www.lutixia.cn ~]# . /etc/profile

ps:软连接做在PATH路径是第一位,因为yum安装的在/usr/sbin/目录下,
which按照PATH的顺序找到第一个,就不找了。

2、配置环境变量:
[root@www.lutixia.cn ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh

然后重新读取下配置文件
[root@www.lutixia.cn ~]# source /etc/profile

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

3、设置别名:

[root@www.lutixia.cn ~]# alias  nginx='/usr/local/nginx/sbin/nginx'

ps:which优先找别名
4.2.1 启动nginx:
代码语言:txt
复制
[root@www.lutixia.cn ~]# nginx
4.2.2 立即停止nginx:
代码语言:txt
复制
[root@www.lutixia.cn ~]# nginx  -s stop
4.2.3 优雅停止nginx:
代码语言:txt
复制
[root@www.lutixia.cn ~]# nginx -s quit
4.2.4 重新打开日志文件

该命令可用于日志切割,定期执行。

代码语言:txt
复制
[root@www.lutixia.cn logs]# ls
access.log  error.log  nginx.pid
[root@www.lutixia.cn logs]# mv access.log{,.bak}
[root@www.lutixia.cn logs]# ls
access.log.bak  error.log  nginx.pid
[root@www.lutixia.cn logs]# /usr/local/nginx/sbin/nginx -s reopen
[root@www.lutixia.cn logs]# ls
access.log  access.log.bak  error.log  nginx.pid
4.2.5 重载配置文件:

修改工作进程数,重启服务,对比前后进程数

代码语言:txt
复制
[root@www.lutixia.cn 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@www.lutixia.cn logs]# vim /usr/local/nginx/conf/nginx.conf
[root@www.lutixia.cn logs]# /usr/local/nginx/sbin/nginx -s reload
[root@www.lutixia.cn 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
4.2.6 启动指定的配置文件:

在/data/目录下拷贝一份nginx的配置文件,然后修改用户名为www。

==注意==:配置文件中引用的其他配置文件路径也要做一个修改。

代码语言:txt
复制
[root@www.lutixia.cn logs]# cp /usr/local/nginx/conf/nginx.conf /data/
[root@www.lutixia.cn logs]# vim /data/nginx.conf
[root@www.lutixia.cn logs]# /usr/local/nginx/sbin/nginx  -c /data/nginx.conf 
[root@www.lutixia.cn 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
4.2.7 设置全局变量

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

代码语言:txt
复制
[root@www.lutixia.cn logs]# /usr/local/nginx/sbin/nginx  -g "daemon off;"

现在当前nginx在前端运行,
输入ctrl +c,则nginx就退出了。
4.2.8 实现配置文件语法高亮:
代码语言:txt
复制
[root@www.lutixia.cn ~]# cp -r /usr/src/nginx-1.20.2/contrib/vim/* /usr/share/vim/vimfiles/

nginx的安装部署初始化就到此结束了。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • web服务器简介:
  • 1. 常见web服务器:
  • 2. nginx 的发展:
  • 3. nginx的安装方式:
    • 3.1 yum安装:
      • 3.1.1 配置nginx的yum源:
      • 3.1.1 安装稳定版本:
      • 3.1.2 安装主线版本:
    • 3.2 源码安装:
      • 3.2.1 下载包:
      • 3.2.2 安装依赖:
      • 3.2.3 解压包:
      • 3.2.4 预编译:
      • 3.2.5 编译并安装
      • 3.2.6 查看版本
      • 3.2.7 启动nginx:
      • 3.2.8 检查进程及端口:
  • 4. nginx的常用命令:
    • 4.1 命令解读:
      • 4.2 命令演示:
        • 4.2.1 启动nginx:
        • 4.2.2 立即停止nginx:
        • 4.2.3 优雅停止nginx:
        • 4.2.4 重新打开日志文件
        • 4.2.5 重载配置文件:
        • 4.2.6 启动指定的配置文件:
        • 4.2.7 设置全局变量
        • 4.2.8 实现配置文件语法高亮:
    相关产品与服务
    云服务器
    云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档