阿里云安装nginx和部署vue项目

2018-11-28  本文已影响0人  呆木的小蝎


安装依赖

yum -y install pcre*

yum -y install openssl*

下载nginx

cd /usr/local/

执行下面下载

wget http://nginx.org/download/nginx-1.12.2.tar.gz

# 如果没有安装wget

# 下载已编译版本

$ yum install wget


编译安装

cd nginx-1.12.2

./configure

安装报错误的话比如:“C compiler cc is not found”,这个就是缺少编译环境,安装一下就可以了 
yum -y install gcc make gcc-c++ openssl-devel wget

编译成功后执行下面

make -j4 && make install

nginx测试

运行下面命令会出现两个结果

/usr/local/nginx/sbin ./nginx -t

# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

配置开机自启动

进入目录

cd /etc/init.d/

如果没有nginx文件执行下面添加自启动内容

vi nginx

插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径

#!/bin/bash

# Startup script for the nginx Web Server

# chkconfig: - 85 15

# description: nginx is a World Wide Web server. It is used to serve

# HTML files and CGI.

# processname: nginx

# pidfile: /usr/local/nginx/logs/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

NGINX_HOME=/usr/local/nginx/sbin

NGINX_CONF=/usr/local/nginx/conf

PHP_HOME=/usr/local/php-fcgi/bin

if [ ! -f "$NGINX_HOME/nginx" ]

then

    echo "nginxserver startup: cannot start"

    exit

fi

case "$1" in

    'start')

        $PHP_HOME/spawn-fcgi -a 127.0.0.1 -p 10080 -C 20 -u nobody -f $PHP_HOME/php-cgi

        $NGINX_HOME/nginx -c $NGINX_CONF/nginx.conf

        echo "nginx start successful"

        ;;

    'stop')

        killall -TERM php-cgi

        killall -TERM nginx

        ;;

esac

设置执行权限

chmod  a+x  /etc/init.d/nginx

注册成服务

chkconfig --add nginx

设置开机启动

chkconfig nginx on

重启, 查看nginx服务是否自动启动.

shutdown -h0 -r

netstat -apn|grep nginx

运维

# 启动

/usr/local/nginx/sbin/nginx

# 重启

/usr/local/nginx/sbin/nginx -s reload

# 关闭进程

/usr/local/nginx/sbin/nginx -s stop

# 平滑关闭nginx

/usr/local/nginx/sbin/nginx -s quit

# 查看nginx的安装状态,

/usr/local/nginx/sbin/nginx -V

nginx卸载

如果通过yum安装,使用下面命令安装。

yum remove nginx

编译安装,删除/usr/local/nginx目录即可

如果配置了自启动脚本,也需要删除。

配置部署VUE打包后的项目

先找到nginx.conf文件

cd /usr/local/nginx/conf/

打开nginx.conf文件进行编写修改

vi nginx.conf

然后把它用编辑器的方式打开,打开之后找到这里的sever的listen就是你的端口号,默认的是80端口,你可以根据自己没有被占用的端口进行改写,改写完成之后保存然后打开你的localhost:你改写的端口号就OK了

通过命令:qw 保存成功后最好是重新启动下nginx

/usr/local/nginx/sbin/nginx -s reload

服务器的已经准备好了,现在只要把自己的vue项目通过命令 npm run build 编译生成一个dist文件夹 这就是我们要的vue打包后的项目

然后打开这个dist文件夹把里面的内容复制下来里面会有两个文件一个是index.html是主目录还有一个是static文件夹

然后进入服务器nginx目录下的html 把目录下的文件删除后,把刚刚生成的vue文件项目文件复制上传到html目录下可以同上传工具 Xftp 6 把文件上传到目录中

cd /usr/local/nginx/html/

到这里配置就已经结束了

阿里云添加安全组规则

添加安全组规则主要是把端口开放出来,把需要访问的端口添加进去

上一篇下一篇

猜你喜欢

热点阅读