Refer to this article, thanks to the original 78929201
Recently learning Python, how do I deploy Python to a Web server except to write a little py script that runs on Idlex?
My Computer environment: WIN7 64-bit flagship version, has been writing PHP scripts with Phpstudy, so the computer installed phpstudy2016, this is an old version, has not been updated to 2017 and 2018, which is not the focus;
1.Install Python, I previously installed Winpython this package, when the installation of the version is 3.5.3,1g, the content is full, including the PIP; View native Python version and PIP version:
c:\users\administrator>python--version3.5.3
C:\users\administrator>Pip-V10.0.1 from D:\WinPython\python-3.5.3.amd64\lib\site-packages\ Pip (Python 3.5)
2. Install the Flup, this now does not need to download to install, directly through the PIP can be installed:
c:\users\administrator>pip install Flup
The process is very smooth:
collecting Flup Downloading https://files.pythonhosted.org/packages/88/e5/ 17bcf4431e811ffaec213feea7609a6f003084006d2e210f53cee09095d9/flup-1.0.3-py3-none-ANY.WHL (74kB) 100% |████████████████████████████████| 81kB 64kb/sinstalling collected packages:flupsuccessfully installed Flup-1.0.3
After loading, verify that the success, enter the line of Code Red Word return, see the last black bold word that line, that the installation is successful:
C:\users\administrator> python python3.5.3 (v3.5.3:1880cb95a742, Jan, 16:02:32) [MSC v.1900 64bit (AMD64)] on Win32type" Help","Copyright","credits"Or"License" forMore information.>>> import flup;print (flup.__path__)
[' D:\\winpython\\python -3.5.3.amd64\\lib\\site-packages\\flup ']
3. install Nginx, because my machine has been installed Phpstudy, has come with nginx, feel free to skip this step
Nginx Official website: http://nginx.org/en/download.html
Download the latest version of Nginx on Windows, Http://nginx.org/download/nginx-1.15.0.zip decompression can be;
After running Nginx.exe, the local browser opens http://localhost or http://127.0.0.1 will see the Welcome page, which proves that Nginx is running well locally, if the Welcome page does not appear, Check to see if there are other Web servers that are using port 80.
4. Install Django, I looked back, as if this can also be installed through the PIP, but I was installed through the download package,
Official address: https://www.djangoproject.com/download/
The latest version is now: https://www.djangoproject.com/m/releases/2.0/Django-2.0.6.tar.gz
PIP command-line installation:
Pip Install django==2.0.6
I was downloaded back to install
Unzip after download, e.g. extract to D:\MyPython\Django\Django-1.10.8\
Run CMD Open the console window and switch to D:\MyPython\Django\Django-1.10.8\
d:\mypython\django\django-1.10.8>python setup.py install
After a bunch of characters flashed, there were no error prompts,
After the installation is complete, start Python, enter >>>import Django;print (Django. VERSION)
See the following tips: (1, 8, ' final ', 0) that the Django installation was successful
Then add the Django path to the Windows system variable path, and you can execute the script directly under any path:
d:\winpython\python-3.5.3.amd64\lib\site-packages\django-1.10.8-py3.5.egg\django\bin> It's the Django-installed folder on my computer.
d:\winpython\python-3.5.3.amd64\lib\site-packages\django-1.10.8-py3.5.egg\django\bin> Python django-admin.py- HType ' Django-admin.py Help <subcommand> ' forHelp on a specific subcommand. Available Subcommands:[django] Check compilemessages createcachetable Dbshell diffsettings dumpdata F Lush Inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail Shell SHOWM Igrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test Testser Vernote that only Django core commands is listed as settings is not properly configured (error:requested setting instal Led_apps, but settings is not configured. You must either define the environment variable Django_settings_module or call Settings.configure () before accessing Setti NGS.).
5. Configure the Nginx nginx.conf file
Open the Nginx.conf file, add the following, and then shut down the restart Nginx server, Web listening 3333 port, fastcgi listening 8090 port, root is the Web root directory, I continue to use the Phpstudy Web directory:
server { Listen 3333; server_name localhost; #charset Koi8-r; #access_log logs/host.access.log main; Location/{ root d:/phpstudy/www; Index index.html index.htm; Include fastcgi.conf; Fastcgi_param script_filename $fastcgi _script_name; Fastcgi_param path_info $fastcgi _script_name; Fastcgi_pass 127.0.0.1:8090; } }
6. Write a helloworld.py file in any text editor, put it under the Web root, and I'll put it under d:/phpstudy/www/ , and take note of Python's strict indentation syntax.
#!/usr/bin/python#Encoding:utf-8 fromflup.server.fcgiImportWsgiserverdefMyApp (environ, start_response): Start_response ('OK', [('Content-type','Text/plain')]) return['Hello world!\n']if __name__=='__main__': Wsgiserver (myapp,bindaddress=('127.0.0.1', 8090)). Run ()
Execute the following command to start HelloWorld,D:\phpStudy\WWW\helloWorld.py
Python D:\phpStudy\WWW\helloWorld.py 9001 fastcgi
Come out a black window, do not close it, use your browser input http://127.0.0.1:3333/ because My Computer 80 and 8080 are occupied, so I use is 3333 port
You can see the right results.
Windows7 Platform Nginx+python Environment construction