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

目录

    • 1. 源码安装nginx,并提供服务脚本。
    • 2. 配置基于IP地址的虚拟主机。
    • 3. 配置基于端口的虚拟主机。
    • 4. 配置基于域名的虚拟主机。
    • 5. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

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/
源码安装nginx,并提供服务脚本_第1张图片
下载编译器
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

源码安装nginx,并提供服务脚本_第2张图片
编译
make

安装
make install
源码安装nginx,并提供服务脚本_第3张图片
修改配置文件
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
在这里插入图片描述

源码安装nginx,并提供服务脚本_第4张图片

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 

源码安装nginx,并提供服务脚本_第5张图片

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

[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 

源码安装nginx,并提供服务脚本_第6张图片
配置文件添加虚拟主机部分

[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

源码安装nginx,并提供服务脚本_第7张图片

重启服务

systemctl restart nginx

测试

源码安装nginx,并提供服务脚本_第8张图片
源码安装nginx,并提供服务脚本_第9张图片

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

源码安装nginx,并提供服务脚本_第10张图片

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

源码安装nginx,并提供服务脚本_第11张图片

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

①定义错误页面内容:
echo "the require failed" > /usr/share/nginx/html/test1/err.html
源码安装nginx,并提供服务脚本_第12张图片
②编辑配置文件:

[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

④在浏览器进行测试
源码安装nginx,并提供服务脚本_第13张图片

你可能感兴趣的:(运维,nginx,运维,服务器)