nginx

1、执行yum命令安装依赖

yum -y install pcre*

yum -y install openssl*

2、下载nginx

复制代码
 //如果没有安装wget,下载已编译版本
  yum install wget

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

//下载nginx 安装包,“1.16.0”是指定的安装版本,可以选择自己需要或者最新的版本
wget http://nginx.org/download/nginx-1.16.0.tar.gz
复制代码
3、编译安装

复制代码
//通过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
复制代码
4、nginx测试

复制代码
//在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

//也可以执行./nginx 启动,然后在浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功

//部分命令

//重启:./nginx -s reload

//停止:./nginx -s stop

复制代码
5、配置开机启动-配置文件(如果先部署项目,跳过看第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

复制代码
6、配置开机启动-启动

复制代码
//设置执行权限
chmod a+x /etc/init.d/nginx

//注册成服务
chkconfig --add nginx

//重启, 查看nginx服务是否自动启动.
shutdown -h0 -r
netstat -apn|grep nginx
复制代码
7、配置部署自己的项目,下面以一个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
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值