当前位置: 首页 > Linux

在Linux上安装nginx并部署Vue项目

时间:2023-04-06 19:52:14 Linux

部署环境:Ubuntu20.04LTS本地环境:Windows101.用于下载nginx的版本为:nginx-1.19.4.tar.gz官网地址:http://nginx.org/en/download.html百度网盘:链接:https://pan.baidu.com/s/1RNdH...提取码:73492、安装nginx1。连接服务器可以使用xshell工具连接服务器,或者命令行工具也行(gitbash,cmd...)。使用命令行工具执行以下命令连接服务器,root是用户名,8.129.38.87是你的服务器公网IP,然后输入密码。sshroot@8.129.38.87-p222.配置nginx安装所需环境安装两个依赖库sudoapt-getinstallautoconfautomakesudoapt-getinstalllibpcre3libpcre3-devinstallzliblibrarysudoapt-getinstallopensslsudoapt-get安装libssl-dev3。将nginx压缩包上传到服务器并解压。在本地打开刚才下载的nginx压缩包所在目录,打开命令窗口,使用scp命令上传资源。我这里会上传到服务器的/usr/local目录下。会让你输入服务器登录密码scp./nginx-1.19.4.tar.gzroot@8.129.38.87:/usr/local切换到服务器命令窗口,可以看到刚刚上传的压缩包,执行解压命令解压安装包cd/usr/localtar-zxvfnginx-1.19.4.tar.gz4。解压后执行默认配置,进入解压后的文件夹,执行cdnginx-1.19.4然后进行配置。推荐使用默认配置,直接执行/configure即可,如下图:5.编译安装nginx在当前目录/usr/local/nginx-1.19.4下编译。键入make并按Enter。如果出现编译错误,请检查前面4次安装是否正确。cd/usr/local/nginx-1.19.4make编译成功后,即可安装。输入以下命令:makeinstall6。启动nginx,进入/usr/local/nginx/sbin目录,输入./nginx启动nginxcd/usr/local/nginx/sbin./nginx如果启动报错:nginx:[emerg]bind()to0.0.0.0:80failed(98:Addressalreadyinuse),说明80端口被占用,使用如下命令:fuser-k80/tcpshutdownnginxcd/usr/local/nginx/sbin./nginx-squit#或者./nginx-sstoprestartnginxcd/usr/local/nginx/sbin./nginx-sreload查看nginx进程psaux|grepnginx7.在rc.local中设置nginx开机启动,添加启动代码vim/etc/rc.local#编辑文件,在文件底部添加/usr/local/nginx/sbin/nginx这一行,保存退出3.部署Vue。js项目1.打开vue项目,执行打包贴出我的vue项目地址:https://github.com/luxiancan/...npmrunbuild完成后,进入dist文件夹,选择所有打包结果,打包压缩成dist.zip2。将打包结果的压缩包上传到服务器scp./dist.ziproot@8.129.38.87:/usr/local/nginx/html3。在服务器上解压dist.zip压缩包。删除/local/nginx/html目录下的index.htmlcd/usr/local/nginx/htmlrm-rfindex.htmlunzipdist.zip执行后如图4修改nginx配置文件,进入cd/usr/local/nginx/conf目录下可以修改nginx的配置文件:cd/usr/local/nginx/confvimnginx.conf按键盘i编辑,编辑完按Esc退出编辑,然后输入:wq保存并退出贴一个完整版,其中序号表示注释,主要修改服务器nginx.conf#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;的内容;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{includemime.types;default_type应用程序/八位字节流;#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'#'$status$body_bytes_sent"$http_referer"'#'"$http_user_agent""$http_x_forwarded_for"';#access_log日志/access.logmain;发送文件;#tcp_nopush上;#keepalive_timeout0;keepalive_timeout65;#gzip上;server{#1.监听2001端口listen2001;#2.这是你部署的IP,你服务器的公网IPserver_name8.129.38.87;#3.这里配置前端包文件的映射路径root/usr/local/nginx/html;#charsetkoi8-r;#access_log日志/host.access.logmain;罗阳离子/{根html;try_files$uri$uri//index.html;indexindex.htmlindex.htm;}#4.解决跨域问题,在proxy_pass后面写上需要代理的后端地址#把所有http://8.129.38.87:2001/front请求,转发到http://edufront.lagou.com/frontlocation/front{proxy_passhttp://edufront.lagou.com;}#5.同样可以配置多个一个location,关于nginx代理的配置请上网搜索location/boss{proxy_passhttp://eduboss.lagou.com;#proxy_redirect关闭;#proxy_set_header主机$host;#proxy_set_headerX-Real-IP$remote_addr;#proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}#error_page404/404.html;#将服务器错误页面重定向到静态页面/50x.html#error_page500502503504/50x.html;location=/50x.html{根html;}#将PHP脚本代理到Apache监听127.0.0.1:80##location~\.php${#proxy_passhttp://127.0.0.1;#}#将PHP脚本传递给侦听127.0.0.1:9000的FastCGI服务器##location~\.php${#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_indexindex.php;#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#包含fastcgi_params;#}#拒绝访问.htaccess文件,如果Apache的文档根#与nginx的根一致##location~/\.ht{#denyall;#}}#另一个混合使用基于IP、名称和端口的配置的虚拟主机##server{#listen8000;#听某个名字:8080;#server_namesomename别名another.alias;#位置/{#根html;#索引index.htmlindex.htm;#}#}#HTTPS服务器##server{#listen443ssl;#server_name本地主机;#ssl_certificatecert.pem;#ssl_certificate_keycert.key;#ssl_session_cache共享:SSL:1m;#ssl_session_timeout5m;#ssl_ciphers高:!aNULL:!MD5;#ssl_prefer_server_ciphers开启;#位置/{#根html;#索引index.htmlindex.htm;#}#}}另外,如果需要配置多个vue项目,只需要添加多个服务器配置,将nginx下面的注释取消,然后修改配置即可:5.在服务器上开放2001端口管理控制台。2001端口就是我们刚刚在nginx.conf配置文件中监听的端口。登录云服务器管理控制台,添加安全组规则6.重启nginx,进入/usr/local/nginx/sbin/目录,执行重启命令使配置生效:cd/usr/local/nginx/sbin/./nginx-sreload查看nginx进程:psaux|grepnginx7。访问在浏览器中输入您网站的网址http://8.129.38.87:2001/即可访问您的网站。这是我的服务器IP加上刚刚配置的端口。记得修改成自己的~