CentOS7 + node.js + nginx + MySQL搭建服务器全过程

前言

最近项目要求,需要一台服务器,阿里和腾讯都不错,我选择了腾讯云,系统为CentOS 7(linux)。记录搭建服务器的过程,方便以后再次搭建是借鉴和给第一次搭建服务器的朋友借鉴之用。

工具

安装git

执行:

  sudo yum install git

安装nodejs

查看最新版本

下载

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

  cd /usr/local/src/  wget https://nodejs.org/dist/v4.6.0/node-v4.6.0.tar.gz

版本自己替换

解压

  tar zxvf node-v4.6.0.tar.gz

编译安装

  cd node-v4.6.0/  ./configure // 执行 Node.js 安装包自带的脚本,修改相关的系统配置文件

发现报错了,提示系统中没有安装C编译器,接下来先安装C编译器

安装gcc

  yum install gcc

安装g++

  yum install gcc-c++

安装gfortran

  yum install gcc-gfortran

重新执行:

  cd node-v4.6.0/  ./configure // 执行 Node.js 安装包自带的脚本,修改相关的系统配置文件  make //编译 C源代码为 可执行的 Linux程序

好慢啊。。。。。。难道是我买的最低配置的原因么。。。。。。

终于跑完了😂,全程大约十几分钟,所以大家要耐心等待哦。。。。。。

  sudo make install // 安装文件  node Cversion //查看安装node的版本  npm -v //查看npm的版本

现在已经安装了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

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第1张

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

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第2张

这样就运行成功了。

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

  forever start ./bin/www

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

  forever list

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第3张

现在我们就可以在浏览器中输入:公网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服务

  service nginx start

添加开机启动

  systemctl enable nginx

修改Nginx配置文件

  vim /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

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第4张

这样就是配置成功

重启nginx

  service nginx restart

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

三、安装MySQL

查看可用版本

  yum list | grep mysql

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第5张

在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包。

  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.6版本了。

  yum -y install mysql-community-server

MySQL基础配置

  systemctl enable mysqld //添加到开机启动  systemctl start mysqld //启用进程  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.  Set 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] y [禁止root远程登录]   ... Success!  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 ‘密码' WITH GRANT OPTION; //添加授权的用户  flush privileges; //刷新数据库

检测是否开启3306端口

  netstat -tunlp

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第6张

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

CentOS7 + node.js + nginx + MySQL搭建服务器全过程 Linux 第7张

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

参与评论