Build a static website
Build Http static server environment
Task time: 15min ~ 30min
To build a static website, you first need to deploy the environment. The following steps will show you how to deploy HTTP static services through Nginx on the server.
00, install Nginx
On CentOS, can be used directly yum
to install Nginx
yum install nginx -y
After installation is complete, use the nginx
command to start Nginx:
nginx
At this point, you can see the Nginx test page by visiting http:// ip[?]
If you can not access, please retry
nginx -s reload
command to restart the Nginx
01. Configure static server access path
The web service for external users to access the server is provided by Nginx. Nginx needs to configure the path information of the static resource to correctly access the static resource on the server through the URL.
Open the default configuration file Nginx /etc/nginx/nginx.conf , modify Nginx configuration, the default root /usr/share/nginx/html;
amended as follows: root /data/www;
as follows:
Sample code: /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 { } } }
The configuration file uses /data/www/static as the root path of all static resource requests. If you visit:, http://<您的域名>/static/index.js
it will go to the /data/www/static/ directory to search index.js
. Now we need to restart Nginx to make the new configuration take effect, such as:
nginx -s reload
After restarting, now we should be able to use our static server, now let us create a new static file to see if the service is running normally.
Let us / data create directory www
directory, such as:
mkdir -p /data/www
02, create the first static file
Create our first static file index.html in the /data/www directory
Sample code: /data/www/index.html
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>第一个静态文件</title></head><body>Hello world!</body></html>
If nothing is displayed, please refresh the browser page
completed experiment
Congratulations! You have successfully completed the experimental task of setting up an Http static server environment.
Reprinted at: https://www.cnblogs.com/xiaochina/p/7074156.html