美文网首页藏兵谷Linux的小折腾kk
Centos7 使用 宝塔面板 搭建私人网盘(重排版)

Centos7 使用 宝塔面板 搭建私人网盘(重排版)

作者: 请叫我雯子小姐的小爷 | 来源:发表于2019-05-11 15:35 被阅读224次

项目预览

image

第一步,使用下面命令安装宝塔面板(请确保纯净系统安装)

yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh

安装大概需要2-15分钟,因机器配置不同而不同,完成后会出现一下内容

Bt-panel: http://***:8888
username: ******
password: ******

复制Bt-panel后面的网址到浏览器,可以打开宝塔面板的后台,使用username和password登录即可。

image

第二步,安装LAMP或者LNMP环境,编译安装或者急速安装都可。

第三步,点击左侧‘软件管理’,安装宝塔插件中的’宝塔一键部署源码‘。

image

第四步,打开一键部署源码,选择可道云进行一键部署

image

域名栏填写电脑的路由IP(一般为192.168. * . * )和公网IP(在terminal中输入curl ifconfig.me可得到),备注可随意填写,根目录自行选择,最后点击提交

image

第五步,设置路由器端口转发规则,登录路由器界面,设置外网80端口转发到你的路由IP的80端口(仅看最后一条规则)

image

至此,云盘已经搭建完成,可以在浏览器中输入http://127.0.0.1:80进行访问,局域网内其他电脑通过http://你的路由IP:80进行访问,其余情况下可使用http://你的公网IP:80进行访问

以下教程为附属教程,因为宝塔面板的面板设置中,在教育网或者集团网中服务器IP地址获取不正确,故有时不能通过公网IP进行访问,可按照以下步骤操作

Problem 1 :纠正宝塔面板服务器IP地址

Solution:修改这个宝塔面板的配置文件 ‘sudo vi /www/server/panel/class/public.py’,更改以下内容

def GetLocalIp():
    #~O~V~\~\~V~QIP
    try:
        import re;
        filename = 'data/iplist.txt'
        ipaddress = readFile(filename)
        if not ipaddress:
            import urllib2
            import json
            #url = 'http://pv.sohu.com/cityjson?ie=utf-8'
            url = "https://ifconfig.me/all.json"
            opener = urllib2.urlopen(url)
            str = opener.read()
            #ipaddress = re.search('\d+.\d+.\d+.\d+',str).group(0)
            ipaddress = json.loads(str.decode())
            ipaddress = ipaddress['ip_addr']
            writeFile(filename,ipaddress)
​
        ipaddress = re.search('\d+.\d+.\d+.\d+',ipaddress).group(0);
        return ipaddress
    except:
        try:
            url = web.ctx.session.home + '/Api/getIpAddress';
            opener = urllib2.urlopen(url)
            return opener.read()
        except:
            import web
            return web.ctx.host.split(':')[0];

并且使用 'sudo rm /www/server/panel/data/iplist.txt' 删除已经保存的IP信息,这样就配置好了

Problem 2:公网无法访问云盘

Solution:使用自动脚本重置域名配置文件,以Apache为例

#!/usr/bin/python
#Need to run as root, crontab -e
import os
import smtplib
from email.mime.text import MIMEText
import pickle
import requests
import time
​
### Get public ip ###
pub_ip = requests.get("https://ifconfig.me")
pub_ip = pub_ip.text
​
try:
    fin = open('.pubip.pkl','rb')
    ip = pickle.load(fin)
except:
    ip = {'pub_ip':'000.000.000.000'}
    fout = open('.pubip.pkl','wb')
    pickle.dump(ip,fout)
    fout.close()
    fin = open('.pubip.pkl','rb')
    ip = pickle.load(fin)
if ip['pub_ip'] == pub_ip:
    pass #print "public ip don't change, nothing to do"
else:
    ip['pub_ip'] = pub_ip
    fout = open('.pubip.pkl','wb')
    pickle.dump(ip,fout)
    msm = "Mr Cloud has been changed, The new address: http://"+pub_ip+".p.scnu.edu.cn"
    f = open("/www/server/panel/vhost/apache/192.168.1.101.conf","w")
    f.write('''<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot "/www/wwwroot/192.168.1.101"
    ServerName 78c7ee6f.192.168.1.101
    ServerAlias 192.168.1.101 127.0.0.1 {0}
    errorDocument 404 /404.html
    ErrorLog "/www/wwwlogs/192.168.1.101-error_log"
    CustomLog "/www/wwwlogs/192.168.1.101-access_log" combined
​
    #DENY FILES
     <Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
       Order allow,deny
       Deny from all
    </Files>
​
    #PHP
    <FilesMatch \.php$>
            SetHandler "proxy:unix:/tmp/php-cgi-54.sock|fcgi://localhost"
    </FilesMatch>
​
    #PATH
    <Directory "/www/wwwroot/192.168.1.101">
        SetOutputFilter DEFLATE
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html index.htm default.php default.html default.htm
    </Directory>
</VirtualHost>'''.format(pub_ip))
​
    time.sleep(3)
    os.system("/etc/init.d/bt restart") #此处不是必须
    time.sleep(3)
    os.system("/etc/init.d/httpd restart") #必须有这句,用来重启Apache服务

此脚本需要添加可执行权限(chmod 777 apache.py)添加到root用户的自动化任务中,使用以下命令(crontab -e)

*/30 * * * * /PATH/apache.py

Problem 3:挂载移动硬盘到用户www下

Solution:将下面的命令添加到 'sudo vi /etc/fstab'中

/dev/sdb1 /media/sdb1  ntfs rw,noauto,users,uid=1000,gid=1000,permissions 0 2

相关文章

网友评论

    本文标题:Centos7 使用 宝塔面板 搭建私人网盘(重排版)

    本文链接:https://www.haomeiwen.com/subject/kpbxaqtx.html