CentOS7搭建基于Nginx的web服务器(包含多域名,多端口,配置https访问)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiangshangbashaonian/article/details/82491484

1、添加YUM仓库
[root@localhost ~]# ls /etc/yum.repos.d/
[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# ls /etc/yum.repos.d/
[root@localhost ~]# mount /dev/cdrom /mnt
[root@localhost ~]# yum-config-manager --add file:///mnt
[root@localhost ~]# vim /etc/yum.repos.d/mnt.repo 
...

gpgcheck=0

...
[root@localhost ~]# yum clean all
[root@localhost ~]# yum repolist

2、安装Nginx
[root@localhost ~]# tar xf lnmp_soft.tar.gz
[root@localhost ~]# cd lnmp_soft/
[root@localhost lnmp_soft]# tar xf nginx-1.10.3.tar.gz
[root@localhost lnmp_soft]# cd nginx-1.10.3/
[root@localhost nginx-1.10.3]# useradd nginx
[root@localhost nginx-1.10.3]# yum -y install gcc pcre-devel openssl-devel
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@localhost nginx-1.10.3]# make && make install

3、启动Nginx
[root@localhost nginx-1.10.3]# ls /usr/local/nginx/sbin/
[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx
增加链接
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /bin/

4、平滑升级
[root@localhost ~]# cd /root/lnmp_soft/
[root@lnmp_soft ~]# tar xf nginx-1.12.2.tar.gz
[root@lnmp_soft ~]# cd nginx-1.12.2/
[[email protected] ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[[email protected] ~]# make
[[email protected] ~]# cd /usr/local/nginx/sbin/
[root@sbin ~]# mv nginx nginx.old
[root@sbin ~]# cd -
[[email protected] ~]# cp objs/nginx /usr/local/nginx/sbin/
[[email protected] ~]# make upgrade
[[email protected] ~]# nginx -V

5、配置用户认证
[root@localhost ~]# nginx
[root@localhost ~]# mount /dev/cdrom /mnt
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@conf ~]# cp nginx.conf nginx.conf.bak
[root@conf ~]# vim nginx.conf

location / {
        root   html;
        index  index.html index.htm;
    auth_basic 'Input your name and pass';
    auth_basic_user_file /usr/local/nginx/pass;
    }

[root@conf ~]# htpasswd -c /usr/local/nginx/pass admin
[root@conf ~]# nginx -s reload

6、基于域名的虚拟主机
[root@conf ~]# cd /usr/local/nginx/conf
[root@conf ~]# vim nginx.conf
server {

    listen       80;
    server_name  www.aa.com;
    location / {
        root   html;
        index  index.html index.htm;
    }

server {

    listen       80;
    server_name  www.bb.com;
    location / {
        root   www;
        index  index.html index.htm;
    }

}
[root@conf ~]# cd ..
[root@nginx ~]# mkdir www
[root@nginx ~]# echo www.aa.com > html/index.html
[root@nginx ~]# echo www.bb.com > www/index.html
[root@nginx ~]# nginx -s reload
[root@nginx ~]# vim /etc/hosts
...

127.0.0.1 www.aa.com  www.bb.com

...

[root@nginx ~]# firefox www.aa.com
[root@nginx ~]# firefox www.bb.com

7、添加https网站

[root@conf ~]# cd /usr/local/nginx/conf
[root@conf ~]# openssl genrsa > cert.key 2048
[root@conf ~]# openssl req -new -x509 -key cert.key > cert.pem
[root@conf ~]# vim nginx.conf
server {

    listen       443 ssl;
    server_name  www.cc.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@conf ~]# nginx -s reload
[root@conf ~]# vim /etc/hosts
...

127.0.0.1 www.cc.com

...
[root@conf ~]# firefox https://www.cc.com
dhhdhdh.PNG

猜你喜欢

转载自blog.csdn.net/xiangshangbashaonian/article/details/82491484