作者: 1fan

Ubuntu搭建Nginx+Mysql+Flask+uWSGI

Ubuntu 20.04 64 Bit
Python 3.8.2
第一件事当然是更新一下

sudo apt update
sudo apt upgrade

安装并设置宝塔面板

为了部署和配置方便,我们这里直接安装宝塔面板。

wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

登录宝塔面板后,编译安装LNMP:
Nginx+ MySQL+ PureFtpd
(如果站点纯粹使用python则无需安装php和phpmyadmin)

部署flask+uWSGI环境

先安装依赖

sudo apt-get install build-essential python3-dev libssl-dev libffi-dev
sudo apt-get install python3-pip
sudo pip3 install uwsgi flask pymysql

在宝塔上部署网站

我们先在宝塔上新建一个站点,FPT和MYSQL一起新建也可以,假定域名为example.com,目录为yoursite。假定去阿里云申请免费证书,并下载适用于nginx的证书文件,并将证书文件yoursert.pem和yoursite.key放置于目录yoursite下

Nginx配置:

server {
    listen 80;
    server_name example.com www.example.com;              #此处填写域名管理选项卡中绑定的域名,多个域名用空格隔开
    rewrite ^(.*) https://$server_name$1 permanent;
}
server
{
    # ssl证书使用443
      listen 443 ssl;
    server_name example.com www.example.com ;              #此处填写域名管理选项卡中绑定的域名,多个域名用空格隔开
    charset utf-8;
    # 证书.pem的存放地址
      ssl_certificate /www/wwwroot/yoursite/yourcert.pem;
      # 证书.key的存放地址
      ssl_certificate_key /www/wwwroot/yoursite/yourcert.key;
      ssl_session_timeout 5m;
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      error_page 497 https://$host:$server_port$uri$is_args$args;
    location / {
        include uwsgi_params;                       # 导入uwsgi配置 
        uwsgi_pass 127.0.0.1:5555;                  # 转发端口,需要和uwsgi配置当中的监听端口一致
        uwsgi_param UWSGI_PYTHON /usr/bin/python3;  # Python解释器所在的路径,如果有虚拟环境可将路径设置为虚拟环境
        uwsgi_param UWSGI_CHDIR /www/wwwroot/yoursite;         # 项目根目录
        uwsgi_param UWSGI_SCRIPT run:app;           # 项目的主程序,比如你整站用run.py文件来响应客户端请求,文件中app = Flask(__name__),那么这里就填run:app 
    }
}

这样设定好以后网站默认是开启SSL的,SSL监听的是443端口,需要在宝塔的安全面板中,在防火墙设置里放行443,否则网站将无法访问。

UWSGI配置:

在网站根目录新建一个uwsgi.ini,内容如下:

[uwsgi]
# uWSGI 的监听端口
socket          = 127.0.0.1:5555
# 项目根目录
chdir           = /www/wwwroot/yoursite
# Flask 项目的启动文件
wsgi-file       = run.py
# 程序内启用的application变量名
callable        = app 
# 进程数量
processes       = 4
vacuum          = true
master          = true
# py文件更新后自动重启
py-autoreload   = 1
# 设置stats文件和pidfile位置
stats           = %(chdir)/uwsgi.status
pidfile         = %(chdir)/uwsgi.pid
# 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器
daemonize       = %(chdir)/uwsgi.log

启动uWSGI服务:

uwsgi --ini /www/wwwroot/yoursite/uwsgi.ini &

这时候可以测试一下网站是否能够访问。如果出现500错误或者Bad Gateway,可以cat /www/wwwroot/yoursite/uwsgi.log查看一下uWSGI的日志

设置uwsgi自启动服务

在 Ubuntu 上使用 Nginx+ uWSGI 部署Flask项目,在实际生成环境中需要系统自动启动这两项服务,Ubuntu默认自启动Nginx,需要对uwsgi设置为系统自启动。

Ubuntu20.04采用systemctl来管理开机启动的脚本,对于uwsgi服务要设置成系统服务来进行自启动。

