Apache+Flask在云服务器搭建个人网站


持有云服务器的前提下,基于Flask开发简易个人网站并使用Apache部署,而且允许外界通过域名访问。


一、服务器


1.在Vultr官网(https://my.vultr.com/)部署一台符合需求的服务器,我仅仅为了演示demo,所以选的配置如下:




2.部署服务器之后,使用FinalShell等软件连接服务器,下面提供了FinalShell的下载地址。
链接:https://pan.baidu.com/s/1GZ7UsJKJOXOl7QKlTS1O0w
提取码:4iug


3.通过服务器的IP、用户名和密码来连接服务器,本文使用IP:1.2.3.4进行演示。



4.为了外界能够直接访问个人网站,需要配置防火墙规则

sudo ufw enable
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

二、域名


1.可以在各大域名网站选择自己的域名,我在Namesilo官网(https://www.namesilo.com/)选了一个域名,演示用a.com代替。


2.在Namesilo官网配置好两条A记录,使域名www.a.com和a.com能够解析到服务器IP:1.2.3.4上。(温馨提示:为了网站的安全性,可以选择Cloudflare的DNS服务,但Cloudflare提供SSL/TLS加密模式一定不能选灵活,否则访问域名会出现重定向次数过多的问题)



三、配置环境


1.编辑sources列表

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list

添加以下内容

deb https://deb.debian.org/debian buster main contrib non-free
deb-src https://deb.debian.org/debian buster main contrib non-free

deb https://debian.mirror.constant.com buster main contrib non-free
deb-src https://debian.mirror.constant.com buster main contrib non-free

deb https://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb-src https://deb.debian.org/debian-security/ buster/updates main contrib non-free

deb https://deb.debian.org/debian buster-updates main contrib non-free
deb-src https://deb.debian.org/debian buster-updates main contrib non-free

2.服务器需要安装Python3.7、Mariadb和Git。以下是安装命令:

# 更新软件包列表
sudo apt update

# 安装Python3.7
sudo apt install python3.7 virtualenv

# 安装MySQL
sudo apt install mariadb-server

# 安装Git
sudo apt install git

3.配置MySQL

# 启动MySQL服务
sudo systemctl start mysql

# 启动安装向导
sudo mysql_secure_installation

# 使用root身份登录数据库
mysql -u root -p

# 创建数据库新用户
CREATE USER 'user'@'localhost' IDENTIFIED BY 'User.555';

# 创建DB
CREATE DATABASE web_db;

# 授予用户权限
GRANT ALL PRIVILEGES ON web_db.* TO 'user'@'localhost';

# 刷新权限并退出数据库
FLUSH PRIVILEGES;
EXIT

4.下载源码并进入项目目录

git clone https://github.com/qinhj5/WebRepo.git
cd WebRepo

5.创建虚拟环境和安装依赖

virtualenv --python=python3.7 venv
source venv/bin/activate
pip3.7 install -r requirements.txt

6.编辑数据库配置文件和初始化数据库表,确保配置文件中的信息和之前设置的一致

cp config.json config.json.bak
vim config.json
python3.7 cli.py --func create
python3.7 cli.py --func init_users

(init_users脚本会初始化两位用户Jack和Alice)


四、启动服务


1.安装 Apache 服务器以及依赖模块

sudo apt install apache2 libapache2-mod-wsgi-py3

2.为a.com获取 Let’s Encrypt 免费证书

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d a.com -d www.a.com

根据向导配置完成后会提示fullchain.pem(完整证书链)和 privkey.pem(私钥)的路径,通常在以下位置:
/etc/letsencrypt/live/a.com/fullchain.pem
/etc/letsencrypt/live/a.com/privkey.pem


3.移动项目到部署目录,并授权给www-data

cp -r ../WebRepo /var/www
sudo chown -R www-data:www-data /var/www/WebRepo

4.编写 Apache 虚拟主机配置文件

vim /etc/apache2/sites-available/WebRepo.conf

输入以下内容,其中私钥、完整证书链和域名需要修改:

<VirtualHost *:80>
    ServerName a.com
    ServerAlias www.a.com
    Redirect permanent / https://a.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName a.com
    ServerAlias www.a.com
	
    DocumentRoot /var/www/WebRepo
	
    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCertificateFile /etc/letsencrypt/live/a.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/a.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    WSGIDaemonProcess WebRepo python-home=/var/www/WebRepo/venv processes=2 threads=4 user=www-data group=www-data home=/var/www/WebRepo
    WSGIProcessGroup WebRepo

    WSGIScriptAlias / /var/www/WebRepo/app.wsgi

    <Directory /var/www/WebRepo>
        WSGIProcessGroup WebRepo
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/WebRepo_error.log
    CustomLog /var/log/apache2/WebRepo_access.log combined
</VirtualHost>


5.关闭Apache默认的虚拟主机配置文件

sudo a2dissite 000-default.conf
sudo a2dissite 000-default-le-ssl.conf

6.开启Apache的SSL模块

sudo a2enmod ssl

7.启用自定义的虚拟主机配置文件并重启Apache服务

sudo a2ensite WebRepo.conf
sudo systemctl reload apache2

8.此时即可通过域名https://www.a.com或者https://a.com 访问个人网站





至此教程结束,完整的个人网站还需要二次开发,本文只提供了一个简单的Demo。

有错误或者改进的地方请各位积极指出!

猜你喜欢

转载自blog.csdn.net/embracestar/article/details/132919569