美文网首页我爱编程
CentOS7 node.js nginx MySQ

CentOS7 node.js nginx MySQ

作者: Jerome_e7ec | 来源:发表于2018-02-09 17:53 被阅读0次

    安装nodejs

    官网查看最新版本,二进制官方下载包

    下载

    先进入/usr/src文件夹,这个文件夹通常用来存放软件源代码:

    cd /usr/local/src/
    wget https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.xz
    国内源
    wget https://npm.taobao.org/mirrors/node/v8.9.4/node-v8.9.4-linux-x64.tar.xz
    版本自己替换
    

    解压

    tar -vxf  node-v8.9.4-linux-x64.tar.xz
    mv node-v8.9.4-linux-x64 node // 移动至/node
    

    配置环境变量

    进入 etc 目录下,打开 profile 文件 $ vi /etc/profile 增加以下内容:

    export NODE_HOME=/usr/local/src/node
    export PATH=$NODE_HOME/bin:$PATH
    

    现在执行查看 node 版本:

    $ node -v
    v8.9.4 说明环境变量配置成功。

    如果关闭终端再进去发现 node 环境变量失效,可以在 ~/.bash_profile 增加 export

    $ vi ~/.bash_profile

    在结尾添加以下内容:

    export NODE_HOME=/usr/local/src/node
    export PATH=$NODE_HOME/bin:$PATH
    

    退出系统,再次进入执行 node -vnpm -v,出现版本号则说明没有问题。

    现在已经安装了Node.js,可以开始部署应用程序, 首先要使用Node.js的模块管理器npm安装Express middleware和forever(一个用来确保应用程序启动并且在需要时重启的非常有用的模块),其中g参数是把express安装到NodeJS的lib目录,d参数表示同时安装依赖模块包:

    npm install -gd express-generator forever

    建立测试项目并执行

    在/home文件夹下执行:
    express testapp
    cd testapp
    npm install
    npm start
    

    上面,第一条命令是创建express框架通用项目,第三条命令是安装依赖包,第四条是执行。
    执行:cat package.json

    1.jpg

    第四条命令就相当于执行了node ./bin/www

    2.png

    这样就运行成功了。

    但是当我们关闭终端之后,进程就将结束,现在刚安装的forever就派上用场了,forever可以让进程在终端关闭之后继续运行:

    forever start ./bin/www

    我们可以使用下面命令查看forever运行的程序:

    forever list

    3.png

    现在我们就可以在浏览器中输入:公网IP + :3000,来访问我们的程序。

    如果要修改3000端口,我们可以修改./bin/www文件中关于监听3000端口的字段。

    停止运行:

    forever stop 0 //0代表前面[0],这是当前进程的ID

    停止所有:

    forever stopall

    二、安装Nginx

    HTTP请求是80端口,但是在Linux上非root权限是无法使用1024以下端口的,并且因为安全原因,最好不要使用root权限登录服务器,所以无法直接用node.js程序监听80端口。因此我们需要使用Nginx给node.js做反向代理,将80端口指向应用程序监听的端口(如node.js默认的3000端口)。

    添加Nginx仓库

    yum install epel-release

    下载Nginx

    yum install nginx

    启用nginx服务

    systemctl start nginx

    添加开机启动

    systemctl enable nginx

    修改Nginx配置文件

    vi /etc/nginx/nginx.conf //使用lnpm意见安装,Nginx 目录: /usr/local/nginx/

    添加:

    server {
     listen 80;
     server_name jakexin.top,www.jakexin.top;  #绑定的域名
     location /
     {
     proxy_set_header X-Real-IP  $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host   $http_host;
     proxy_set_header X-NginX-Proxy true;
     proxy_set_header Connection "";
     proxy_http_version 1.1;
     proxy_pass http://127.0.0.1:3000;  #对应该的Nodejs程序端口
     }
     access_log /mnt/log/www/jakexin_access.log; #网站访问日志
    }
    

    测试配置文件是否能够正确运行
    nginx -t

    4.png

    这样就是配置成功

    重启nginx

    service nginx restart

    现在直接在浏览器中输入我们配置的域名就可以访问我们的项目了。

    三、安装MySQL

    查看可用版本

    yum list | grep mysql

    5.jpg

    在centOS 7中不能使用yum -y install mysql mysql-server mysql-devel安装,这样会默认安装mysql的分支mariadb。

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可的MariaDB是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

    正确的安装方法

    众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本。所以我们需要先安装带有当前可用的mysql5系列社区版资源的rpm包

    uname -a命令看看自己的平台版本

    rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
    yum repolist enabled | grep “mysql.-community.“ //查看当前可用资源
    

    从上面的列表可以看出, mysql56-community/x86_64 和 MySQL 5.6 Community Server 可以使用。

    因此,我们就可以直接用yum方式安装了MySQL5.7版本了。

    yum -y install mysql-community-server

    MySQL基础配置

    systemctl enable mysqld //添加到开机启动
    systemctl start mysqld.service //启用进程
    systemctl status mysqld.service
    mysql -uroot -p //设置密码,默认为空
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!’;
    或者:
    [root@centos-linux ~]# mysql_secure_installation
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MySQL to secure it, we'll need the current
    password for the root user.  If you've just installed MySQL, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none): 
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.
    
    You already have a root password set, so you can safely answer 'n'.
    
    Change the root password? [Y/n] y   [设置root用户密码]
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] y  [删除匿名用户]
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] n  [禁止root远程登录]
     ... skipping.
    
    By default, MySQL comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] y  [删除test数据库]
     - Dropping test database...
    ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
     ... Failed!  Not critical, keep moving...
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] y  [刷新权限]
     ... Success!
    
    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    Thanks for using MySQL!
    Cleaning up...
    

    四、操作MySQL

    配置远程连接

    grant all privileges on *.* to 'root'@'%' identified by ‘root' with grant option; //添加授权的用户
    flush privileges; //刷新数据库
    

    检测是否开启3306端口
    netstat -tunlp

    6.png

    看到3306端口被开启之后,我们就可以使用本地客户端远程访问数据库了

    相关文章

      网友评论

        本文标题:CentOS7 node.js nginx MySQ

        本文链接:https://www.haomeiwen.com/subject/kjkqtftx.html