CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、主機(jī)域名、虛擬主機(jī)、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。

工具

安裝git

執(zhí)行:

sudo yum install git

安裝nodejs

查看新版本

下載


先進(jìn)入/usr/src文件夾,這個(gè)文件夾通常用來(lái)存放軟件源代碼:

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 // 執(zhí)行 node.js 安裝包自帶的腳本,修改相關(guān)的系統(tǒng)配置文件

發(fā)現(xiàn)報(bào)錯(cuò)了,提示系統(tǒng)中沒(méi)有安裝c編譯器,接下來(lái)先安裝c編譯器

安裝gcc

yum install gcc

安裝g++

yum install gcc-c++

安裝gfortran

yum install gcc-gfortran

重新執(zhí)行:

cd node-v4.6.0/
./configure // 執(zhí)行 node.js 安裝包自帶的腳本,修改相關(guān)的系統(tǒng)配置文件
make //編譯 c源代碼為 可執(zhí)行的 linux程序

好慢啊。。。。。。難道是我買的最低配置的原因么。。。。。。

終于跑完了?,全程大約十幾分鐘,所以大家要耐心等待哦。。。。。。

sudo make install // 安裝文件
node –version //查看安裝node的版本
npm -v //查看npm的版本

現(xiàn)在已經(jīng)安裝了node.js, 可以開(kāi)始部署應(yīng)用程序, 首先要使用node.js的模塊管理器npm安裝express middleware 和forever(一個(gè)用來(lái)確保應(yīng)用程序啟動(dòng)并且在需要時(shí)重啟的非常有用的模塊),其中g(shù)參數(shù)是把express安裝到nodejs的lib目錄,d參數(shù)表示同時(shí)安裝依賴模塊包:

npm install -gd express-generator forever

建立測(cè)試項(xiàng)目并執(zhí)行


在/home文件夾下執(zhí)行:

express testapp
cd testapp
npm install
npm start

上面,第一條命令是創(chuàng)建express框架通用項(xiàng)目,第三條命令是安裝依賴包,第四條是執(zhí)行。


執(zhí)行:

cat package.json[object Object]

第四條命令就相當(dāng)于執(zhí)行了node ./bin/www。

CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法這樣就運(yùn)行成功了。

但是當(dāng)我們關(guān)閉終端之后,進(jìn)程就將結(jié)束,現(xiàn)在剛安裝的forever就派上用場(chǎng)了,forever可以讓進(jìn)程在終端關(guān)閉之后繼續(xù)運(yùn)行:

forever start ./bin/www

我們可以使用下面命令查看forever運(yùn)行的程序:

forever list

CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法現(xiàn)在我們就可以在瀏覽器中輸入:公網(wǎng)ip + :3000,來(lái)訪問(wèn)我們的程序。

如果要修改3000端口,我們可以修改./bin/www文件中關(guān)于監(jiān)聽(tīng)3000端口的字段。

停止運(yùn)行:

forever stop 0 //0代表前面[0],這是當(dāng)前進(jìn)程的id

停止所有:

forever stopall

二、安裝nginx

http請(qǐng)求是80端口,但是在linux上非root權(quán)限是無(wú)法使用1024以下端口的,并且因?yàn)榘踩颍貌灰褂胷oot權(quán)限登錄服務(wù)器,所以無(wú)法直接用node.js程序監(jiān)聽(tīng)80端口。因此我們需要使用nginx給node.js做反向代理,將80端口指向應(yīng)用程序監(jiān)聽(tīng)的端口(如node.js默認(rèn)的3000端口)。

添加nginx倉(cāng)庫(kù)


yum install epel-release

下載nginx


yum install nginx

啟用nginx服務(wù)


service nginx start

添加開(kāi)機(jī)啟動(dòng)


systemctl enable nginx

修改nginx配置文件


vim /etc/nginx/nginx.conf //使用lnpm意見(jiàn)安裝,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;  #對(duì)應(yīng)該的nodejs程序端口
 }
 access_log /mnt/log/www/jakexin_access.log; #網(wǎng)站訪問(wèn)日志
}

測(cè)試配置文件是否能夠正確運(yùn)行


nginx -t[object Object]

這樣就是配置成功

重啟nginx


service nginx restart

現(xiàn)在直接在瀏覽器中輸入我們配置的域名就可以訪問(wèn)我們的項(xiàng)目了。

三、安裝mysql

查看可用版本

yum list | grep mysql

CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法在centos 7中不能使用yum -y install mysql mysql-server mysql-devel安裝,這樣會(huì)默認(rèn)安裝mysql的分支mariadb。

mariadb數(shù)據(jù)庫(kù)管理系統(tǒng)是mysql的一個(gè)分支,主要由開(kāi)源社區(qū)在維護(hù),采用gpl授權(quán)許可 mariadb的
的是完全兼容mysql,包括api和命令行,使之能輕松成為mysql的代替品。


正確的安裝方法


眾所周知,linux系統(tǒng)自帶的repo是不會(huì)自動(dòng)更新每個(gè)軟件的新版本(基本都是比較靠后的穩(wěn)定版),所以無(wú)法通過(guò)yum方式安裝mysql的高級(jí)版本。所以我們需要先安裝帶有當(dāng)前可用的mysql5系列社區(qū)版資源的rpm包。

rpm -uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
yum repolist enabled | grep “mysql.-community.“ //查看當(dāng)前可用資源

從上面的列表可以看出, mysql56-community/x86_64 和 mysql 5.6 community server 可以使用。

因此,我們就可以直接用yum方式安裝了mysql5.6版本了。

yum -y install mysql-community-server

mysql基礎(chǔ)配置


systemctl enable mysqld //添加到開(kāi)機(jī)啟動(dòng)
systemctl start mysqld //啟用進(jìn)程
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   [設(shè)置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遠(yuǎn)程登錄](méi)
 ... 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數(shù)據(jù)庫(kù)]
 - 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  [刷新權(quán)限]
 ... 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

配置遠(yuǎn)程連接

grant all privileges on . to ‘root'@'%' identified by ‘密碼' with grant option; //添加授權(quán)的用戶
flush privileges; //刷新數(shù)據(jù)庫(kù)

檢測(cè)是否開(kāi)啟3306端口


netstat -tunlp[object Object]

看到3306端口被開(kāi)啟之后,我們就可以使用本地客戶端遠(yuǎn)程訪問(wèn)數(shù)據(jù)庫(kù)了

“CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

本文標(biāo)題:CentOS7+node.js+nginx+MySQL搭建服務(wù)器的方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://bm7419.com/article14/ihhde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化云服務(wù)器、企業(yè)網(wǎng)站制作、外貿(mào)建站微信公眾號(hào)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)