我试图创建一个在Windows 10上运行的服务器环境


目前,我正在制作可以在Web上运行的策略游戏,但由于遇到各种原因取消了租用的服务器,因此遇到了麻烦。

钱...我不知道... ;;;

因此,我的PC上装有Windows 10,但是我检查了是否可以在本地创建实验环境,并且当我实际尝试该环境时,我设法做到了,因此我将对其进行部署。
如果您有一台个人计算机,则可以在不花钱的情况下建立网络环境,因此即使遇到麻烦,也请尝试一下!

仅仅因为它是一个"环境",安全性几乎被忽略了。另外,在Windows 10上构建服务器并将其发布到Web上也违反了Microsoft的许可,因此您绝对不要这样做。
仅通过使用本地实验环境。

目录

  • 前提
  • 操作图
  • 环境

    • nginx安装
    • python安装
    • uwsgi安装
    • postgreSQL安装
    • 测试源准备
    • Cygwin测试
    • 颁发SSL证书
    • Nginx设置
  • 开始和结束
  • 引用网站?

假设

适用于从Web浏览器键入的应用程序。基本上,这是使用HTML5和Javascript进行的前端操作,但假定复杂的计算和输出将由Python在虚拟服务器上处理。也可以处理数据库。

软件要求
* Windows10
* python3
* nginx(Web服务器)
* PostgreSQL(数据库服务器)
* uWsgi(网络→python执行应用程序)
* cygwin(uwsgi安装环境执行环境)
*操作系统默认编码:utf-8

nginx具有与Windows兼容的版本,请使用该版本。
而且,flask被用作uwsgi和nginx之间的框架。

操作图像

上面有描述,虚拟服务器中使用的uwsgi在Windows上受支持,但实际上不能在Windows上直接执行(它不起作用...)。而是可以在Windows上运行的终端软件" cygwin"上虚拟运行uwsgi。
流程是通过nginx将输入操作从Web传递到Cygwin,处理Python和PostgreSQL并将其返回给nginx的图像。

作为

图像,cygwin是uwsgi和postgreSQL服务器。
20200831003334.jpg

环境建设

实际设置环境。在这里,安装以下应用程序。
* Nginx
* python3
* uwsgi
* postgreSQL

1.安装nginx。
从以下位置获取档案:我基本上使用稳定版本。将获得的存档解压缩到您选择的位置。
Nginx HP

1-1。在目标nginx-X.X.X中启动命令提示符。
1-2。开始输入nginx执行。
1-3。在浏览器中显示http://本地主机。
如果被防火墙阻止,则允许。如果显示" Welcome to nginx!",则表示设置正常。
输入1-4.nginx -s quit退出。

2.安装python和所需的库。
从以下位置获取安装程序:基本上使用*基于Web的安装程序。使用安装程序在任何您喜欢的地方进行设置。
Python HP

将Python x.x添加到Path应该被检查。对于其他用户,请根据您的环境选择默认值或选项。安装后让我们对其进行测试。

1
2
3
4
5
6
7
8
9
# python -V
Python 3.8.2
  ※Windowsの場合、python3ではなく、pythonらしいです  

# pip3 install flask requests requests_oauthlib python-dateutil psycopg2
~中略~

# pip3 list
~以下省略~

3.安装uwsgi。
在这里,我们将使用Cygwin Terminal将其安装在Linux仿真器上。使用安装程序在任何您喜欢的地方进行设置。
Cygwin HP

包选择以下内容(请注意,如果不选择它,它将被跳过)。
* python3-pip
* wget
* libcrypt-devel
* libintl-devel
* libssl-dev
* libreadline-deval
* zlib
* zlib-devel
*制作
* gcc-core
*解压
* gettext
* Python3被复制为第2项,但由于它是Cygwin环境的安装,因此也适用于此。
*如果您未选择上述所有库,则将无法正常设置。

根据您的环境选择默认值或选项。虚拟环境可以在当前设置下。

设置后,启动cygwin并设置uwsgi。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ホームユーザ名を確認する  以後[Cygwinユーザ]で統一します  
# echo $USER
※デフォルトだとWindowsのログインユーザ名(フォルダ)と同一名になります

wgetでapt-cygを取得する
$ wget https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg
$ chmod 755 apt-cyg
$ mv apt-cyg /usr/local/bin

システム系コマンドのために
$ apt-cyg install procps

python3のライブラリを入手する
$ apt-cyg install python3-devel

从git下载uwsgi并进行设置。

1
2
3
4
5
6
$ wget https://github.com/unbit/uwsgi/archive/[リリースアーカイブ名]
$ unzip [アーカイブ名]
$ cd uwsgi-[バージョン名]
$ python3 setup.py install
  ※アーカイブはリリースバージョンを指定します  
  ※コンパイルが動作するのでかなり時間がかかります

顺便说一句,以防万一,请检查默认编码。如果它是基于Linux的,则uft-8是基本的,但是Cygwin默认情况下是utf-8,因此应该没有问题。

1
2
3
4
5
6
$ python3
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>> exit
[ctrl+D] ※キー入力で終了

如果不是utf-8怎么办?将编码添加到配置文件。

