快速上手Linux(四):WebSercive安装和配置详解:Apache/Nginx

WebService安装和配置详解

1.Apache

1.1Apache基本操作

安装 yum install httpd
启动 service httpd start
停止 service httpd stop
service httpd restart

* 非root账户记得提权操作
* 启动后可以通过ps -ef | grep httpd 命令来查看该进程是否存在
* 此时就可以在浏览器中输入ip10.160.129.208 (默认为80端口),如果打不开,说明防火墙拦截了,可以关闭防火墙sudo service firewalld stop

1.2Apache 虚拟主机配置及伪静态操作

1.2.1Apache 虚拟主机配置
  • 虚拟主机:用于配置多域名,比如一个服务器内配置很多网站,保证每个域名访问的内容和源代码是不一样的。

    配置步骤

    • 移到指定目录 cd /etc/httpd/conf

    • 编辑httpd.conf文件 sudo vim httpd.conf

    • 搜索关键字virtaul /virtual 找到 virtual host being defined

    配置如下文件,配置虚拟主机,监听80端口

    <VirtualHost *:80>
            ServerName www.imooc.test
            DocumentRoot /data/www
            #我没有填写<Directory>标签的内容,发现也可以使用
            <Directory "/data/www">
                     Options Indexes FollowSymLinks
                     AllowOverride None
                     Order allow,deny
                     Allow from all
            </Directory>
    </VirtualHost>
    

    DocumentRoot表示路径

    新建该路径 mkdir -p /data/www

    SGkAu.png

    • 保存并重启Apacheservice httpd restart

      扫描二维码关注公众号,回复: 1026706 查看本文章
    • 进入/data/www目录下 ,并新建index.html文件

    • 同时要改变indxl.html的文件权限 sudo chown -R imooc:imooc /data (可以用ll /data/ 命令查看权限变化)

    • 此时可以在index.html中输入一些文字

    • 指定域名的服务器地址

    • Linux:sudo vim /etc/hosts ,然后添加如下信息

      • 10.160.129.208 www.imooc.test
    • Windows:c:\Windows\System32\Drivers\etc 然后添加如下信息
      • 10.160.129.208 www.imooc.test
    • 此时在浏览器输入10.160.129.208 或者www.imooc.test 都可以看到刚才在Index中写入的信息

    • 在填写了内容后,

    • sudo setenforce 1 强制模式,则浏览器无法打开

    • sudo setenforce 0 宽松模式,则浏览器可以打开
    • 如何彻底关闭
      • sudo vim /etc/selinux/config
      • 设置SELINUX即可
      • fKr8z.png
  • 配置多个虚拟主机

    • 就是重复粘贴就好了
    <VirtualHost *:80>
          ServerName www.imooc.test
          DocumentRoot /data/www
          #我没有填写<Directory>标签的内容,发现也可以使用
          <Directory "/data/www">
                   Options Indexes FollowSymLinks
                   AllowOverride None
                   Order allow,deny
                   Allow from all
          </Directory>
    </VirtualHost>
    <VirtualHost *:80>
          ServerName www.imooc2.test
          DocumentRoot /data/www2
          #我没有填写<Directory>标签的内容,发现也可以使用
          <Directory "/data/www2">
              Options Indexes FollowSymLinks
              AllowOverride None
              Order allow,deny
              Allow from all
          </Directory>
    </VirtualHost>
    • 然后在指定文件夹下创建index.html文件就可以l
    • 可是接下来呢?怎么访问呢?依然在host中配置如下吗?可是这样我发现输入www.imooc2.test 返回的是imooc1的内容,有大神知道的话,可以告诉我!!!
    10.160.129.208   www.imooc.test
    10.160.129.208   www.imooc2.test
1.2.2伪静态操作
  • 移到指定目录cd /etc/httpd/modules 里面都是各种模块,其中mod_rewrite.so 是关于伪静态的
  • 移到指定目录cd /etc/httpd/conf ,打开httpd.conf
  • 开启伪静态规则,在httpd.conf中搜索/LoadModule

LoadModule rewrite_module modules/mod_rewrite.so

f1POY.png

  • 重启httpd,这时伪静态的功能已经生效了
  • 然后设置www.imooc.test 等同于 www.imooc.test/1.hmop [随意设置的]
  • 打开httpd.conf,为刚才配置的虚拟主机添加<IfModule> 标签
