Ubuntu安装nginx并部署静态网站


学习内容:

  1. 安装nginx

1.1进入终端输入命令:

apt-get install nginx

(权限不够,可以加sudo操作)

1.2当安装结束时输入

nginx -v

查看版本 如下图所示。

如果没有重新试一下。大概率都会成功的

 2.安装成功后可以在浏览器输入虚拟机ip:80就会出现此页面说明安装成功。如下图所示

 

之后呢就可以试试把自己的前端代码放上来了。


nginx部署前端项目:

注意!:nginx部署的是静态页面,静态请求。例如动态请求,比如servlet,.jsp文件是无法部署到nginx。

下面我交大家如何部署自己的前端页面在nginx上

1:首先呢创建一个目录dist来存放前端项目。这里我是把windows里的文件放入虚拟机里

  • 同样我们要把dist目录放到虚拟机的 /etc/nginx 下。
  • 接下来重点!我们要配置一下我们的nginx.conf文件。
  • 我亲测用apt-get install nginx命令下载的nginx中,nginx.conf是少了点东西的。
  • 这里我给一个nginx.conf的模板,直接复制覆盖掉原先的nginx.conf
  • #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;

        #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  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }

            #error_page  404              /404.html;

            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}

            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

    }

  • 复制完成后  在末尾复制server这段出来,并且把#号去掉,然后做一些修改。
  • 注意末尾的大括号啊!粘贴server模块要放在在http模块的大括号里,注意一下!
  • 修改完成后大概是这样子,图如下。修改完后看看你修改的server模块是否在http模块下不然测试的时候会报错。
  • 确认无误后在终端输入
    nginx -t

    来测试是否配置完成。

  • 出现successful说明配置成功。

  • 每次配置一次,都需要重启一下nginx服务

     
    service nginx reload

     

    service nginx start

 最后输入你在网页上nginx.conf文件配置的你的ip:端口号就会出现你放上去的前端项目。

 

 


 


版权声明:本文为qq_50930205原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。