Centos7下啟用端口

今天寫(xiě)了一下node
的小demo,于是想放到阿里云服務(wù)器上,但是訪問(wèn)時(shí)卻發(fā)生了錯(cuò)誤。考慮了下,覺(jué)得問(wèn)題在于端口號(hào)上,我寫(xiě)的node
demo是監(jiān)聽(tīng)的3000
端口,然而linux
下有防火墻默認(rèn)只有80
端口開(kāi)放,于是嘗試了下將服務(wù)器的3000
端口打開(kāi),期間還是遇坑不少,下面就梳理下整個(gè)過(guò)程:
關(guān)閉防火墻
網(wǎng)上的大部分資料都是用iptables
防火墻的,但是阿里云的centos 7
默認(rèn)防火墻是firewall
。最為簡(jiǎn)單的方法其實(shí)就是關(guān)閉我們的防火墻:
查看下防火墻的狀態(tài):

systemctl status firewalld
1

這里寫(xiě)圖片描述

關(guān)閉防火墻:

systemctl stop firewalld
1

這樣就解決了,在訪問(wèn)下ip地址:端口號(hào)
就可以看到所寫(xiě)的應(yīng)用啦! 但是這樣的問(wèn)題是很不安全,其實(shí)可以將firewall
服務(wù)禁用,應(yīng)用iptables
服務(wù)(網(wǎng)上大部分啟用端口的資料都是基于iptables
服務(wù))。
安裝iptables
由于沒(méi)有防火墻會(huì)造成不安全,所以給服務(wù)器安裝一應(yīng)用更廣的防火墻iptables
,首先要禁用firewall
,通過(guò)yum
安裝iptables

systemctil disable firewalldyum install -y iptables-services
1
2

啟動(dòng)iptables
systemctl start iptables
1

啟動(dòng)后可以通過(guò)systemctl status iptables
查看狀態(tài)。
更改iptables規(guī)則
將iptables
文件備份下:

cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
1

設(shè)置 INPUT 方向所有的請(qǐng)求都拒絕

iptables -P INPUT DROP
1

放開(kāi)所需端口

iptables -I INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPTiptables -I INPUT -p tcp --dport 3000 -m state --state NEW -j ACCEPT
1
2

保存規(guī)則

iptables-save > /etc/sysconfig/iptables
1

設(shè)置為開(kāi)機(jī)啟動(dòng)并且重啟

systemctl enable iptables.servicesystemctl reboot
1
2

這樣就將所需的端口3000
啟用,進(jìn)入到目錄下運(yùn)行node app.js
,就可以來(lái)看node
demo啦! 安利下一個(gè)小的腳手架: https://github.com/zp1996/lsgo-cli

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容