1
2
3
4
5
$ vi /home/[Cygwinユーザ]/.bash_profile

export LANG=ja_JP.UTF-8

:wq

添加后,执行以下步骤加载它。

1
$ source ~/.bash_profile

4.安装postgreSQL。

4-1。从以下代码中获取源代码。
PostgreSQL惠普
选择.qz格式。

1
$ wget [ソースコードのURL]

4-2。编译并安装源代码。

1
2
3
4
5
6
7
8
9
10
$ tar -xvzf [アーカイブファイル名]
$ cd [解凍されたフォルダ]
$ mkdir make_dir
$ cd make_dir
$ ../configure --enable-nls --enable-thread-safety
$ make
$ make install
$ cd src/interfaces/libpq
$ make
$ make install

4-3。将环境变量添加到.bash_profile。

1
2
3
4
5
6
7
8
$ vi /home/[Cygwinユーザ]/.bash_profile

export PATH=/usr/local/pgsql/bin:/usr/local/pgsql/lib:$PATH
export PGHOST=localhost
export PGLIB=/usr/local/pgsql/lib
export PGDATA=/usr/local/pgsql/data

:wq

添加后,执行以下步骤加载它。

1
2
source ~/.bashrc
source ~/.bash_profile

4-4。初始设置。

1
2
$ /usr/sbin/cygserver.exe &
$ initdb -U postgres

这会将postgreSQL超级用户名更改为[postgres]。

4-5。启动postgreSQL服务器并检查是否可以登录。

1
2
3
4
5
6
7
8
$ pg_ctl start
$ pg_ctl status
$ psql -l
$ psql postgres -U postgres

=>

=> \q

如果没有错误,则可以。

4-6。登录接下来,设置超级用户密码,创建数据库并执行简单的密码身份验证。
*如果可能,建议使用Peer和md5
*假定此示例在"星区"中使用。用户名和数据库名将为[strgdb]。
*当然,如果您忘记了密码,将无法访问它,所以请不要忘记密码!我不能承担责任。 (尽管可以原样重设)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$ createuser strgdb -U postgres
$ createdb -O strgdb strgdb -U postgres

スーパーユーザ[postgres]でログインする
$ psql postgres -U postgres

スーパーユーザ[postgres]にパスワードを設定する
=> alter role postgres with password '[postgresのマスタパスワード]';  

Star Region用ユーザ[strgdb]にパスワードを設定する。
=> alter role strgdb with password '[DBパスワード]';  
=> alter role strgdb with login;  
=> \q  

  アクセス権限ファイルを編集します。  
$ vi /etc/local/pgsql/data/pg_hba.conf

L95から以下のように設定する  trust→passwordへ

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
###local   all             all                                     trust
local   all             all                                     password
# IPv4 local connections:
###host    all             all             127.0.0.1/32            trust
host    all             all             127.0.0.1/32            password
# IPv6 local connections:
###host    all             all             ::1/128                 trust
host    all             all             ::1/128                 password
# Allow replication connections from localhost, by a user with the
# replication privilege.
###local   replication     all                                     trust
###host    replication     all             127.0.0.1/32            trust
###host    replication     all             ::1/128                 trust

:wq

終わったらpg_sqlをリスタートします  
$ pg_ctl start

$ psql postgres -U postgres
Password for user postgres:  ※スーパーユーザのパスワードを入力

=>

=> \q

$ psql strgdb -U strgdb
Password for user strgdb:  ※strgdbユーザのパスワードを入力
=>
=> \q
ログインできればOKです。

5.准备测试源。

1
2
3
4
5
6
7
8
9
10
11
12
13
nginx(Windows)側
/www/index.html
 適当に。

uwsgi(Cygwin)側
/home/[Lucida(ユーザ名)]/uwsgi/test.py
 pythonテストソース

#!/usr/bin/python
# coding: UTF-8
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

6.启动cygwin并测试启动uwsgi服务器。

1
2
3
4
uwsgi --http :9090 --wsgi-file uwsgi/test.py
*** Starting uWSGI 2.0.18 (64bit) on [Tue Aug 25 17:58:48 2020] ***
~省略~
*** uWSGI is running in multiple interpreter mode ***

如果被防火墙阻止则允许。
在浏览器中访问以下内容。

1
http://localhost:9090

如果显示

Hello World,则使用Ctrl C停止uwsgi服务器。

8.颁发SSL证书。
使用SSL需要以下证书。
*根CA证书(适用于客户端)
*中级CA证书
*服务器证书

如果您没有这些证书,则可以购买并获得一个证书,也可以颁发自签名证书。下面显示了颁发自签名证书的过程。

8-1。使用文本编辑器打开openssl.cnf文件,然后编辑以下内容。
[Cygwin安装文件夹] /etc/pki/tls/openssl.cnf

1
2
3
4
5
6
7
L91 : default_days    = 825

L150: countryName_default = JP
L155: stateOrProvinceName_default = XXXXXX  ※都道府県
L158: localityName_default        = XXXXXX  ※市町村
L161: 0.organizationName_default  = XXXXXX  ※組織名
※opensslのバージョンによって行が変わるようです

