源码安装nginx,并提供服务脚本

1. 源码安装nginx,并提供服务脚本。

安装nginx所需的pere库
pere,peri兼容正则表达式
yum install -y pcre-devel
安装nginx
安装openssl-devel
yum install -y openssl-devel
检查并安装nginx基础依赖包pcre-devel、openssl-devel
rpm -qa | egrep 'pcre-devel|openssl-devel'

开始安装nginx
创建组和用户
groupadd -r -g 995 nginx
useradd -r -u 995 -g 995 -s /sbin/nologin -M nginx
下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压
tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
在这里插入图片描述
下载编译器
yum install gcc gcc-c++ make -y
进入目录
cd /usr/local/src/nginx-1.18.0/
./configure --help

./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_stub_status_module

在这里插入图片描述
编译
make

安装
make install
在这里插入图片描述
修改配置文件
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/After=network-online.target remote-fs.targe nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
测试:
systemctl enable --now nginx
在这里插入图片描述
查看端口
netstat -lnupt | grep 80
在这里插入图片描述

在这里插入图片描述

2. 配置基于IP地址的虚拟主机。

基于rpm下载的nginx
参考http://nginx.org/en/linux_packages.html

新增IP地址

[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 172.25.10.110/24
[root@localhost ~]# nmcli connection up ens33 
[root@localhost ~]# nmcli connection reload 

在这里插入图片描述

创建站点目录
创建主页文件

[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# for name in bbs blog; do mkdir $name; done
[root@localhost html]# for name in bbs blog; do echo "$name test page." > $name/index.html;done 

在这里插入图片描述
配置文件添加虚拟主机部分

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim vhost.conf

server{
    listen  172.25.10.100:80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html/bbs/;
        index index.html index.htm;
    }
}

server{
    listen  172.25.10.110:80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html/blog/;
        index index.html index.htm;
    }
}

[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

在这里插入图片描述

重启服务

systemctl restart nginx

测试

在这里插入图片描述
在这里插入图片描述

3. 配置基于端口的虚拟主机。

[root@localhost conf.d]# vim vhost.conf

server{
    listen  80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html/bbs/;
        index index.html index.htm;
    }
}

server{
    listen  81;
    server_name localhost;
    location / {
        root /usr/share/nginx/html/blog/;
        index index.html index.htm;
    }
}

[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# systemctl restart nginx

在这里插入图片描述

4. 配置基于域名的虚拟主机。

[root@localhost conf.d]# vim vhost.conf

server{
    listen  80;
    server_name bbs.jzz.org;
    location / {
        root /usr/share/nginx/html/bbs/;
        index index.html index.htm;
    }
}

server{
    listen  80;
    server_name blog.jzz.org;
    location / {
        root /usr/share/nginx/html/blog/;
        index index.html index.htm;
    }
}
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# vim /etc/hosts
172.25.10.100 bbs.jzz.org blog.jzz.org
[root@localhost conf.d]# systemctl restart nginx

在这里插入图片描述

5. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

①定义错误页面内容:
echo "the require failed" > /usr/share/nginx/html/test1/err.html
在这里插入图片描述
②编辑配置文件:

[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf

修改内容如下:

server {
        listen  80;
        server_name www.test1.com;
        location / {
            root   /usr/share/nginx/html/test1/;
            index  index.html index.htm;
            if (!-f $request_filename) {
                rewrite /.* err.html permanent;}
        }
}

③重启服务
systemctl restart nginx

④在浏览器进行测试
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值