Windows环境(Cygwin)下,使用PostgreSQL, Redis

install python3, Postgresql, Redis, psycopg2, gunicorn in Cygwin (under Windows7)
Web开发,最好是Linux开发环境(如Ubuntu)。因为好多工具都是Linux专用版本的,比如:PostgreSQL, Redis, gunicorn。
有些场合(如学校、公司),只有Windows环境,那如果使用这些Linux工具呢?
— 用Cygwin吧!

以这篇需要REDIS的为例:https://beenje.github.io/blog/posts/running-background-tasks-with-flask-and-rq/

《Windows环境(Cygwin)下,使用PostgreSQL, Redis》 app.jpg

在Cygwin里,运行时界面:

  • Redis RQ运行中

    《Windows环境(Cygwin)下,使用PostgreSQL, Redis》 rq_worker.jpg

  • python3运行中

    《Windows环境(Cygwin)下,使用PostgreSQL, Redis》 runserver.jpg

  • Redis-Server运行中

    《Windows环境(Cygwin)下,使用PostgreSQL, Redis》 redis-server.jpg

  • PostgreSQL运行中

    《Windows环境(Cygwin)下,使用PostgreSQL, Redis》 postgresql.jpg

cygwin安装步骤:

  1. 先从官方安装cygwin64
    https://www.cygwin.com/setup-x86_64.exe
    选择包的时候,勾选:web->lynx,python3,dev->make,dev->mingw64-x86_64-gcc-core

  2. 打开Cygwin64 terminal
    Option -> DejaVu Sans Mono 字体 常用的编程字体
    修改 ~/.bashrc -> 去除alias ls/df 之类的注释
    printenv HOME
    export PATH=/usr/sbin:/home/<user-id>/bin:`printenv PATH`
    永久性的加入你的PATH变量,修改 ~/.bash_profile 文件
    export PATH=/usr/sbin:/home/<user-id>/bin:$PATH

  3. 安装apt-cyg,类似于Ubuntu apt-get
    lynx -source https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
    install apt-cyg /bin

  4. 尽情用apt-cyg下载安装吧:
    apt-cyg install wget
    apt-cyg update
    apt-cyg install gcc-core tmux sl libpq-devel python3-devel libxml2-devel python3-pip

PostgreSql

apt-cyg install postgresql

初始化PostgreSql:

export CYGWIN=server; /usr/sbin/cygserver -E&
/usr/sbin/initdb.exe -D /var/psql/data
# 属于此数据库系统的文件宿主为用户 "<user-id>".
# 数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
# 默认的数据库编码已经相应的设置为 "UTF8".
# initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
# 缺省的文本搜索配置将会被设置到"simple"

修复已存在目录 /var/psql/data 的权限 ... 成功
正在创建子目录 ... 成功
选择默认最大联接数 (max_connections) ... 30
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择动态共享内存实现 ......posix
创建配置文件 ... 成功
正在运行自举脚本 ...成功
正在执行自举后初始化 ...成功
同步数据到磁盘...成功

成功。您现在可以用下面的命令开启数据库服务器:
    /usr/sbin/pg_ctl -D /var/psql/data -l logfile start

# https://gist.github.com/buruzaemon/5351379 => 内容保存为~/bin/pg文件
mkdir ~/bin
lynx -source https://gist.githubusercontent.com/buruzaemon/5351379/raw/fde74631d35afea4e7b8ddc6678fd5637466b2f7/gistfile1.txt > ~/bin/pg
chmod 777 ~/bin/pg
mkdir -p /var/psql/log
touch /var/psql/log/postgresql.log
$~/bin/pg start/stop/status
# starting postgresql...
# 正在启动服务器进程
$ pg status
pg_ctl: 正在运行服务器进程(PID: 5328)
/usr/sbin/postgres "-D" "/var/psql/data"

创建数据库
psql postgres

create user <user-id> with password 'password';
create database exampledb owner <user>;
grant all privileges on database exampledb to <user>;

Redis

安装

$ wget http://download.redis.io/releases/redis-stable.tar.gz
$ tar xzf redis-stable.tar.gz
$ cd redis-stable
$ make (耐心等待2分钟,请确保apt-cyg已经下载依赖包)
$ cp src/*.exe ~/bin
$ redis-server &
$ redis-cli -h localhost
localhost:6379> ping
PONG
localhost:6379>

use Cyginw internal Python, NOT Windows Python!!

否则的话,很多unix的功能不能用,比如:os.fork()
ln -s /usr/bin/python3 /usr/bin/python
python -v # 应该显示3.6.3或以上
还需要安装pip3: apt-cyg install python3-pip
ln -s /usr/bin/pip3 /usr/bin/pip
$ pip -V
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)

autoenv

懒人神器,进入某目录时,自动执行命令,比如source venv/bin/activate

$ pip3 install autoenv
$ which activate.sh
/usr/bin/activate.sh
echo "source `which activate.sh`" >> ~/.bashrc
source ~/.bashrc
touch git/flask-rq-example/.env
vi git/flask-rq-example/.env
# 在 .env文件内,加入你想自动执行的命令,如:source venv/bin/activate
# export XXX
# 注意:需要用vi 输入这些命令,其它编辑软件好像格式不兼容
$ cd git/flask-rq-example/
-bash: shasum: 未找到命令
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/yuanyuan/git/flask-rq-example/.env:
autoenv:
autoenv:     --- (begin contents) ---------------------------------------
autoenv:     source venv/bin/activate
autoenv:
autoenv:     --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
这里,就会自动变成python虚拟环境了,哈哈:  
(venv)
yuanyuan@yuanyuan-PC ~/git/flask-rq-example

好了,环境已经Ready!开工吧!
目前为止,cygwin目录855MB左右。

测试

cd ~/git
git clone https://github.com/beenje/flask-rq-example.git
cd flask-rq-example
# 虚拟环境目录,取名venv,换其它的也行,比如myenv
python3 -m venv venv
source venv/bin/activate
# 注意:pip是python3的版本
touch requirements.txt
# 输入以下python模块:
Flask==0.12.2
Flask-Bootstrap==3.3.7.1
Flask-Script==2.0.6
Flask-WTF==0.14.2
redis==2.10.6
requests==2.18.4
rq==0.9.2

# 安装python模块:
pip install -r requirements.txt

# 修改你的redis服务地址:
# ~/git/flask-rq-example/app/settings.py
REDIS_URL = 'redis://localhost:6379/0'

# 运行RQ worker:
$ python manage.py runworker
10:57:28 RQ worker 'rq:worker:yuanyuan-PC.59068' started, version 0.9.2
10:57:28 Cleaning registries for queue: default
10:57:28
10:57:28 *** Listening on default...

# 另外开一个Terminal,运行web server:
$ python manage.py runserver
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!

打开浏览器,输入:http://localhost:5000,Bingo!

补遗:

  • lxml安装不了?Beautiful Soup需要这个的。
python3安装时,其实已经装好lxml==3.7.3了,不需要再重新安装。
直接复制到venv里就行:
cp -r /usr/lib/python3.6/site-packages/lxml*  ~/git/ourbits/myenv/lib/python3.6/site-packages/

# 应该是最新的lxml 4.1.1跟Cygwin环境不兼容,直接pip安装lxml==3.7.3就可以了。
    原文作者:非梦nj
    原文地址: https://www.jianshu.com/p/28960f5b1464
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