nginx + uwsgi + flask 补充内容

于 2024年6月1号开始把的云主机从张北迁移到杭州来,因为 OS 是从 6U 升级到 阿里云的龙蜥OS 8u,所以,光 WordPress 就折腾了1天多,终于搞定。

然后,记起来以前也通过 php 的 server 支持了 ios 的 API服务,所以,尝试把 ios 的服务端切换到 flask + python 来。

继续研究了 2018年的在博客记录的 flask 的配置,稍作了修改,如下:

1: 首先,使用一个域名 cmesoft.com 提供服务。

2:其次,配置 /etc/nginx/conf.d/cmesoft.conf 支持 WordPress站点,内容如下:

server {
	    	listen       80;                        # 监听端口
		server_name www.cmesoft.com cmesoft.com;    # 站点域名
		#server_name  121.199.16.184 121.199.16.184;
			client_max_body_size 50M;
	                  # 站点根目录

	    	
	       location / {
		       		#index index.html index.htm index.php;   # 默认导航页
				root  /data/web/cmesoft;    
				index  index.php index.html index.htm;
						        }

	           location ~ \.php$ {

			        fastcgi_pass 127.0.0.1:9000;
				#fastcgi_index index.php;
				#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
				fastcgi_param  SCRIPT_FILENAME  /data/web/cmesoft/$fastcgi_script_name;
				include        fastcgi_params;
			}
}

3:因为 wordpress 的页面放在: /data/web/cmesoft 下,而 python api的代码放在 /data/web/pyapi 底下,所以,为了把服务隔离开,不同的目录,不同额端口,给 pyapi 配置另外一个服务端口。

4. pyapi 的 nginx 配置如下:

cat  /etc/nginx/conf.d/pyapi.conf 
server {
	listen       8080 default_server;
	server_name  _;

	location ~ /pyapi { 
		proxy_pass http://127.0.0.1:8888;
			}

		error_page 404 /404.html;
		location = /40x.html {
				}

		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
			}

}

让 pyapi 监听 8080 端口,转到后端 flask 的 8888 端口,uwsgi 配置如下:

cat uwsgi.ini
[uwsgi]
## For directlly http access
http-socket=127.0.0.1:8888

### For nginx proxy
#socket=/tmp/ppt.sock

wsgi-file=/data/web/pyapi/server.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/web/pyapi/
processes = 4
threads = 4
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi
daemonize = uwsgi.log
pidfile = uwsgi.pid

然后,/data/web/pyapi/server.py 内容如下:

cat server.py 
from flask import Flask,render_template, jsonify
from flask import request

app = Flask(__name__)

@app.route('/pyapi', methods=['GET', 'POST'])
def index():
    requester = request.remote_addr
    #logging.info('Data request from: %s' % requester)
    url = request.url
    uid = url.split('uid=')[-1]
    get_name=request.args.get("name")
    post_name=request.form.get("name")
                                            #return "Test message, your input uid=%s, method=%s, get_name=%s, post_name=%s  from %s\n" % (uid, request.method, get_name, post_name, requester)
                                            
    return jsonify(get_name=get_name,post_name=post_name)

if __name__ == '__main__':
    app.run()

在阿里云的ECS上开启对外的 8080 端口服务,测试如下:

# curl -s "cmesoft.com:8080/pyapi?uid=3434&name=get_Wade" -d "name=post_james" | jq
{
  "get_name": "get_Wade",
  "post_name": "post_james"
}

从代码和测试可以看到,我们的 FLASK API 接受 GET 和 POST 调用,可以作为一个小小的 服务端 API 网关。

今天就更新到这里。 —- 2024-06-02 22:20 分。

Posted in 运维相关 | nginx + uwsgi + flask 补充内容已关闭评论

mysql几个常用知识【2007-11-20 19:14:22】

