分享

Linux学习9-CentOS搭建nginx环境

 上海悠悠 2021-05-27

前言

之前我们搭建网站的时候,把war包放到tomcat下就能运行起来了,为什么部署上线的时候,又用到了nginx呢?
nginx可以做多台服务器的负载均衡,当用户非常少的时候,可以用一台服务直接部署web环境,那么当用户达到百万级别,千万级别的时候,就需要增加服务器,多台服务器又如何管理协作的呢?

nginx有以下功能:
1.静态HTTP服务器-Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。
2.反向代理服务器-客户端本来可以直接通过HTTP协议访问某网站应用服务器,网站管理员可以在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向代理服务器。
3.负载均衡-当网站访问量非常大,网站站长开心赚钱的同时,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。
于是将同一个应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其他服务器正常运行,就不会影响用户使用。
4.虚拟主机-有的网站访问量大,需要负载均衡。然而并不是所有网站都如此出色,有的网站,由于访问量太小,需要节省成本,将多个网站部署在同一台服务器上。
5.FastCGI-Nginx本身不支持PHP等语言,但是它可以通过FastCGI来将请求扔给某些语言或框架处理(例如PHP、Python、Perl)。

什么是nginx?

Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;
Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。

正向代理,代理的是客户端,比如小伙伴们平常科学上网,访问google网站就是用到的正向代理。

反向代理,它代理的是服务端,主要用于服务器集群分布式部署的情况下,反向代理隐藏了服务器的信息。

nginx依赖包安装

先安装依赖包

1.gcc安装:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
2.PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。
nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
3.zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
4.OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

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

nginx下载与安装

直接下载.tar.gz安装包,下载地址:https:///en/download.html

把下载的包放到/usr/local/nginx目录,然后解压安装到nginx-1.12.0当前目录,make编译安装

[root@yoyo sbin]# cd ~ [root@yoyo ~]# cd /usr/local/ [root@yoyo local]# mkdir nginx [root@yoyo local]# cd nginx [root@yoyo nginx]# wget -c https:///download/nginx-1.12.0.tar.gz [root@yoyo nginx]# tar -zxvf nginx-1.12.0.tar.gz [root@yoyo nginx]# cd nginx-1.12.0 # 安装到当前目录 [root@yoyo nginx]# ./configure [root@yoyo nginx]# make [root@yoyo nginx]# make install

到此为止环境已经安装好,接下来启动nginx服务

[root@yoyo nginx]# cd /usr/local/nginx/sbin/ [root@yoyo nginx]# ./nginx

启动服务后,nginx默认是在80端口启动的,在浏览器输入http://47.104.x.x:80/  (80端口默认可以省略),能正常访问到页面,说明服务启动成功

相关指令

先cd到/usr/local/nginx/sbin/

1.启动服务

./nginx

2.停止服务,此方式停止步骤是待nginx进程处理任务完毕进行停止。

./nginx -s stop

3.退出服务,此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

./nginx -s quit

4.重新加载,当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,
使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效

./nginx -s reload

5.查询nginx进程

ps aux|grep nginx

开机自启动

在系统服务目录里创建nginx.service文件

vim /lib/systemd/system/nginx.service

内容如下

Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

:wq保存退出。

[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target

1.设置开机自启动

systemctl enable nginx.service

2.停止开机自启动

systemctl disable nginx.service

3.启动服务

systemctl start nginx.service 

4.重新启动服务

systemctl restart nginx.service 

5.查看所有已启动的服务

systemctl list-units —type=service

修改nginx启动端口

如果80端口之前已经使用过了,可以修改nginx的服务端口,先cd到/usr/local/nginx/conf目录
为了保险起见,编辑前先备份下原来的文件:cp nginx.conf nginx.conf.bak

[root@yoyo ~]# cd /usr/local/nginx/conf [root@yoyo conf]# ll total 60 -rw-r--r-- 1 root root 1077 Jan  8 14:16 fastcgi.conf -rw-r--r-- 1 root root 1077 Jan  8 14:16 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Jan  8 14:16 fastcgi_params -rw-r--r-- 1 root root 1007 Jan  8 14:16 fastcgi_params.default -rw-r--r-- 1 root root 2837 Jan  8 14:16 koi-utf -rw-r--r-- 1 root root 2223 Jan  8 14:16 koi-win -rw-r--r-- 1 root root 3957 Jan  8 14:16 mime.types -rw-r--r-- 1 root root 3957 Jan  8 14:16 mime.types.default -rw-r--r-- 1 root root 2656 Jan  8 14:16 nginx.conf -rw-r--r-- 1 root root 2656 Jan  8 14:16 nginx.conf.default -rw-r--r-- 1 root root  636 Jan  8 14:16 scgi_params -rw-r--r-- 1 root root  636 Jan  8 14:16 scgi_params.default -rw-r--r-- 1 root root  664 Jan  8 14:16 uwsgi_params -rw-r--r-- 1 root root  664 Jan  8 14:16 uwsgi_params.default -rw-r--r-- 1 root root 3610 Jan  8 14:16 win-utf [root@yoyo conf]# cp nginx.conf nginx.conf.bak [root@yoyo conf]# vim nginx.conf

vim打开后,找到服务端口listen       80这段,输入键盘上i键后编辑,改成81

   server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }

编辑完成后按Esc键,输入:wq保存退出

修改后重新加载下配置文件

[root@yoyo sbin]# cd /usr/local/nginx/sbin/ [root@yoyo sbin]# ./nginx -s reload

接着去阿里云ECS服务后台-安全组-新增规则-添加81端口,在浏览器上就能访问了

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多