Nginx 的安装与常见命令

安装

windows 本地安装 nginx

进入官网下载,解压任意目录。
cmd 此处打开,start nginx.exe。如果报错说明80端口占用,进nginx.conf 修改80到任意,正常情况下,在“任务管理器”中会看到“nginx.exe”进程,打开浏览器访问127.0.0.1:xx 有 nginx 的欢迎界面,则说明安装成功
项目目录地址,推荐与ngnix同盘符

root         /root/xx/dist;
/为 与 nginx同盘符的根目录

centosnginx

https://www.linuxidc.com/Linux/2016-09/134907.htm

sshecsping www.baidu.com 检查网络。如果 ping 不通需要修改 DNS 配置

`vim /etc/resolv.conf`
`i` 键编辑
`#` 注释掉上边
新增 `nameserver 114.114.114.114`
`esc :wq `保存推出
  1. yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    如果 yum 安装出错
    https://opsx.alibaba.com/mirror centos 的帮助 centos7 ,epel 的帮助走一遍
    ps 目前页面已经打不开了,yum 装不了的自行百度。。

  2. wget -c -P /usr/src https://nchc.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    如果没有 wget :yum install -y wget
    或手动下载:http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz,将下载好的压缩包通过 sftp 工具放入 /usr/src

  3. 解压
    tar -xvf /usr/src/pcre-8.35.tar.gz -C /usr/src
    cd /usr/src/pcre-8.35
    ./configure
    make && make install

  4. wget -c -P /usr/src http://nginx.org/download/nginx-1.16.1.tar.gz
    或手动下载 http://nginx.org/en/download.html,将下载好的压缩包通过 sftp 工具放入 /usr/src

  5. 解压:
    tar -xvf /usr/src/nginx-1.16.1.tar.gz -C /usr/src
    cd /usr/src/nginx-1.16.1 进文件夹
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module (增加 SSL 支持)
    make && make install

  6. 安装完成后进入 /usr/loaclnginx 文件夹 ,或者 nginx -v 有版本信息说明安装成功
    /usr/local/nginx/conf/nginx.confnginx 的配置文件

    如果没有配置全局变量,使用 nginx 命令的时候,必须进入 /usr/local/nginx/sbin 目录
    /usr/local/nginx/sbin/ + ng 命令,例:任意目录下 /usr/local/nginx/sbin/nginx -s reload

  7. centos nginx 系统环境变量
    vim /etc/profile 在文件末尾添加 export PATH=$PATH:/usr/local/nginx/sbin
    重新加载环境 :source /etc/profile
    此时应该可以在任何地方执行 nginx 命令

  8. 查看 nginx 安装位置:whereis nginx


常见 nginx 命令

注意 nginx 命令成功无提示

1.查看版本
  ./nginx -v
2.启动 nginx
  ./nginx
3.关闭 nginx
  ./nginx -s stop
4.重载 nginx
  ./nginx -s reload

ps -ef | grep nginx 查看 nignx 进程状态

root      2271 23946  0 16:05 pts/1    00:00:00 grep --color=auto nginx
root     25805     1  0 14:01 ?        00:00:00 nginx: master process /usr/sbinnginx -c /etc/nginx/nginx.conf
nginx    25806 25805  0 14:01 ?        00:00:00 nginx: worker process

nginx 常见报错

nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

解决:

  1. 先试试
    nginx -c /usr/local/nginx/conf/nginx.conf

  2. 如果不行的话,杀掉 nginx 进程,重新跑
    killall -9 nginx
    nginx -c /usr/local/nginx/conf/nginx.conf
    /usr/local/nginx/sbin/nginx


配置 nginx.conf

  1. 目录: /usr/local/nginx/conf/nginx.conf
  2. 文件结构分析
    conf / nginx.conf 是 nginx 的默认配置文件。可以使用 nginx -c 指定你的配置文件,也可以通过 include 将配置分成若干在导入到 nginx.conf 避免臃肿难维护
    配置由三部分组成
1.全局块:从配置文件开始到 events 块之间的配置,针对服务器整体运行的一些配置
  worker_processes:并发处理量,也会受硬件/软件的限制
