CentOS7 + node.js + nginx + MySQL搭建服務(wù)器全過程
前言
最近項(xiàng)目要求,需要一臺(tái)服務(wù)器,阿里和騰訊都不錯(cuò),我選擇了騰訊云,系統(tǒng)為CentOS 7(linux)。記錄搭建服務(wù)器的過程,方便以后再次搭建是借鑒和給第一次搭建服務(wù)器的朋友借鑒之用。
工具
安裝git
執(zhí)行:
sudo yum install git
安裝nodejs
官網(wǎng)查看最新版本
下載
先進(jìn)入/usr/src文件夾,這個(gè)文件夾通常用來存放軟件源代碼:
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)中沒有安裝C編譯器,接下來先安裝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, 可以開始部署應(yīng)用程序, 首先要使用Node.js的模塊管理器npm安裝Express middleware 和forever(一個(gè)用來確保應(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
第四條命令就相當(dāng)于執(zhí)行了node ./bin/www
。
這樣就運(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
現(xiàn)在我們就可以在瀏覽器中輸入:公網(wǎng)IP + :3000,來訪問我們的程序。
如果要修改3000端口,我們可以修改./bin/www文件中關(guān)于監(jiān)聽3000端口的字段。
停止運(yùn)行:
forever stop 0 //0代表前面[0],這是當(dāng)前進(jìn)程的ID
停止所有:
forever stopall
二、安裝Nginx
HTTP請(qǐng)求是80端口,但是在Linux上非root權(quán)限是無法使用1024以下端口的,并且因?yàn)榘踩?,最好不要使用root權(quán)限登錄服務(wù)器,所以無法直接用node.js程序監(jiān)聽80端口。因此我們需要使用Nginx給node.js做反向代理,將80端口指向應(yīng)用程序監(jiān)聽的端口(如node.js默認(rèn)的3000端口)。
添加Nginx倉(cāng)庫
yum install epel-release
下載Nginx
yum install nginx
啟用nginx服務(wù)
service nginx start
添加開機(jī)啟動(dòng)
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; #對(duì)應(yīng)該的Nodejs程序端口 } access_log /mnt/log/www/jakexin_access.log; #網(wǎng)站訪問日志 }
測(cè)試配置文件是否能夠正確運(yùn)行
nginx -t
這樣就是配置成功
重啟nginx
service nginx restart
現(xiàn)在直接在瀏覽器中輸入我們配置的域名就可以訪問我們的項(xiàng)目了。
三、安裝MySQL
查看可用版本
yum list | grep mysql
在centOS 7中不能使用yum -y install mysql mysql-server mysql-devel
安裝,這樣會(huì)默認(rèn)安裝mysql的分支mariadb。
MariaDB數(shù)據(jù)庫管理系統(tǒng)是MySQL的一個(gè)分支,主要由開源社區(qū)在維護(hù),采用GPL授權(quán)許可 MariaDB的
的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。
正確的安裝方法
眾所周知,Linux系統(tǒng)自帶的repo是不會(huì)自動(dòng)更新每個(gè)軟件的最新版本(基本都是比較靠后的穩(wěn)定版),所以無法通過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 //添加到開機(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)程登錄] ... 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ù)庫] - 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ù)庫
檢測(cè)是否開啟3306端口
netstat -tunlp
看到3306端口被開啟之后,我們就可以使用本地客戶端遠(yuǎn)程訪問數(shù)據(jù)庫了
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Linux如何實(shí)現(xiàn)斷點(diǎn)續(xù)傳文件功能
最近在工作中遇到一個(gè)需求,要實(shí)現(xiàn)Linux下的文件傳輸,支持?jǐn)帱c(diǎn)續(xù)傳,所以這篇文章主要給大家介紹了關(guān)于Linux如何實(shí)現(xiàn)斷點(diǎn)續(xù)傳文件功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12Linux下部署springboot項(xiàng)目的方法步驟
這篇文章主要介紹了Linux下部署springboot項(xiàng)目的方法步驟,由于springboot是內(nèi)嵌了tomcat,所以可以直接將項(xiàng)目打包上傳至服務(wù)器上,具體實(shí)例方法大家參考下本文2018-06-06Centos7配置fastdfs和nginx分布式文件存儲(chǔ)系統(tǒng)實(shí)現(xiàn)過程解析
這篇文章主要介紹了centos7配置fastdfs及nginx并實(shí)現(xiàn)分布式文件存儲(chǔ)系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06zabbix進(jìn)行數(shù)據(jù)庫備份以及表分區(qū)的方法
這篇文章主要介紹了zabbix進(jìn)行數(shù)據(jù)庫備份以及表分區(qū)的方法,需要的朋友可以參考下2015-10-10