Linux初学(十三)中间件

本文详细介绍了如何在Linux上安装Nginx,包括编译安装和yum安装,以及Nginx的配置过程,如关闭防火墙和SELinux、启动与停止、配置文件结构和部署多个网站的方法。同时,还提供了一个简单的Nginx服务脚本,便于管理和操作。
摘要由CSDN通过智能技术生成

一、Nginx

简介

Nginx是一个高性能的HTTP和反向代理web服务器
轻量级、高性能

1.1 Nginx安装

方法一:编译安装

  • 依赖:openssl-devel、zlib-devel、ncurses-devel、pcre-devel、gcc、gcc-c++

方法二:yum安装

  • Nginx的rpm包在epel源中

编译安装Nginx

下载位置:https://nginx.org

第一步:下载

[root@localhost html]# wget http://nginx.org/download/nginx-1.24.0.tar.gz

第二步:安装依赖

[root@localhost ~]# yum install openssl-devel zlib-devel ncurses-devel \
pcre-devel gcc gcc-c++ -y

第三步:安装Nginx

[root@localhost ~]# tar xvf nginx-1.24.0.tar.gz
[root@localhost ~]# cd nginx-1.24.0
[root@localhost nginx-1.24.0]#./configure --prefix=/usr/local/nginx && make && make install

Nginx的目录结构

[root@localhost ~]# cd /usr/local/nginx
[root@localhost ~]# ls
conf html logs sbin
  • conf:这个目录存放的是Nginx的配置文件
  • html:这个目录是Nginx默认网站的根目录
  • logs:这个是Nginx的日志文件目录
  • sbin:这个是Nginx的启动程序的目录

1.2 关闭防火墙和selinux

systemctl stop firewalld

systemctl disable firewalld

sed -i s/SELINUX=enforcing/SELINUX=disabled/g   /etc/selinux/config    #永久关闭

setenforce 0    #临时关闭

1.3 启动Nginx

启动程序:

方法一:
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
[root@localhost sbin]# lsof -i :80
方法二:
export PATH=$PATH:/usr/local/nginx/sbin/
nginx

b 扩展:重启Nginx

方法一:
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -s reload

方法二:
先kill -9杀死进程,然后启动

方法三:
pkill nginx
/usr/local/nginx/sbin/nginx

方法四:
PATH=$PATH:/usr/local/nginx/sbin/nginx
nginx

扩展:让Nginx开机自动启动

[root@localhost ~]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.d/rc.local
[root@localhost ~]#chmod +x /etc/rc.d/rc.local

关闭Nginx

[root@localhost ~]#pkill nginx

1.4 Nginx的配置文件

配置文件的位置

yum安装:/etc/nginx/
编译安装:安装位置/conf/

文件名

nginx.conf

配置文件的基本结构

全局段:
       可以什么都没有
       可以有

http段: - 虚拟主机段

配置文件的格式

worker_processes  3;            #设置Nginx的工作进程数为3
events {                        #开始定义事件相关的配置
    worker_connections 1024;    #每个工作进程允许的最大连接数为1024
}                               #结束事件相关的配置

http {                       #开始定义HTTP的配置
    include mime.types       #包含MIME类型映射文件,用于确定请求资源的类型
    default_type application/octet-stream;    #设置默认的MIME类型为二进制流
    sendfile on;                              #开启高效文件传输模式
    keepalive_timeout  65;                    #设置长连接超时时间为65秒

    server {               #开始定义一个服务器块
        listen 80;         #监听80端口
        server_name www.web1.com;                #设置服务器名称为www.web1.com
        root /usr/local/nginx/html;              #设置服务器根目录为/usr/local/nginx/html
        index index.html index.htm;              #设置默认的索引文件为index.html和index.htm
        access_log logs/host.access.log main;    #设置访问日志文件为logs/host.access.log,使用main日志格式
        error_log logs/host.error.log main;      #设置错误日志文件为logs/host.error.log,使用main日志格式。
    }    #结束服务器块配置

}    #结束HTTP相关的配置

1.5 基于nginx发布多个网站

第一步:更改配置文件

位置:/urs/local/nginx/conf/nginx.conf

注意:一个server模块就是一个网站,现在部署三个网站

worker_processes  3;
events {
    worker_connections 1024;
}

http {
    include mime.types
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name www.web1.com;
        root /usr/local/nginx/html/web1;
        index index.html index.htm;
    }

    server {
        listen 80;
        server_name www.web2.com;
        root /usr/local/nginx/html/web2;
        index index.html index.htm;
    }

    server {
        listen 80;
        server_name www.web3.com;
        root /usr/local/nginx/html/web3;
        index index.html index.htm;
    }
}

第二步:修改自己电脑的hosts文件

位置:C:\windows\system32\dirvers\etc\hosts

添加三行:

        192.168.1.96  www.web1.com
        192.168.1.96 www.web2.com
        192.168.1.96 www.web3.com
注意:

        需要把hosts文件,拖到桌面上,才能修改并保存,然后拖回去
        IP地址,是自己Linux的IP

第三步:创建文件

echo "1111111"  >/usr/local/nginx/html/web1/a.html

echo "222222"  >/usr/local/nginx/html/web2/b.html

echo "333333"  >/usr/local/nginx/html/web3/c.html

 第四步:浏览器访问

在浏览器中输入域名:www.web1.com/a.html
在浏览器中输入域名:www.web2.com/b.html

在浏览器中输入域名:www.web3.com/c.html

1.6 nginx服务脚本

#!/bin/bash

#判断用户是否传进来一个参数
if [ $# -ne 1 ];then
    echo "UseAge: $0 start|restart|stop/status"
    exit 3;
fi
#定义一个参数,用来判断nginx是否已经启动
lsof -i :80 | grep nginx &>/dev/null
flag=$?

check_status(){
    if [ f$lag -eq 0 ];then
        echo "nginx is already running..."
    else
        echo "ngins is already stopped"
    fi
}

start_nginx() {
    if [ $flag -eq 0 ];then
        echo "Nginx is already running...."
        exit;
    else
        /usr/local/nginx/sbin/nginx
        echo "nginx start success"
        exit;
    fi
}

stop_nginx(){
    if [ $flag -ne 0 ];then
        echo "nginx is already stop"
        exit;
    else
        pkill nginx;
        echo "nginx stop success"
        exit;
    fi
}

restart_nginx(){
    if [ $flag -gt 0 ];then
        start_nginx;
        echo "nginx restart success"
        exit;
    else
        stop_nginx;
        start_nginx;
        echo "nginx restart success"
        exit;
    fi
}
if [ $1 == "start" ];then
    start_nginx
elif [ $1 == "status" ];then
    check_status
elif [ $1 == "stop" ];then
    stop_nginx
elif [ $1 == "restart" ];then
    restart_nginx
else
    echo "UseAge:$0 start|stop|restart|status"
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ys52IT

你的鼓励将是我创作的最大动力你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值