CentOS7+node.js+nginx+MySQL搭建服务器的方法

发布时间:2022-04-16 17:47:28 作者:zzz
来源:亿速云 阅读:174

本篇内容介绍了“CentOS7+node.js+nginx+MySQL搭建服务器的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

工具

安装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 –version //查看安装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[object Object]

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

CentOS7+node.js+nginx+MySQL搭建服务器的方法这样就运行成功了。

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

forever start ./bin/www

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

forever list

CentOS7+node.js+nginx+MySQL搭建服务器的方法现在我们就可以在浏览器中输入:公网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[object Object]

这样就是配置成功

重启nginx

service nginx restart

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

三、安装mysql

查看可用版本

yum list | grep mysql

CentOS7+node.js+nginx+MySQL搭建服务器的方法在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[object Object]

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

“CentOS7+node.js+nginx+MySQL搭建服务器的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Linux 搭建服务器和客户端 所用的函数
  2. php搭建服务器的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

centos7 node.js nginx

上一篇:CentOS7的网络配置命令有哪些

下一篇:Centos7中Nginx开机自启动问题怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》