为扩展个人知识面以及满足我对网站建设的好奇心,以及在课程的推动下,我开始搭建属于自己的个人网站。
一 实验材料:
硬件:云服务器(可用虚拟机代替)
软件:(VMware),VScode, Pycharm, Xshell, Xftp
二实验过程:
1.使用Xshell连接远程服务器
使用xshell建立一个对话,添加主机地址(远程服务器公网IP地址)端口22,使用密码连接
使用root以及登录密码登录
2.安装Nginx以及Python
yum install nginx -y
yum install python3 -y
3.配置python虚拟环境
pip3 install virtualenv
pip3 install virtualenvwrapper
4.创建虚拟环境
选择一个我喜欢的目录,或创建一个目录
mkdir $HOME/.virtualenvs
然后进行配置
在vim ~/.bashrc中添加行
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
执行 .bashrc
source ~/.bashrc
创建虚拟环境
mkvirtualenv -p /usr/bin/python3 虚拟环境名称
自动进入虚拟环境
退出虚拟环境,以及其他管理命令
deactivate 离开虚拟环境
workon 列出虚拟环境列表
lsvirtualenv 列出虚拟环境列表
mkvirtualenv 新建虚拟环境
workon [虚拟环境名称] 进入虚拟环境
rmvirtualenv 删除虚拟环境
5.进入虚拟环境后安装Flask+uwsgi
pip3 install flask
pip3 install uwsgi
6.创建示范程序(使用Xftp)将已经建好的示例项目上传到服务器(项目为PyCharm开发,自动创建虚拟环境venv)示例代码如下
from flask import Flask, request, jsonify, session, send_file, render_template, url_for
app = Flask(__name__, template_folder='./templates', static_folder="./static", )
app.secret_key = 'asxefdwqefwefgwefwefwefwefwq234hujkwefwefwefzxfcq1230938i90x4uj0819u401jxo1u094xi90' # 设置一个用于加密会话数据的密钥
print('''
____ _ _ _ _ _ _
/ ___| | | | / \ | | | \ | |
| | | |_| | / _ \ | | | \| |
| |___| _ |/ ___ \| |___| |\ |
\____|_| |_/_/ \_\_____|_| \_|
''')
users = []
@app.route('/favicon.ico')
def icon():
return send_file("./static/favicon.ico")
@app.route('/')
def main():
return render_template('index.html')
@app.route('/show', methods=['GET'])
def show():
return 'Hello World!'
@app.route('/register', methods=['POST'])
def register():
data = request.get_json()
username = data['username']
password = data['password']
# 检查用户名是否已存在
for user in users:
if user['username'] == username:
return jsonify({'message': 'Username already exists'}), 400
user = {'username': username, 'password': password}
users.append(user)
session['username'] = username # 在会话中存储当前用户的用户名
return jsonify({'message': 'Registration successful'})
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
username = data['username']
password = data['password']
# 遍历用户列表,检查用户名和密码是否匹配
for user in users:
if user['username'] == username and user['password'] == password:
session['username'] = username # 在会话中存储当前用户的用户名
return jsonify({'message': 'Login successful'})
return jsonify({'message': 'Invalid credentials'}), 400
@app.route('/logout', methods=['POST'])
def logout():
session.pop('username', None) # 从会话中移除当前用户的用户名
return jsonify({'message': 'Logout successful'})
@app.route('/check_session', methods=['GET'])
def check_session():
if 'username' in session:
return jsonify({'logged_in': True, 'username': session['username']})
else:
return jsonify({'logged_in': False})
if __name__ == '__main__':
app.run()
7.编辑配置nginx和uwsgi
创建配置文件
touch uwsgi.ini
配置如下
socket = 127.0.0.1:5000
chdir = /home/[项目地址]
virtualenv =/home/[项目地址]/venv
wsgi-file = /home/[项目地址]/app.py
callable = app
#plugins = python
master = true
vacuum = true
chmod-socket = 664
processes = 5
daemonize = /home/[项目地址]/uwsgi.log
pidfile = /home/[项目地址]/uwsgi.pid
启动uwsgi
uwsgi uwsgi.ini
其他操作
ps -ef|grep uwsgi 查看进程是否启动
重启uwsgi: uwsgi --reload uwsgi.pid
停止: uwsgi --stop uwsgi.pid
启动: uwsgi --ini uwsgi.ini
修改nginx配置实现反向代理 vim /etc/nginx/nginx.conf
listen [::]:80;
server_name [公网IP];
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static{
root /usr/share/nginx/html;#这里写上项目地址就可以了,不用再加/static
}
location / {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
设置nginx为开机自启,并启动nginx
systemctl enable nginx
systemctl start nginx
重启以及停止命令
systemctl restart nginx
systemctl stop nginx
8.打开防火墙80端口,并 重新启动防火墙
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=80/udp
firewall-cmd --reload
9.启动项目
python3 app.py
9.浏览器访问 [公网IP]/show 出现Hello World便可大获成功!!