Linux nginx+node+supervisor+mysql+redis 环境部署

来源:互联网 发布:贝克汉姆 帅 知乎 编辑:程序博客网 时间:2024/06/09 08:45

环境部署

系统环境 Centos 7.2
架构:nginx + node + supervisor + mysql + redis

添加默认的环境配置信息

yum -y install gcc gcc-c++ autoconf automake makeyum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

一、nginx部署

gcc安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install -y gcc-c++

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

官网下载

  1. 直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html
  2. 使用wget命令下载(推荐)。
wget http://nginx.org/download/nginx-1.12.0.tar.gz

安装

# 解压tar -zxvf nginx-1.12.0.tar.gzcd nginx-1.12.0# 配置./configure# 编译安装makemake install# 查找安装的路径whereis nginx

添加nginx服务

vim /lib/systemd/system/nginx.service

添加如下内容:

[Unit]Description=nginxAfter=network.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=trueRestart=on-failureRestartSec=20s[Install]WantedBy=multi-user.target

设置开机自动启动

systemctl enable nginx.service

nginx配置

添加log目录

mkdir -p /var/log/nginx

修改nginx.conf

vim /usr/local/nginx/conf/nginx.conf

添加如下内容:

#user  nobody;worker_processes  1;error_log  /var/log/nginx/error.log;pid        /var/log/nginx/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    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;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    include /usr/local/nginx/conf/conf.d/*.conf;}

二、mysql部署

配置YUM源

# 下载mysql源安装包wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安装mysql源yum localinstall mysql57-community-release-el7-8.noarch.rpm# 检查mysql源是否安装成功yum repolist enabled | grep "mysql.*-community.*"

安装MySQL

yum -y install mysql-community-server

启动MySQL服务

systemctl start mysqld# 查看MySQL的启动状态systemctl status mysqld

开机启动 — 添加到系统启动

systemctl enable mysqldsystemctl daemon-reload

修改root默认密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

grep 'temporary password' /var/log/mysqld.log# 修改密码mysql -uroot -p# 方法1mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';# 方法2mysql> set password for 'root'@'localhost'=password('MyNewPass4!');

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误

添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'webuser'@'%' IDENTIFIED BY 'web123!' WITH GRANT OPTION;

配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

[mysqld]character_set_server=utf8init_connect='SET NAMES utf8'

重新启动mysql服务,查看数据库默认编码

mysql> show variables like '%character%';

默认配置文件路径

  • 配置文件:/etc/my.cnf
  • 日志文件:/var/log/mysqld.log
  • 服务启动脚本:/usr/lib/systemd/system/mysqld.service
  • socket文件:/var/run/mysqld/mysqld.pid

三、node部署

官网 https://nodejs.org

方法一

# 使用root权限的账号运行curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -# 安装nodejsyum -y install nodejs# 查看nodejs版本node -v`v6.10.2`

方法二

# 获取rpm包wget https://rpm.nodesource.com/pub_6.x/el/6/x86_64/nodejs-6.10.2-2nodesource.el6.x86_64.rpm# rpm安装rpm -ivh nodejs-6.10.2-2nodesource.el6.x86_64.rpm# 查看当前版本号node -v`v6.10.2`

四、redis部署

官网 https://redis.io/

源码安装redis

# 创建db存储目录mkdir -p /data/db# 下载源文件wget http://download.redis.io/releases/redis-3.0.7.tar.gz# 安装tar zxf redis-3.0.7.tar.gzcd redis-3.0.7make && make install// 复制文件mkdir /usr/rediscp redis-server /usr/rediscp redis-benchmark /usr/rediscp redis-cli /usr/rediscp redis.conf /usr/redis

配置文件

修改配置文件 /usr/redis/redis.conf

daemonize yesdir /data/db/

添加服务 —启动配置到系统自启动

vim /lib/systemd/system/redis.service

内容如下:

