搭建靜態網站

搭建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地址便可訪問網站
相關文章
相關標籤/搜索