美文网首页Linux基础知识
第四章:Web Server

第四章:Web Server

作者: fxqp1043 | 来源:发表于2019-04-04 14:00 被阅读0次

Apache

Apache基本操作

  • 安装 yum install httpd(centos)
  • 启动 service httpd start
  • 停止 service httpd stop

注意防火墙(firewalld)

(操作演示:)

捕获.PNG

(访问服务器80端口)

80_test.PNG

Apache的虚拟主机配置和伪静态操作

虚拟主机配置
  • 编辑配置文件/etc/httpd/conf/httpd.conf
cd /etc/httpd/conf/httpd.conf
vim httpd.conf

# virtual host being defined.
#
<VirtualHost *:80>
        ServerName www.fxqp1202.com
        DocumentRoot /data/www
        <Directory>
                Options Indexes FollowSymLinks
                AllowOverride none
                Require all granted
        </Directory>
</VirtualHost>
#
  • 重启httpd服务
    service httpd restart
  • 创建DocumentRoot
    mkdir -p /data/www
  • 将目录所有者设置为其它非root用户
    sudo chown -R(递归) alice:alice /data
  • 指定域名所对应的服务器
    vim /etc/hosts
(如果是公网,需要将自己购买的域名先解析到自己的服务器,这样设置一下字段是才会有用,否则公网DNS查询时,查询不到ip。)
39.98.164.240 www.fxqp1202.com
39.98.164.240 www.fxqp1202.xin
(通过域名来区分不同的虚机)

(阿里云解析DNS需要实名认证,否则会产生异常。)

临时设置强制和宽松模式( SELinux)
setenforce 1(强制模式)
setenforce 0(宽松模式)
直接SELinux设置
vim /etc/selinux/config

SELINUX=disabled
伪静态操作

(比如php写的文件,访问的时候应该是php结尾,但是,使用伪静态,可以以html结尾,有利于SEO优化。)

  • 查看一下伪静态相关的module文件
cd /etc/httpd/modules

与伪静态相关的module文件:mod_rewrite.so
  • 配置文件中加载伪静态模块
vim /etc/httpd/conf/httpd.conf

/LoadModule(查找到相应的位置)

# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule rewrite_module modules/mod_rewrite.so
  • 设置具体的伪静态规则
<VirtualHost *:80>
        ServerName www.fxqp1202.com
        DocumentRoot /data/www
        <Directory>
                Options Indexes FollowSymLinks
                AllowOverride none
                Require all granted
                <IfModule mod_rewrite.c>
                    RewriteEngine  on
                    RewrireRule ^(.*).htmp$ index.html
                <IfModule>
        </Directory>
</VirtualHost>
  • 重启httpd
  • (所有的.htmp文件都会重定向到index.html)
  • 访问

Nginx


nginx的基本操作

  • 安装
    • 添加Centos7 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 -y nginx
    
  • 启动
service nginx start

[root@fxqp1202 ~]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@fxqp1202 ~]# ps -ef | grep nginx
root      3402     1  0 21:19 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     3403  3402  0 21:19 ?        00:00:00 nginx: worker process
root      3405  3232  0 21:19 pts/0    00:00:00 grep --color=auto nginx
[root@fxqp1202 ~]# 
启动.PNG
  • 停止(stop)
  • 重启(restart)
  • 重载
    (nginx服务器重启过程中,用户无法访问。)
    (而重载是无缝迁移。)
service nginx reload
  • 查看配置文件
[root@fxqp1202 ~] cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  • 看看根目录
    (cat /usr/share/nginx/html/index.html)
    (这个就是欢迎页面的内容!)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

虚拟主机配置

  • 首先复制一份配置文件
    (cp default.conf fxqp1202.conf)
server {
    listen       80;
    # listen 8000;(可以监听多个端口)
    server_name  www.fxqp1202.com;
    # server_name  www.fxqp1202.com www.imooc.test;(多域名)
    # 域名需要设置DNS解析,(Host文件)
    location / {
        root   /data/www;
        index  index.html index.htm;
    }
}
  • 重载
  • 访问

Nginx伪静态的实现

相关文章

网友评论

    本文标题:第四章:Web Server

    本文链接:https://www.haomeiwen.com/subject/kqbfiqtx.html