Nginx安装和Nginx配置虚拟主机

Nginx安装

源码包获取地址:http://nginx.org/download/
RPM包获取地址:http://nginx.org/packages/centos/7Server/x86_64/RPMS/

RPM安装

这里选择的RPM包是 nginx-1.22.0-1.el7.ngx.x86_64.rpm

[root@localhost ~]# yum install nginx-1.22.0-1.el7.ngx.x86_64.rpm -y
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx

在这里插入图片描述

直接输入IP地址
在这里插入图片描述

#找到默认网站根目录的路径
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
root   /usr/share/nginx/html;
[root@localhost ~]# echo web test page > /usr/share/nginx/html/index.html

在这里插入图片描述

源码安装

这里选择源码包是nginx-1.22.0.tar.gz
安装之前确保安装了 gcc openssl-devel pcre-devel zlib-devel

[root@localhost ~]# yum install gcc openssl-devel pcre-devel zlib-devel -y
[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# grep nginx /etc/group
nginx:x:996:
[root@localhost ~]# useradd nginx -u 996 -r -g 996 -c "nginx user" -s /sbin/nologin 
[root@localhost ~]# yum install gcc gcc-c++ make -y
[root@localhost ~]# tar xf nginx-1.22.0.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/nginx-1.22.0/
[root@localhost nginx-1.22.0]# ./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

[root@localhost nginx-1.22.0]# make
#未出现错误
[root@localhost nginx-1.22.0]# make install

[root@localhost nginx-1.22.0]# 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.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target

[root@localhost nginx-1.22.0]# systemctl daemon-reload
[root@localhost nginx-1.22.0]# systemctl start nginx.service 
[root@localhost nginx-1.22.0]# systemctl status nginx.service 

在这里插入图片描述

[root@localhost ~]# vi /etc/nginx/nginx.conf
root   html; #相对路径
[root@localhost ~]# echo web test page 135 > /etc/nginx/html/index.html

在这里插入图片描述

Nginx虚拟主机

以rpm包安装的虚拟主机来进行实验

#创建目录
[root@localhost ~]# mkdir /usr/share/nginx//html/{bbs,blog}
[root@localhost ~]# echo blog test page > /usr/share/nginx//html/blog/index.html
[root@localhost ~]# echo bbs test page > /usr/share/nginx//html/bbs/index.html

基于IP的虚拟主机

[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.40.129/24
[root@localhost ~]# nmcli connection up ens33
[root@localhost ~]# cd /etc/nginx/conf.d/

[root@localhost conf.d]# vim vhost.conf
server {
    listen       192.168.40.128:80;
    server_name  localhost;
    error_log   /var/log/nginx/bbs_error.log;
    access_log  /var/log/nginx/bbs_access.log  main;
    
    location / {
        root   /usr/share/nginx/html/bbs;
        index  index.html index.htm;
    }
}
server {
    listen       192.168.40.129:80;
    server_name  localhost;
    error_log   /var/log/nginx/blog_error.log;
    access_log  /var/log/nginx/blog_access.log  main;
    
    location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
    }
}

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

基于端口的虚拟主机

修改完成重启服务

server {
    listen       81;
    server_name  localhost;
    error_log   /var/log/nginx/bbs_error.log;
    access_log  /var/log/nginx/bbs_access.log  main;

    location / {
        root   /usr/share/nginx/html/bbs;
        index  index.html index.htm;
    }
}
server {
    listen       82;
    server_name  localhost;
    error_log   /var/log/nginx/blog_error.log;
    access_log  /var/log/nginx/blog_access.log  main;

    location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
    }
}

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

基于域名的虚拟主机

server {
    listen       80;
    server_name  bbs.open.cn;
    error_log   /var/log/nginx/bbs_error.log;
    access_log  /var/log/nginx/bbs_access.log  main;

    location / {
        root   /usr/share/nginx/html/bbs;
        index  index.html index.htm;
    }
}
server {
    listen       80;
    server_name  blog.open.cn;
    error_log   /var/log/nginx/blog_error.log;
    access_log  /var/log/nginx/blog_access.log  main;

    location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
    }
}

#在C:\Windows\System32\drivers\etc\hosts中添加
192.168.40.128  bbs.open.cn blog.open.cn

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值