您的位置:首页 > 运维架构 > Nginx

web服务器nginx

2020-07-13 04:17 295 查看

web服务器nginx
    静态,php
    
一、使用源码包安装nginx软件包
[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel   ==>>安装软件依赖包
[root@proxy ~]# useradd -s /sbin/nologin nginx     ==>>创建一个用户,不可登录
[root@proxy ~]# tar -xf nginx-1.11.2.tar.gz 
[root@proxy ~]# cd nginx-1.11.2/
[root@proxy nginx-1.11.2]# ./configure \
> --prefix=/usr/local/nginx \        ==>>指定安装目录
> --user=nginx \                  ==>>指定用户
> --group=nginx \              ==>>指定用户组
> --with-http_ssl_module       ==>>开启ssl加密认证
[root@proxy nginx-1.11.2]# make && make install
[root@proxy nginx-1.11.2]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@proxy nginx-1.11.2]# /usr/local/nginx/sbin/nginx                ==>>启动nginx服务
[root@proxy nginx-1.11.2]# /usr/local/nginx/sbin/nginx -s reload      ==>>不关闭服务的情况下重启nginx服务 
[root@proxy nginx-1.11.2]# /usr/local/nginx/sbin/nginx -s stop        ==>>停止nginx服务
[root@proxy nginx-1.11.2]# /usr/local/nginx/sbin/nginx -V             ==>>查看软件信息
[root@proxy nginx-1.11.2]# netstat -antup |grep :80

-------------------------------------------------------------------------------------------------------------------------------------------------

nginx加入ststem
[root@web3 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]                                                ==>>对服务的说明
Description=nginx - high performance web server             ==>>描述服务
After=network.target remote-fs.target nss-lookup.target        ==>>描述服务类别

[Service]                                ==>>服务的一些具体运行参数的设置
Type=forking                                    ==>>后台运行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid           ==>>PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   ==>>启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf          ==>>启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload        ==>>重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop       ==>>停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit         ==>>快速停止
PrivateTmp=true                              ==>>给服务分配临时空间

[Install]
WantedBy=multi-user.target

-------------------------------------------------------------------------------------------------------------------------------------------------

二、用户认证
[root@proxy ~]# cd /usr/local/nginx/conf/
[root@proxy conf]# vim nginx.conf
server {
    listen       80;
    server_name  localhost;
    auth_basic "input pass";                            ==>>认证提示信息
    auth_basic_user_file "/user/local/nginx/pass";      ==>>认证的密码文件
        location / {
            root   html;
            index  index.html index.htm;
        }
}
[root@proxy conf]# yum -y install httpd-tools
[root@proxy conf]# htpasswd -c /usr/local/nginx/pass tom      ==>>创建认证用户,追加用户不使用-c选项
New password:                                             ==>>输入密码
Re-type new password: 
Adding password for user tom
[root@proxy conf]# cat /usr/local/nginx/pass 
tom:$apr1$0aYyFx3K$Hg3tIyGF8thT8/64LDIOF.
[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload

三、基于域名的虚拟主机
[root@proxy conf]# vim nginx.conf          ==>>修改文件,添加虚拟主机
server{
        listen  80;                           ==>>端口
        server_name www.a.com;               ==>>域名
        location / {
          root www;                         ==>>网站根目录
          index index.html index.htm;         
}
}
server {
        listen       80;
        server_name  www.b.com;
        auth_basic "input pass";
        auth_basic_user_file "/user/local/nginx/pass";
        location / {
            root   html;
            index  index.html index.htm;
        }
}
[root@proxy conf]# mkdir /usr/local/nginx/www                   ==>>为新域名添加内容
[root@proxy conf]# echo www >/usr/local/nginx/www/index.html
[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload
----------------------------------------------------------------------------------------------------
    扩展:
    基于端口的虚拟主机
    server {
            listen       80;
            server_name  www.b.com;
            location / {
                root   www;
                index  index.html index.htm;
            }
    }
    server {
            listen       8080;
            server_name  www.b.com;
            location / {
                root   html;
                index  index.html index.htm;
            }
    }
    基于IP的虚拟主机
    server {
            listen 192.168.4.2:80;
            server_name  www.a.com;
            location / {
                root   www;
                index  index.html index.htm;
            }
    }
    server {
            listen 192.168.4.1:80;
            server_name  www.b.com;
            location / {
                root   html;
                index  index.html index.htm;
            }
    }
-----------------------------------------------------------------------------------------------------

四、SSL虚拟主机
[root@proxy nginx]# cd /usr/local/nginx/conf/
[root@proxy conf]# openssl genrsa > cert.key       ==>>生成私钥
[root@proxy conf]# openssl req -new -x509 -key cert.key > cert.pem    ==>>生成证书
[root@proxy conf]# vim nginx.conf
server {
     listen       443 ssl;                 ==>>加密端口
     server_name  www.c.com;

     ssl_certificate      cert.pem;        ==>>证书
     ssl_certificate_key  cert.key;        ==>>私钥

     ssl_session_cache    shared:SSL:1m;
     ssl_session_timeout  5m;
 
     ssl_ciphers  HIGH:!aNULL:!MD5;
     ssl_prefer_server_ciphers  on;

     location / {
       root   html;
       index  index.html index.htm;
    }
}
[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: