环境:

  • Debian 9
  • PostgreSQL 9.6

[========]

安装PostgreSQL

新增并编辑:/etc/apt/sources.list.d/pgdg.list

deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main

通过apt安装

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6 postgresql-contrib-9.6

通过pip安装pgAdmin 4

sudo apt-get install python3-pip build-essential python3-dev
sudi pip3 install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v2.0/pip/pgadmin4-2.0-py2.py3-none-any.whl

初始化pgadmin配置

进入/usr/local/lib/python3.5/dist-packages/pgadmin4/,新建config_local.py,并写入:

LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = '/var/lib/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = '/var/lib/pgadmin4/sessions'
STORAGE_DIR = '/var/lib/pgadmin4/storage'
SERVER_MODE = False
ALLOW_SAVE_PASSWORD = False

SERVER_MODE 设置为True,则pgAdmin需要登录后使用。
ALLOW_SAVE_PASSWORD设置为True,则允许保存数据库密码。

执行python3 setup.py将上述设置保存到pgadmin4.db。

安装uwsgi

sudo pip3 install uwsgi

测试

uwsgi --http :8000 --wsgi-file /usr/local/lib/python3.5/dist-packages/pgadmin4/pgAdmin4.wsgi

访问 http://localhost:8000进行测试。

配置uwsgi的emperor mode,并使用systemd配置为服务

sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals

新增并编辑/etc/uwsgi/emperor.ini

[uwsgi]
emperor = /etc/uwsgi/vassals
uid = www-data
gid = www-data

新增并编辑/etc/systemd/system/emperor.uwsgi.service

[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

配置nginx连通uwsgi

新增并编辑/etc/uwsgi/vassals/pgadmin.ini

[uwsgi]
wsgi-file       = /usr/local/lib/python3.5/dist-packages/pgadmin4/pgAdmin4.wsgi
master          = true
processes       = 1
socket          = /tmp/pgadmin.sock
chmod-socket    = 664
vacuum          = true

编辑/etc/nginx/sites-available/pgadmin.conf

upstream pgadmin {
    server unix:///tmp/pgadmin.sock; 
}

server {
    listen 80 default_server;
    server_name _;
    charset     utf-8;

    client_max_body_size 75M;  

    location / {
        include uwsgi_params;
        uwsgi_pass  pgadmin;
    }
}

/etc/nginx/sites-available/pgadmin.conf链接到/etc/nginx/sites-enabled

/etc/nginx/sites-enabled# ln -s /etc/nginx/sites-available/pgadmin.conf

配置文件权限:

chown www-data:www-data /var/lib/pgadmin4/pgadmin4.db
chown -R www-data:www-data /var/log/pgadmin4/
chown -R www-data:www-data /var/lib/pgadmin4/

重启nginx&uwsgi

/etc/init.d/nginx restart
systemctl restart emperor.uwsgi.service