2.events 块:nginx 服务器与用户网络连接配置
  worker_connections 1024:支持最大连接数

3.http 块:配置最频繁的部分,分为 http 全局块,和 server 块
  1.http 全局块
    文件引入
    MIME-TYPE
    日志
    超时时间
  2. server 块:与虚拟主机相关
    1.全局 server
      监听端口号
      主机名称
    2.location 块
  1.反向代理
  2.负载均衡
  3.动静分离
  4.高可用
# 如果不写这个会报 403 没权限的错误,默认 nobody
user root;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name _ localhost;
	    root         /root/deQing/dist;
	    index index.html;
	    include /usr/local/nginx/*.conf;
        # nginx 反向代理的配置
        location ^~/zhengyuan/ {
            proxy_pass http://115.231.247.222:8055/;
            break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


抽取 server 为单独文件,统一引入
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf; // 这里正则匹配一个目录下所有 .conf 文件,即每个 server

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
    



nginx配置常见问题

  1. 问题: 本地项目上传之后, 路由重定向 跳转 404 页面
    在 /usr/local/nginx/conf/nginx.conf 中修改如下配置
server {
  listen 10002;
  server_name localhost;
  location / {
    root 前端 dist 路径地址;
    try_files $uri $uri/ /index.html;	# 解决单页 App 刷新页面或路由重定向跳转 404
    index index.html;
  }
}
  1. 本地开发反向代理没问题,放在服务器上没数据
    在 nginx.conf 中添加反向代理
...
	
server {
  listen 10002;
  server_name localhost;
  location / {
    root zmh_react_project;
    try_files $uri /index.html; #解决路由重定向跳转 404 页面配置
    index index.html;
  }
  # 反向代理
  location /marketing { 
    proxy_pass https://resource.smartisan.com/marketing; 
  }
  location /product {
    proxy_pass https://www.smartisan.com/product;
  }
}
  1. 多项目/多端口 配置
    解决: 在nginx配置文件中添加多个 server 配置, 但是要注意项目文件夹要和 nginx /html 目录同级

    推荐目录结构
    nginx
    	html
    	vue-project
    	react-project
    

    .conf

    http: {
    	server: {}
    	server: {}
    	server: {}
    }
    

  1. ssh 连远程服务器异常
    保证 git 协议 中的公钥和私钥已配置
    配置完成之后重启 示例
    以上都不行, 重装镜像
多页面应用配置
location ^~ /index {
  try_files $uri $uri/ /index.html;
}
location ^~ /registry {
  try_files $uri $uri/ /registry.html;
}
location ^~ /login {
  try_files $uri $uri/ /login.html;
}
location ^~ /about {
  try_files $uri $uri/ /about.html;
}

nginx 相关技术文档

Nginx负载均衡配置实战 http://www.linuxidc.com/Linux/2014-12/110036.htm

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm


ceontos 装 mongodb

通过yum安装的.仅限 64位 centos

  1. 创建仓库文件
    vi /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

esc :wq 保存退出

  1. yum 安装
    yum install -y mongodb-org
  2. 修改配置文件
    vi /etc/mongod.conf
    找到 bindIp 默认 127.0.0.1只限于本机连接,修改为 0.0.0.0
  3. 测试安装
  • 启动 service mongod start
  • 停止 service mongod stop
  • 重启 service mongod restart
  • 进入环境:mongo
  • 查看数据库:show dbs
  • 查看版本:db.version()
  • 常用命令帮助:db.help()
  1. 开机启动:chkconfig mongod on 或者 systemctl enable mongod.service
  2. 查看日志:cat /var/log/mongodb/mongod.log
    最后一句输出 waiting for connections on port <port> 是mongodb 的端口
  3. 删除:yum erase $(rpm -qa | grep mongodb-org)
  4. 移除数据库文件和日志文件: rm -r /var/log/mongodb rm -r /var/lib/mongo
    MongoDB默认将数据文件存储在/var/lib/mongo目录,默认日志文件在/var/log/mongodb中。如果要修改,可以在 /etc/mongod.conf 配置中指定备用日志和数据文件目录。
    修改配置文件最好重启 mongodb 生效
    本教程是参考官网的安装说明安装的,英语水平有限,只能靠自己理解整理的部分重点.英语好的同学,可以自己查看官网安装说明
    https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/

centos 装 nodejs
1. wget -P /usr/src https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz 
    -P 后边是指定的目录 这里是 usr/src
	或者去https://nodejs.org/en/download/找最新的地址
	下载完成后 cd 到对应目录准备解压
3. xz -d node-v9.3.0-linux-x64.tar.xz
4. tar -xf node-v9.3.0-linux-x64.tar
	这里的 xz -d 可能是解压 .tar.xz
	tar -xf 解压 .tar 具体我也没了解过
	解压后我把文件夹重命名为 nodejs 并且放在了 /usr/local 目录下
5. 设置环境变量 (软链)
ln -s /usr/local/nodejs/bin/node /usr/bin/node
ln -s /usr/local/nodejs/bin/npm /usr/bin/npm
ln -s /usr/local/nodejs/bin/npm /usr/bin/npx

接下来就是 node -v npm -v

npm 全局安装的比如 nodemon 安装后还是没有环境变量

这个问题就是 /etc/profile 添加环境变量后 source /etc/profile 在终端内有效,但是关闭终端重新打开一个就失效,解决办法:

第一种: 在 /root/.bashrc 中添加环境变量 export PATH=$PATH:/usr/local/nodejs/bin
第二种:在 /etc/profile 添加环境变量 在 /root/.bashrc 添加 source /etc/profile
但是之前安装的 nginx 只是在 /etc/profile 添加 然后 source /etc/profile 后就一直有效

VScode 远程免密

插件自行百度 这里只讲免密登录

  1. 查看本地电脑的 ssh key 如果没有就生成一个
  2. 连接 centos 找到 /root/.ssh/authorized_keys 这个文件
  3. vim /root/.ssh/authorized_keys 把本地的 ssh key 复制到这个文件里 保存
  4. 下次登录直接就可以了不用每次都输密码

部署前端项目

vue 项目为例

  1. 本地项目根目录下终端执行 npm run build,得到打包文件夹(dist)

  2. 连接服务器,在 nginx 下的 config/nginx.conf 中配置项目端口,项目根目录。如果不会自行百度 nginx 配置

  3. 通过 sftp 软件放置在服务器 ngnix 配置文件的跟目录下

  4. 如果修改过 nginx.conf 需要服务器重启 nginx : nginx -s reload 如果提示 nginx 没这个命令自行百度 nginx 环境变量

  5. 如果网速过慢或者系统不稳定可以先压缩 dist 然后在服务器上解压 unzip dist.zip


远程开发杂记
  1. 服务器
    阿里云 百度云 新浪云 腾讯云
    主机: 不建议
阿里云服务器 ECS node 环境搭建 流程
  1. 购买一个云服务器 公共镜像系统: Centos 64 7.x
  2. 购买域名,进行备案 || 云服务器提供的 ip地址(公有)
  3. 记录自己的ip
  4. 远程连接
    1. 阿里云服务器网页自带的 , 输入远程连接密码
    2. 终端 ssh root@公网IP

如果报错

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:AsIAPiYK8s+4gu6of4Xui8yjWCQ1lqltMow9iPvD85U.
Please contact your system administrator.
Add correct host key in /c/Users/Pc/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /c/Users/Pc/.ssh/known_hosts:1
ECDSA host key for 59.110.226.77 has changed and you have requested strict checking.
Host key verification failed.

解决方案: rm -rf ~/.ssh/known_hosts

  1. 安装node

如何从EPEL库安装Node.js

另一个有效且简单的方法来安装Node.js就是从官方库。这同样确保您可以访问到EPEL库,

你可以通过运行以下命令。

sudo yum install epel-release

现在可以使用yum命令安装Node.js了。

sudo yum install nodejs:装 node

因为在开发过程中我需要管理节点包,我还要安装新公共管理的软件包管理器,

使用以下命令。

sudo yum install npm:装 npm

whereis node:node 位置

  1. 配置安全组

    1. 端口范围 1/60000
    2. 授权对象:0.0.0.0/0:前四位ip后为端口。这么设置谁都可以访问
  2. 书写代码来测试一下 node 使用

    1. 先使用express简单搭建一个服务器, 暴露几个接口, 本地测试, 测试通过在去连接远程
  3. 连接远程服务器, 上传本地代码(本地文件) xftp 5

    1. 安装时我们选择 : 选择第一个 : 商用 添加注册码: 101210-450789-147200

    2. 新建一个会话 连接远程服务器

左上加号,名字:阿里云,
主机:公网 ip
协议:SFTP
端口:22
下边使用身份验证代理选上
用户名:root
密码:_a
点击连接,然出安全警告,点击接受并保存,可以保存密码
新窗口右边是云主机,可以点击...查看
在...同级新建一个 demo 文件夹,上传本地文件
  1. 连接之后就可以看到我们 远程服务器的根目录 /root

  2. 创建一个目录

  3. 将本地代码传输到 远程服务器目录(不要上传node_modules等文件)

  4. 在远程服务器目录 安装依赖 npm i

  5. 启动项目

11.添加负载均衡 pm2 https://www.cnblogs.com/lxg0/p/7771229.html 永久挂起服务

npm i pm2 -g 全局安装pm2

pm2 start app.js  后台挂起服务

pm2 list  查看后台挂起所有服务

pm2 stop id  根据服务id 停止当前服务 

pm2 delete id 根据服务 id 删除当前服务 

pm2 restart id 重启服务 

  1. 安装 mongodb 数据库 http://www.cnblogs.com/web424/p/6928992.html
    1. vim命令基本使用
      1. vim a.txt
      2. 先按键盘的 I
      3. 写入内容 (shift + ins)
      4. 退出 先按 ESC 键
      5. 再打 : wq 会出即可保存并退出
    2. 注意点: 防火墙忽略

简介
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
Packages包说明
MongoDB官方源中包含以下几个依赖包:
mongodb-org: MongoDB元数据包,安装时自动安装下面四个组件包:
1.mongodb-org-server: 包含MongoDB守护进程和相关的配置和初始化脚本。
2.mongodb-org-mongos: 包含mongos的守护进程。
3.mongodb-org-shell: 包含mongo shell。
4.mongodb-org-tools: 包含MongoDB的工具: mongoimport, bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop。

安装步骤

1.配置MongoDB的yum源

创建yum源文件:
vim /etc/yum.repos.d/mongodb-org-3.4.repo
添加以下内容:
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

这里可以修改 gpgcheck=0, 省去gpg验证

安装之前先更新所有包 :yum update (可选操作)

2.安装MongoDB
安装命令:
yum -y install mongodb-org

img

安装完成后

查看mongo安装位置 whereis mongod

查看修改配置文件 : vim /etc/mongod.conf

3.启动MongoDB
启动mongodb :systemctl start mongod.service
停止mongodb :systemctl stop mongod.service

查到mongodb的状态:systemctl status mongod.service

img

4.外网访问需要关闭防火墙:

5.设置开机启动

img

6.启动Mongo shell

查看数据库:show dbs

img

7.设置mongodb远程访问:

img

重启mongodb:systemctl restart mongod.service

本地数据库同步远程云服务器数据库

mongo 公网ip

$ mongo 59.110.226.77





云服务器有很多,流行的有阿里,腾讯,还有上海的美城

  1. 阿里云(推荐)六个月:
  2. 腾讯云
  3. 百度云
  4. 新浪云

系统镜像
centOS:新的
windowServer:老的不推荐
Ubuntu:linux的 py同学用


  1. 购买一个云服务器
  2. 购买域名进行备案 || 云服务器的提供的公网 IP
  3. 公共镜像系统 content

LAMP:PHP环境搭建
安全组:解决 IP和的问题,如果没有只能 localhost:8000
端口范围:1/60000
授权对象:允许谁 0.0.0.0/0全部允许

模拟数据:mark,json

Nginx:静态服务器 / 反向代理
普通JS文件里的反向代理不能用,之前通过 webpack 反向代理。打包后 webpack没有了,所以配置也没有了。需要 Nginx 反向代理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值