<VirtualHost *:80>
        ServerName www.imooc.test
        DocumentRoot /data/www
        <Directory "/data/www">
                 Options Indexes FollowSymLinks
                 AllowOverride None
                 Order allow,deny
                 Allow from all
                <IfModule mod_rewrite.c>
                        RewriteEngine On
                        RewriteRule ^(.*).hmop$ index.html
                </IfModule>
        </Directory>
</VirtualHost>
  • 重启httpd
  • 在浏览器中输入www.imooc.test/1.hmop

2.Nginx

2.1Nginx的基本操作

f1LfJ.png

reload 是无缝衔接的,用户不会觉察到,但是restart用户是可以觉察到的

1.Nginx安装
  • 添加CentOS 7 Nginx yum 资源库
    • sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    • sudo yum install nginx p
    • ps -ef | grep nginx 查看命令是否启动
    • sudo service nginx start
2.Nginx虚拟主机配置
  • 浏览器此时输入公网ip,就可以看到nginx的欢迎页面
  • cd /etc/nginx/conf.d
  • sudo cp default.conf imooc.conf
  • sudo vim imooc.conf
  • 如下面实例,还可以配置多域名和多端口[但是我的多端口没有成功,应该是腾讯云没有放开权限,日后再试],另外此时的多域名指向的是同一处资源,即都是index.html
server {
    listen       80;
    listen       9999;
    server_name  www.imooc.test www.imooc2.test;
    location / {
        root   /data/www;
        #之前在Apache配置过了,故直接使用
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

2.2伪静态

  • Nginx的伪静态是默认开启的
  • 配置伪静态rewrite ^(.*)\.sk$ /index.html; 写在location /{} 里面
    • ^ 表示开始,$ 表示结束 ^(.*)\.sk$ 表示以.任意 开始,并且是.sk 结束,\ 是转义字符
server {
    listen       80;
    server_name  www.imooc.test www.imooc2.test;
    location / {
        root   /data/www;
        index  index.html index.htm;
        rewrite ^(.*)\.sk$ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
  • 重启

2.3查看日志

  • cd /etc/nginx
  • sudo vim nginx.conf
    • 具体可以搜索nginx log_format 格式化输出日志
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;
  • 可以到指定的目录下查看 tail -f /var/log/nginx/access.log
  • tips:可以配置虚拟主机的日志
server {
    listen       80;
    server_name  www.imooc.test www.imooc2.test;
    #配置该虚拟主机的日志文件
    access_log  /var/log/nginx/access_imooc.log  imooc;
    location / {
        root   /data/www;
        index  index.html index.htm;
        rewrite ^(.*)\.sk$ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

2.4反向代理和负载均衡

  • 反向代理:输入一个网址,跳转到指定的页面,从而隐藏了后端的服务器。

    有时候只设置proxy_pass 还不行,还需要设置 proxy_set_header Host www.baidu.com

server {
    listen       80;
    server_name  www.imooc.test www.imooc2.test;
    location / {
        root   /data/www;
        index  index.html index.htm;
        #有些这个不需要设置
        proxy_set_header Host www.baidu.com
        #百度的ip
        proxy_pass http://61.135.169.125;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

也可以使用别名.

upstream imooc_hosts{
    server 61.135.169.125:80;
}
server {
    listen       80;
    server_name  www.imooc.test www.imooc2.test;
    location / {
        root   /data/www;
        index  index.html index.htm;
        #有些这个不需要设置
        proxy_set_header Host www.baidu.com
        #百度的ip
        proxy_pass http://imooc_hosts;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
  • 负载均衡,用于服务器分流,至少两台服务器

    即添加两台以上的服务器,并且可以设置权重

upstream imooc_hosts{
    server 61.135.169.125:80 weight=5;
    server 118.25.54.196:80 weight=1;
}
server {
    listen       80;
    server_name  www.imooc.test www.imooc2.test;
    location / {
        root   /data/www;
        index  index.html index.htm;
        proxy_set_header Host www.baidu.com;
        proxy_pass http://imooc_hosts;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

2.5调试nginx

  • 主要用于在出错的时候,返回一些信息
  • 不过暂时用的不多,没有太多了解
server {
    listen       80;
    add_header Content-Type "text/plain;charaset=utf-8";
    return 200 "$http_host";
    server_name  www.imooc.test www.imooc2.test;
    location / { 
        root   /data/www;
        index  index.html index.htm;
        rewrite ^(.*)\.sk$ /index.html;
    }   
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }   

}

猜你喜欢

转载自blog.csdn.net/endlessseaofcrow/article/details/80293268