8-2。创建证书颁发机构和根CA证书的私钥。
它将与cygwin一起使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ mkdir /etc/pki/CA
$ chmod 700 /etc/pki/CA
$ cd /etc/pki/CA
$ openssl genrsa -des3 -out ca.key 2048
Enter pass phrase for ca.key:                         ※認証局のパス 適当に
Verifying - Enter pass phrase for ca.key:             ※認証局のパス 上と一緒

$ openssl req -x509 -new -nodes -key ca.key -sha256 -out ca.pem
Enter pass phrase for ca.key:                         ※認証局のパス 上と一緒
~省略~
Common Name (eg, your name or your server's hostname) []:  ※認証局の名前 適当に
                                                      ※それ以外は未入力
~省略~

8-3。在客户端上安装证书颁发机构证书。
在Windows→设置中,搜索"证书"。打开用户证书管理。
右键单击受信任的根证书颁发机构,所有任务→导入。
选择当前用户ca.pem,选择根证书颁发机构,然后单击

8-4。创建服务器证书。
它将与cygwin一起使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ mkdir /etc/pki/Server
$ chmod 700 /etc/pki/Server
$ cd /etc/pki/Server
$ openssl genrsa -out server.key 2048
$ openssl req -new -key server.key -out server.csr
~省略~
Common Name (eg, your name or your server's hostname) []:  ※IPアドレスやホスト名 適当に
                                                      ※それ以外は未入力
~省略~
$ vi server.txt
---------
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost
---------\wq
$ openssl x509 -req -in server.csr -CA ../CA/ca.pem -CAkey ../CA/ca.key -CAcreateserial -out server.crt -sha256 -extfile server.txt
Enter pass phrase for ../CA/ca.key:                   ※認証局のパス

9. nginx Config设置
9-1在nginx中调整nginx.config。
[Nginx安装文件夹] /config/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#user  nobody;
worker_processes auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile 2048;
events {
### worker_connections 1024;
    worker_connections 2048;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include [HTMLソースフォルダ]/source/nginx/*.conf;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    include [HTMLソースフォルダ]/source/nginx/sites-enabled/*;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ※この2行を追加する

###    server {
###        listen       80;
###        server_name  localhost;
###
###        #charset koi8-r;
###
###        #access_log  logs/host.access.log  main;
###
###        location / {
###            root   html;
###            index  index.html index.htm;
###        }
###
###        #error_page  404              /404.html;
###
###        # redirect server error pages to the static page /50x.html
###        #
###        error_page   500 502 503 504  /50x.html;
###        location = /50x.html {
###            root   html;
###        }

~省略~
    }
~省略~
}

9-2。为nginx服务器准备配置。准备与默认文件不同的文件。
[适当的文件夹] /nginx/https.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
server {
    listen 443 ssl http2;
    server_name localhost;

    ssl_protocols TLSv1.2;
    ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
    ssl_ecdh_curve prime256v1;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_certificate [Cygwinインストールフォルダ]/etc/pki/Server/server.crt;
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ サーバ証明書
    ssl_certificate_key [Cygwinインストールフォルダ]/etc/pki/Server/server.key;
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ サーバ秘密鍵

    keepalive_timeout 70;
    sendfile on;
    client_max_body_size 0;
    root [HTMLソースフォルダ]
       ~~~~~~~~~~~~~~~~~~~~~
    server_tokens off;
    charset utf-8;

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        index index.html;
    }

    location /uwsgi {
        include uwsgi_params;
        proxy_pass https://127.0.0.1:9090;
    }

}

9-3。测试启动服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cygwinを起動する。  
$ uwsgi --master --https 127.0.0.1:9090,/etc/pki/Server/server.crt,/etc/pki/Server/server.key --wsgi-file strg_uwsgi/test.py

WindowsのDOSプロンプトでnginxを起動する。
$ [nginxのインストールドライブ]:
$ cd [nginxのインストールフォルダ]
$ start nginx

ブラウザで次の2か所にアクセスします。
 https://localhost/
 Webページのトップが表示されればOKです。

 https://localhost/uwsgi
  Hello Worldが表示されればOKです。  

一旦nginxとuwsgiサーバを停止する。uwsgiはCtrl+Cで停止する。  
$ nginx -s quit

这就是

的全部。然后做任何你想做的。

开始和结束

启动是以下命令。

1
2
3
4
5
6
7
8
nginx(Windows)側
# start nginx

cygwin側
# /usr/sbin/cygserver.exe &
# pg_ctl start
# pg_ctl status
# uwsgi --master --https 127.0.0.1:9090,/etc/pki/Server/server.crt,/etc/pki/Server/server.key --wsgi-file uwsgi/test.py

退出是以下命令。

1
2
3
4
5
6
7
8
9
nginx(Windows)側
# nginx -s stop

cygwin側
※Ctrl+C を押す
# pg_ctl stop
# pg_ctl status

Cygwinを終了する

引用网站?第

下面省略了标题。

  • 在Windows上设置uwsgi(在其他说明中)
  • 启动cygserver.exe(WinOfSQL)
  • 创建不会产生SSL错误的SSL自签名证书的过程(snyomo)