(1)一般情况下,我们在后台直接访问数据库的机会很少,大多数时间都是通过工具如mysqladmin或者phpadmin远程访问mysql数据库服务器的,以mysqladmin为例。
如果想从192.168.1.102上以cme身份访问192.168.1.10上的mysql数据库服务,那么就需要在数据库服务器上添加这个账户。
首先,登录数据库后台:
mysql -uroot -plinuxmysql
登录到数据库提示符,然后创建cme用户,指定主机为192.168.1.102,从192.168.1.102以cme用户名登录到这台数据库服务器的密码为‘windowsmysql’,如下:
mysql>CREATE USER cme@192.168.1.102 IDENTIFIED BY ‘windowsmysql’;
mysql>exit;
这个时候,在192.168.1.102上用mysqladmin连接192.168.1.10上的mysql服务,默认端口为3306,不需要修改。
我们可以看到,很快就连接上了,如果有问题请检查防火墙设置等问题。
然后,右键创建数据库,你会发现弹出对话框提示不能创建,因为还没有给cme分配数据库的操作权限,所以下来需要给cme@192.168.1.102分配权限。
mysql>GRANT CREATE ON . TO cme@192.168.1.102 WITH GRANT OPTION;
给cme@192.168.1.102分配了create database的权限,然后我们退出mysqladmin后再次用mysqladmin连接上,创建数据库,成功,相应的,如果你还想使得在cme@192.168.1.102访问数据库时拥有跟多权限,就需要给它grant跟多的操作,如INSERT, DROP, UPDAE等。ON ‘数据库’表示对某个数据库的操作。
一般都是分配所有的权限,比如
mysql > GRANT ALL PRIVILEGES ON . TO cme@192.168.1.102 WITH GRANT OPTION
这样cme@192.168.1.102登录到192.168.1.10上的数据库后就能进行任何操作了。
如果你想在任何机子上都能登录数据库服务器并进行操作,可以如下写:
mysql>RANT ALL PRIVILEGES ON . TO cme@”%” [IDENTIFIED BY ‘windowsmysql’] WITH GRANT OPTION
如果已经设置了密码就没有必要再写上[IDENTIFIED BY ’windowsmysql’]了。
然后就可以尽情的访问了
一般情况下需要以root超级用户登录到数据库上设置其他超级用户的权限。
所以,我的数据库一般都设置为
mysql>
mysql>RANT ALL PRIVILEGES ON . TO root@”%” [IDENTIFIED BY ‘windowsmysql’] WITH GRANT OPTION
直接使用root进行访问。
注意,我们在这里给root grant的密码跟root直接从后台登陆的密码并不一定一致。linuxmysql是在localhost上登录时root的密码,windowsmysql是从任意主机登录时的密码(或者你也可以针对不同的客户端给root分配不同的连接密码,比如192.168.1.102连接到数据库的root密码为123455,而192.168.1.104以root连接数据库时密码可以为54321等等),这个我们可以从mysql的user表中的数据得到验证。
mysql>use mysql;
mysql> select Host, User, PassWord from user;
+———————–+——+——————————+
| Host | User | PassWord |
+———————–+——+—————–+
| localhost | root | *0E2B5059346AC0140970837959E5F4B3BA8E96E0 |
| localhost.localdomain | root | *0E2B5059346AC0140970837959E5F4B3BA8E96E0 |
| 127.0.0.1 | root | *0E2B5059346AC0140970837959E5F4B3BA8E96E0 |
| % | root | *6C24E7AC61619C4631613121B51F419DAA626E5E |
| 192.168.1.102 | cme | *6C24E7AC61619C4631613121B51F419DAA626E5E |
+———————–+——+——————————————-+
我们能看到,虽然密码加密了,但是不同的Host还是有不同的密码的。
(2)root密码忘记后恢复。
这个只能在后台直接修改了,远程就不行了。
先停止数据库服务
service mysqld stop
然后启动msyql,跳过身份验证。
#/usr/local/mysql/bin/mysqld_safe –skip-grant-tables &
然后mysql -uroot登录就不需要输入密码了
mysql>update mysql.user set password=PASSWORD(‘nothing’) where (User=’root’) and (Host = ‘localhost’);
mysql>flush privileges;
mysql>exit;
注意:如果你不加上Host = “localhost”的话,就改了所有root的密码
即192.168.1.102登录mysql的密码也被改了,这个要慎重修改喔。
然后再次连接就OK了
#mysql -uroot -pnothing
正常登录了。

(2)

Posted in 运维相关 | mysql几个常用知识【2007-11-20 19:14:22】已关闭评论

sun880服务器安装sparc solaris9以及网络配置-[2006-11-22 15:31:29]

