实训day16(7.29)

一.Web

为用户提供互联网上浏览信息的服务,web服务是动态的,可交互的。

1.安装httpd

yum -y install httpd

2.启动

systemctl start httpd

3.关闭防火墙

systemctl stop firewalld

[root@rs html]# echo "我手机号是" > index.html

[root@rs html]# ls

index.html

4.在浏览器中输入ip地址进行访问

二.http协议

1.http特点:

静态文件和动态文件

生成一个大文件

dd if=/dev/zero of=/var/www/html/a.txt bs=30M count=1

2.UEI和URL的区别

http状态码:

三.Apache

最早的web服务,基于http提供浏览器浏览

1.查看华为云主机所有打开的端口

firewall-cmd --list-ports

2.在虚拟机上停用防火墙,远程主机就无法访问web服务

3.搭建apache服务器

(1)查看安装情况

[root@rs ~]# rpm -qa|grep httpd

(2)配置文件

[root@rs ~]# vim /etc/httpd/conf/httpd.conf

启(3)启动http服务

[root@rs ~]# systemctl start httpd

(4)在浏览器访问

(5)在windows客户端scp一张图到/var/www/html下

[root@rs ~]# cd /var/www/html

[root@rs html]# mkdir img

[root@rs html]# vim /var/www/html/index.html

<!doctype html>

<html>

        <head>

                <meta charset="utf-8">

                <title>正方形</title>

                <style>

                        div{

                                background-color:red;

                                width:120px;

                                height:120px;

                        }

                </style>

        </head>

        <body>

                <div>正方形</div>

                <img src="img/000.jpg" />

        </body>

</html>

(6)在浏览器访问

四.Nginx

开源,轻量级,高性能的http和反向代理服务器

1.特点:占用内存少,并发能力强

2.作用:用来做负载均衡和反向代理

安装nginx

源码编译安装

3.下载源码包

[root@web ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz

4.解压

[root@web ~]# tar -zxvf nginx-1.26.1.tar.gz

[root@web ~]# yum -y install gcc gcc-c++

[root@web ~]# yum -y install openssl-devel

[root@web ~]# yum -y install pcre-devel

[root@web ~]# yum -y install make

5.编译安装nginx

[root@web nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

[root@web nginx-1.26.1]# make && make install

[root@web nginx-1.26.1]# useradd -s /bin/nologin -M nginx  //创建nginx用户和组不然无法启动

6.检查目录

[root@web nginx-1.26.1]# tree /usr/local/nginx/

7.启动nginx

[root@web nginx]# ./sbin/nginx

8.查看主要配置文件

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

9.优化nginx服务控制

[root@web nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/  //做一个软连接

[root@web nginx]# nginx

之所以指令能在命令行使用,是因为在$PATH目录中能找到这个可执行文件或者是可执行文件的链接文件

(1)修改配置文件后重载nginx服务

./nginx -s reload

(2)脚本启动nginx

[root@web nginx]# vim ~/nginx.sh

#!/bin/bash

/usr/local/sbin/nginx &> /dev/null

netstat -lnput|grep nginx

if [ $? -eq 0 ];then

    echo "nginx正在执行,或者80端口被占用"

fi

[root@web nginx]# source ~/nginx.sh

(3)以systemctl控制nginx

[root@web nginx]# ls /usr/lib/systemd/system

[root@web nginx]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=Flase

[Install]
WantedBy=multi-user.target

[root@web nginx]# systemctl daemon-reload  //重置systemctl进程

如果使用sbin目录下的nginx,就无法使用systemctl

优化注意事项

10.开启nginx状态监听模块

(1)修改配置文件

[root@web ~]# vim /usr/local/nginx/conf/nginx.conf  //在48行添加

location /status {

            stub_status on;    #nginx状态的监听模块

            access_log off;

        }

(2)重启nginx

[root@web ~]# systemctl reload nginx

(3)在浏览器访问,查看nginx状态信息

192.168.2.35/status

  1. nginx虚拟主机配置

⼀个“location”相当于⼀个虚拟主机,也就是⽤户访问⽹站时,

点击跳转的另⼀个⻚⾯。

location 内可以添加 nginx 各种功能模块。

配置多个虚拟主机

[root@server ~]# vim /usr/local/nginx/conf/nginx.conf

......省略部分内容......

server {

listen 80; #监听端⼝

server_name localhost;

charset utf-8;  #中文字符集

#access_log logs/host.access.log main;

location /status {

stub_status on;

access_log off;

}

location / {

root html; #⽹站在服务器上的⽬ 录,默认为/usr/local/nginx/html

index index.html index.htm; #跳转到的⽹站⾸⻚

}

}

......省略部分内容......

[root@server ~]# systemctl restart nginx.service

#重启nginx

12.nginx反向代理

再准备一台机器---tomcat 跟上面一样安装nginx编译并安装

[root@server ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz

解压

[root@server ~]# tar -zxvf nginx-1.26.1.tar.gz

[root@server ~]# yum -y install gcc gcc-c++

[root@server ~]# yum -y install openssl-devel

[root@server ~]# yum -y install pcre-devel

[root@server ~]# yum -y install make

编译安装nginx

[root@server nginx-1.26.1]# ./configure --prefix=/usr/local/nginx

[root@server nginx-1.26.1]# make && make install

[root@server nginx-1.26.1]# useradd -s /bin/nologin -M nginx

[root@server nginx-1.26.1]# echo "我是后端服务" > /usr/local/nginx/html/index.html

[root@server nginx-1.26.1]# firewall-cmd --zone=public --add-port=80/tcp --permanent //打开端口

success

[root@server nginx-1.26.1]# firewall-cmd --reload  //重新加载防火墙

Success

[root@server nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf

[root@server nginx-1.26.1]# /usr/local/nginx/sbin/nginx

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值