创建uwsgi.service文件

sudo nano /etc/systemd/system/uwsgi.service

代码如下:

[Unit]
Description=uWSGI server
After=syslog.target 
[Service]
#用户名
User=www
#用户组
Group=www
#项目目录
WorkingDirectory=/www/wwwroot/yoursite
#服务启动的代码(可以用which uwsgi查看uwsgi安装位置)
ExecStart=/usr/local/bin/uwsgi --ini /www/wwwroot/yoursite/uwsgi.ini
#服务停止代码
ExecStop=/usr/local/bin/uwsgi --stop /www/wwwroot/yoursite/uwsgi.pid
#这种方式可以让你用nohup起动服务
Type=oneshot
#表示虽然nohup后台运行了,也认为这个服务是起来的
RemainAfterExit=yes

[Install]
#指明会跟随系统启动而启动该服务 
WantedBy=multi-user.target

启动、停止、重启、查看服务命令 :

sudo systemctl start uwsgi
sudo systemctl stop uwsgi
sudo systemctl restart uwsgi
sudo systemctl status uwsgi

加入、关闭系统自启动:

sudo systemctl daemon-reload
#执行enable命令后在/etc/systemd/system/multi-user.target.wants目录下生成uwsgi.sevice的链接文件 
sudo systemctl enable uwsgi
sudo systemctl disable uwsgi

How to move Plex metadata and index data to new drive/partition and/or directory location

The most recent versions of the Plex Media Server has the option to auto index, depending on your library size, this can take up a lot of space, and in some instances you may need to have this saved in a different location. You may also have a very large media collection, and the metadata could be very large in size too. Well, I recently had this issue and between metadata and index data I had almost 55GB of a 64GB Solid State Drive used just for Plex metadata and index data! Here’s how I moved all of that data to a new location and did it without making any changes to the configuration…

Update 3/3/2014: Check the comments below for additional methods to change the directory, but the easiest will be modifying the /etc/default/plexmediaserver file and specifying the location.

Just to note, my current setup is using OMV (Open Media Vault) that runs Debian, but this should be the same for almost any version of linux, including Ubuntu, etc. The very simple approach I took to make this change was by using a symlink, and a few other small changes.

Stop Plex

First thing you will need to do is to stop Plex Media Server

sudo service plexmediaserver stop

Make sure all Plex services are killed

Next run this command to make sure all Plex services are killed and no longer running

killall -u plex

Copy Metadata and Index Data to new location

Now copy all of the current meta and index data to the new location, this may take a while depending on the size. This is assuming that Plex data is stored at “/var/lib/plexmediaserver/Library/Application Support” which is the default for Debian based Plex Media Server installs. This command below should copy the Application Support directory to the /new/dir/to/plex, ultimately making it “/new/dir/to/plex/Application Support”

cp -rf '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server' '/data/DataCenter/Plex Media Server'

Move old directory to .OLD in case of issues

Now we will move the old directory to .OLD in case we need to restore it if there are any issues

sudo mv '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server' '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server.OLD'

Create symbolic link to new directory

Next we will create a symbolic link to the new location for our meta and index data

sudo ln -s '/data/DataCenter/Plex Media Server' '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server'

Change owner of new location to Plex

We now have to update the permissions and change the owner to Plex otherwise there will be issues

sudo chown -R plex.plex '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server'
sudo chown -R plex.plex '/data/DataCenter/Plex Media Server' 

Start Plex Media Server

Next we will start Plex Media Server to make sure everything worked correctly. After issuing the command below, login to Plex via http://ipofserver:32400/manage and make sure everything is still the same and working correctly.

sudo service plexmediaserver start

Remove old Meta and Index Data files

As long as everything works correctly, you can now remove the old directory we renamed in case of any issues.

sudo rm -rf '/var/lib/plexmediaserver/Library/Application Support.OLD'

Voila, you have now moved your Meta and Index data to a new location