昨天下午进驻机房安装sparc Solaris9,目标机是一台sun880的服务器
4G内存,6个70G的硬盘。到刚才终于把网络配置好了,赶紧收藏一下经验!
记录一些关键点:
(1):机子启动后同时按 stop + a 这两个键,然后输入boot cdrom,会进入光盘安装模式,这个不知道就惨了。
(2):语言选择 Chinese-GBXXX,不要选chinese utfXXX之类的
(3):网络不要选nis,dns之类的,选择none
(4):网络设备选择eri0或者hme0,但是不要选择ge0,否则会遇到我们今天遇到的问题,后面再说。
(5):分区时,先让系统自己分,然后手动调整,但是记住/export/home要分最大的,剩下的空间都给它。如果多个磁盘不支持raid,就分成/export/home0 /export/home1 ==。
(6):流程 disk1(1/2 software) installing ->auto reboot->disk2(2/2 software)->disk3(language)
(7):安装完成
如果网络配置好但是却不通,就要检测启用的设备是否正确
我在安装时选择了ge0,但是怎么都ping不通内网
最后看帖子发现是设备选错了,修改如下
ifconfig ge0 down
ifconfig ge0 unplumb
ifconfig eri0 plumb
ifconfig eri0 up
…配置参数
但是默认的配置再Reboot后又会恢复,所以写个脚本让它开机自己修改
以我们的Solaris9 图形登陆方式为例
在/etc/rc3.d下建立一个文件S51network,注意你的机子不一定是这个目录这个文件,要根据具体情况进行判断。
内容为:
ifconfig ge0 down
ifconfig ge0 unplumb
ifconfig eri0 plumb
ifconfig eri0 up
ifconfig eri0 10.0.0.58 netmask 255.255.0.0
route add default 10.0.254.254
如果你想在开机时和关机时各执行不通的任务,那就加几句:
case “$1” in
‘start’)
开机做的事情
;;
‘stop’)
关机做的事情
;;
*)
其他的
;;
esac
exit 0
比较容易理解,这个就跟Linux的chkconfig的原理一样。
然后 reboot就行了
。。。。。
估计还要装gcc 和 glibc,有空再加上来吧。(瞌睡死了,阿米豆腐)

Posted in 运维相关 | sun880服务器安装sparc solaris9以及网络配置-[2006-11-22 15:31:29]已关闭评论

python 的 yaml 操作例子

依赖


rpm -qa | grep PyYAML
PyYAML-3.10-3.1.el6.x86_64

例子代码


#!/usr/bin/python
import os, sys,yaml

fname="index.yaml"

f = open(fname,"r")
x=yaml.load(f)

#print(x)

dist={}

dlist="centos6.x86_64|centos7.x86_64"
arr = dlist.split("|")

for ite in arr:
dist[ite] = {}
dist[ite]['release'] = "{{.gitcount}}.{{.dist}}"
x['distribution'] = dist
#print "\n\n"
#print x
#print "\n"
ret = yaml.dump(x)
print ret

Posted in 运维相关 | 2 Comments

wordpress 使用ftp安装插件时权限不足问题解决

使用FTP软件连接FTP空间,进入wp-content目录,新建tmp文件夹,设置文件夹的权限为777

下载:在FTP中返回网站根目录,找到wp-config.php这个PHP文件,下载到本地

修改:在wp-config.php中添加下列三行代码:

define(‘WP_TEMP_DIR’, ABSPATH.’wp-content/tmp’);/* WordPress的临时目录。*/

define(“FS_METHOD”, “direct”);

define(“FS_CHMOD_DIR”, 0777);

define(“FS_CHMOD_FILE”, 0777);

注意:要在定义ABSPATH的后面,即在它的后面添加

if ( !defined(‘ABSPATH’) )

define(‘ABSPATH’, dirname(FILE) . ‘/’);

================================

最后的代码应该是:

if ( !defined(‘ABSPATH’) )

    define('ABSPATH', dirname(__FILE__) . '/');

define(‘WP_TEMP_DIR’, ABSPATH.’wp-content/tmp’);

define(“FS_METHOD”, “direct”);

define(“FS_CHMOD_DIR”, 0777);

define(“FS_CHMOD_FILE”, 0777);

Posted in 运维相关 | wordpress 使用ftp安装插件时权限不足问题解决已关闭评论

centos 配置 nginx 和 uwsgi 运行 python 脚本

安装 uwsgit

pip install uwsgi

添加响应文件。

vim /data/install/bin/pptserver.py

内容是:

cat /data/install/bin/pptserver.py 
from flask import Flask,render_template

app = Flask(__name__)

@app.route('/pptserver')
def index():
 return "Test message, you get it"

if __name__ == '__main__':
 app.run()

然后配置文件修改:

cat /data/install/conf/uwsgi.conf

[root@iZ23srfk8ttZ bin]# cat /data/install/conf/uwsgi.conf 
[uwsgi]
## For directlly http access
http-socket=127.0.0.1:8888

### For nginx proxy
#socket=127.0.0.1:8888
wsgi-file=/data/install/bin/pptserver.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/install/conf/
processes = 2
threads = 2
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi

运行 uwsgi:
注意:当直接curl测试uwsgi的时候,配置为 http-socket=xxx
当使用nginx进行代理时,要配置为 socket=xxx

nohup uwsgi --ini /data/install/conf/uwsgi.conf &

检查服务:

#netstat -atnp | grep uwsgi
tcp        0      0 127.0.0.1:9191              0.0.0.0:*                   LISTEN      584/uwsgi           
tcp        0      0 127.0.0.1:8888              0.0.0.0:*                   LISTEN      584/uwsgi 

可以看到 9191端口是健康检查服务, 8888 端口是服务端口

