搭建静态网站

搭建Http静态服务器环境

任务时间:15min ~ 30minhtml

搭建静态网站,首先须要部署环境。下面的步骤,将告诉你们如何在服务器上经过 Nginx 部署 HTTP 静态服务。node

00、安装 Nginx

在 CentOS 上,可直接使用 yum 来安装 Nginxnginx

yum install nginx -y

安装完成后,使用 nginx 命令启动 Nginx:服务器

nginx

此时,访问 http://ip 能够看到 Nginx 的测试页面 [?]app

 

若是没法访问,请重试用 nginx -s reload 命令重启 Nginxtcp

0一、配置静态服务器访问路径

外网用户访问服务器的 Web 服务由 Nginx 提供,Nginx 须要配置静态资源的路径信息才能经过 url 正确访问到服务器上的静态资源。测试

打开 Nginx 的默认配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,将默认的 root /usr/share/nginx/html; 修改成: root /data/www;,以下:网站

示例代码:/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root /data/www; 
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

配置文件将 /data/www/static 做为全部静态资源请求的根路径,如访问: http://<您的域名>/static/index.js,将会去 /data/www/static/ 目录下去查找 index.js。如今咱们须要重启 Nginx 让新的配置生效,如:url

nginx -s reload

重启后,如今咱们应该已经能够使用咱们的静态服务器了,如今让咱们新建一个静态文件,查看服务是否运行正常。spa

首先让咱们在 /data 目录 下建立 www 目录,如:

mkdir -p /data/www

0二、建立第一个静态文件

在 /data/www 目录下建立咱们的第一个静态文件 index.html

示例代码:/data/www/index.html
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>第一个静态文件</title>
</head>
<body>
Hello world!
</body>
</html>输入服务器ip地址便可访问网站
相关文章
相关标签/搜索