阿里云首次安装和部署nginx

一、执行yum命令安装依赖php

yum -y install pcre* 
yum -y install openssl*

 

二、下载nginxhtml

 //若是没有安装wget,下载已编译版本
  yum install wget前端

//进入指定目录
cd /usr/local/

//下载nginx 安装包,“1.16.0”是指定的安装版本,能够选择本身须要或者最新的版本
wget http://nginx.org/download/nginx-1.16.0.tar.gz

 三、编译安装vue

//经过tar解压安装包
tar -zxvf zlib-1.16.0.tar.gz

//进入nginx
cd nginx-1.16.0

//执行编译
./configure

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

//编译成功执行
make -j4 && make install

四、nginx测试node

//在nginx可执行命令下目录/usr/local/nginx/sbin执行
./nginx -t

//出现下面结果表示安装成功

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

 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfulc++

 //也能够执行./nginx 启动,而后在浏览器访问此机器的 IP,若是浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功后端

 //部分命令api

 //重启:./nginx -s reload跨域

 //中止:./nginx -s stop

五、配置开机启动-配置文件(若是先部署项目,跳过看第7条)

//进入目录,编辑nginx文件
cd /etc/init.d/
vi nginx
//添加以下,注内容修改PATH字段, 匹配本身的安装路径,若是按这个流程安装应该是同样的

#!/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

//重启, 查看nginx服务是否自动启动.
shutdown -h0 -r
netstat -apn|grep nginx

七、配置部署本身的项目,下面以一个node/vue先后端分离的项目为例:

//找到nginx。conf配置文件
cd /usr/local/nginx/conf/

//编辑文件
vi nginx.conf

//在server对象里改成本身要的端口,默认为80
listen 80;

//一样的配置前端打包地址: root为vue打包后存放在服务器的地址
location / {
    root /data/paulBlog/browseClient/dist;
    index index.html index.htm;
}
//一样的配置后端接口地址:proxy_pass 为后端接口地址

  #解决跨域
  location /api { # 自定义nginx接口前缀
    proxy_pass http://127.0.0.1:3000; # 后台api接口地址
    proxy_redirect default;
    #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

//总体文件以下

//经过命令:qw 保存成功后最好是从新启动下nginx
/usr/local/nginx/sbin/nginx -s reload

八、在阿里云后台配置安全组规制放出对应的端口,就能够经过阿里云提供的IP访问了。须要域名访问的话,一样在阿里云上申请,解析,最后备案后,可经过域名访问,备案挺麻烦的^**^

相关文章
相关标签/搜索