[Unit]  Description=Redis  After=syslog.target network.target remote-fs.target nss-lookup.target  [Service]  Type=forking  PIDFile=/var/run/redis.pid  ExecStart=/usr/redis/redis-server /usr/redis/redis.conf  ExecReload=/bin/kill -s HUP $MAINPID  ExecStop=/bin/kill -s QUIT $MAINPID  PrivateTmp=true  [Install]  WantedBy=multi-user.target
// 修改配置信息后执行下面命令刷新配置systemctl daemon-reload// 添加到系统开机自启动   --- 添加到supervisor守护进程中启动不需要开机启动systemctl enable redis.service// 启动redissystemctl start redis.service// 停止redissystemctl stop redis.service// 重启redissystemctl restart redis.service

五、supervisor部署

安装pip

wget https://bootstrap.pypa.io/get-pip.pypython get-pip.py

安装supervisor

pip install supervisor// 配置文件echo_supervisord_conf > /etc/supervisord.conf// 新建日志存放目录mkdir -p /data/log

添加守护配置 /etc/supervisord.conf

// 修改nodaemon nodaemon=true// 修改项目配置目录文件[include]files = /etc/supervisord.d/*.conf

添加新的项目配置 /etc/supervisord.d/*.conf

command=node server.js -p 80%(process_num)02dprocess_name=%(program_name)s_%(process_num)02dnumprocs=4numprocs_start=1directory=/data/web   # 这里的项目目录必须存在umask=022startsecs=23autostart=trueautorestart=truestartretries=10redirect_stderr=truestdout_logfile=/var/log/%(program_name)s_%(process_num)02d_normal.logstdout_logfile_maxbytes=50MBstdout_logfile_backups=50stderr_logfile=/var/log/%(program_name)s_%(process_num)02d_error.logloglevel=infouser=root

添加supervisor服务

vim /lib/systemd/system/supervisord.service

添加如下内容:

# supervisord service for sysstemd (CentOS 7.0+)# by ET-CS (https://github.com/ET-CS)[Unit]Description=Supervisor daemon[Service]ExecStart=/usr/bin/supervisordExecStop=/usr/bin/supervisorctl $OPTIONS shutdownExecReload=/usr/bin/supervisorctl $OPTIONS reloadKillMode=processRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target

设置开机启动

systemctl enable supervisord.service

服务重启

# 启动服务systemctl start supervisord.service# 关闭服务systemctl stop supervisord.service# 重启服务systemctl restart supervisord.service
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果x外壳掉漆怎么办 手机壳按键很硬怎么办 棉质白衣服染色怎么办 白棉t恤混洗染色怎么办 包包被衣服染色了怎么办 白色衣服染了菜汁怎么办 一加3t屏幕刺眼怎么办 怀孕吃了好多杏怎么办 门破了个洞怎么办 钢圈轮毂刮花了怎么办 瓷砖用刀子划了怎么办 陶瓷洗手台裂了怎么办 洗车泵水管坏了怎么办 印胶浆里面渗入了发泡浆怎么办? 管子断在水管里怎么办 衣服上的织带缩水怎么办 真丝衣服拔缝了怎么办 顾客说衣服太花怎么办 铝和碱反应变黑怎么办 40度高温多肉怎么办 沾到医用蓝药水怎么办? 裤子弄上泡沫胶怎么办 苍蝇纸粘衣服上怎么办 苍蝇胶沾衣服上怎么办 灯带为什么不亮怎么办 苹果6比屏幕变黄怎么办 雷腾键盘锁了怎么办 自吸泵电机不转怎么办 孕38周胎儿偏小怎么办 被火烧黑的铁怎么办 锅被烟熏黑了怎么办 墙壁被烟熏黑了怎么办 壁纸被烟熏黑了怎么办 空调被烟熏黑了怎么办 牙被烟熏黑了怎么办 尖头鞋把尖折了怎么办 腰椎固定手术钢钉断了怎么办 脚被钢钉扎了怎么办 皮帘子有胶了怎么办 12v插口没有电怎么办 吃了一颗聚乙烯醇怎么办