测试下业务口:

#curl http://localhost:8888/pptserver
Test message, you get it

测试监控

#curl "http://localhost:9191"
 "version":"2.0.17.1",
 "listen_queue":0,
 "listen_queue_errors":0,
 "signal_queue":0,
 "load":0,
 "pid":584,
 "uid":48,
 "gid":0,
 "cwd":"/root",
 "locks":[
  {
   "user 0":0
  },
  {
   "signal":0
  },
  {
   "filemon":0
  },
  {
   "timer":0
  },
  {
   "rbtimer":0
  },
  {
   "cron":0
  },
  {
   "rpc":0
  },
  {
   "snmp":0
  }
 ],
 "sockets":[
  {
   "name":"127.0.0.1:8888",
   "proto":"http",
   "queue":0,
   "max_queue":100,
   "shared":0,
   "can_offload":0
  }
 ],
 "workers":[
  {
   "id":1,
   "pid":589,
   "accepting":1,
   "requests":0,
   "delta_requests":0,
   "exceptions":0,
   "harakiri_count":0,
   "signals":0,
   "signal_queue":0,
   "status":"idle",
   "rss":0,
   "vsz":0,
   "running_time":0,
   "last_spawn":1532316757,
   "respawn_count":1,
   "tx":0,
   "avg_rt":0,
   "apps":[
    {
     "id":0,
     "modifier1":0,
     "mountpoint":"",
     "startup_time":0,
     "requests":0,
     "exceptions":0,
     "chdir":""
    }
   ],
   "cores":[
    {
     "id":0,
     "requests":0,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    },
    {
     "id":1,
     "requests":0,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    }
   ]
  },
  {
   "id":2,
   "pid":590,
   "accepting":1,
   "requests":2,
   "delta_requests":2,
   "exceptions":0,
   "harakiri_count":0,
   "signals":0,
   "signal_queue":0,
   "status":"idle",
   "rss":0,
   "vsz":0,
   "running_time":2914,
   "last_spawn":1532316757,
   "respawn_count":1,
   "tx":206,
   "avg_rt":889,
   "apps":[
    {
     "id":0,
     "modifier1":0,
     "mountpoint":"",
     "startup_time":0,
     "requests":2,
     "exceptions":0,
     "chdir":""
    }
   ],
   "cores":[
    {
     "id":0,
     "requests":1,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    },
    {
     "id":1,
     "requests":1,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    }
   ]
  }
 ]
curl: (56) Failure when receiving data from the peer
}

配置 nginx 转发

配置 nginx 文件内容为:

location ~ /pptserver {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:8888; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
    }

修改 uwsgi的配置为:

[root@iZ23srfk8ttZ bin]# cat /data/install/conf/uwsgi.conf 
[uwsgi]
## For directlly http access
#http-socket=127.0.0.1:8888

### For nginx proxy
socket=127.0.0.1:8888
wsgi-file=/data/install/bin/pptserver.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/install/conf/
processes = 2
threads = 2
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi

然后测试nginx的接口:

curl "http://ginkgoo.org/pptserver"
Test message, you get it
Posted in 运维相关 | centos 配置 nginx 和 uwsgi 运行 python 脚本已关闭评论

wordpress 手动升级

缘由

   有些服务器不能打开 ftp 服务来升级,或者升级失败,就需要手动来升级。

怎么升级

WordPress 本身升级

wordpress 本身升级比较容易,手动 wget 下载 zip 包,解压缩后,记得删除里面的 wp-content 目录,然后 rsync 到已经部署的目录中即可。

当你刷新页面时,一般会提示升级数据库,很快就能完成升级。

插件升级

插件很少的化,直接手动下载 zip 包,解压缩,然后 rsync 到  wordpress 目录下的 wp-content/plugins 目录里,覆盖即可。

刷新页面会提示最新版升级完成。

主题升级

主题的升级很简单,下载文件,解压缩,然后 rsync 到 wp-content/themes 目录下即可,如果 主体比较多,可以复制下载链接,使用如下的脚本来升级。

#!/bin/bash
TOPDIR="/data/web/cmesoft"
cat them.txt | while read url
do
	fname=`basename $url`
	wget $url
	if ! [ -f "$fname" ]
	then
		echo "wget $url failed"
		continue
	fi
	ndir=`echo $fname | awk '{print substr($1,1, index($1, ".")-1)}'`
	if [ -d "./$ndir" ]
	then
		rm -fr "./$ndir"
	fi
	unzip $fname
	cmd="rsync -avz $ndir $TOPDIR/wp-content/themes/"
	echo "Run command:$cmd"
	$cmd
done

实际运行时,注意修改 TOPDIR 为你部署wordpress 的目录。

Posted in 运维相关 | 1 Comment