Find a lot on the internet did not find a good tutorial, or is a pen with, or is not clear, if the configuration server to do a two or three days or even a two weeks, for beginners is a huge blow, so summed up, hope to help the needy friends.
The main content of this article is to configure the Web site based on Flask Development, although the article title is the local site, but also for the remote server to configure the Flask site, the following is the list of items we need: can be networked PC or laptop, operating system for Ubuntu 16.04 or Ubuntu Server 16.04 (14.04 is OK, and then the lower version I haven't tried) an intellectually sound good-looking programmer or a beauty like a flower Cheng (joking:) Dynamic Web site working principle
Before explaining how to configure your site, let's take a look at how the Dynamic Web works, taking flask as an example:
As shown in the figure above, the general workflow of a dynamic Web site is as follows:
First, the browser (the user) initiates a request to the server (Apache), and after receiving the request, the request is distributed to the WSGI application (Flask application), WSGI the application responds to the request (such as an HTML page) and sends the response content to the server, The server then sends it over the network to the browser. Note that a server can refer to a physical machine (such as IBM xseries 346) or to a server software (such as Apache)
Installing the APACHE2 server
The Apahce name in Ubuntu is Apache2, the latest version is Apache 2.4, so the command to install apache2 is: sudo apt-get install apache2
After the apache2 is installed, let's test to see if Apache2 is working properly: start the APACHE2 server: sudo service apache2 start, or you can enter: Sudo/etc/init.d/apache2 restart Open the browser (my is chrome), enter localhost in the Address bar and press ENTER, if the following page appears, the installation was successful:
We can also stop Apache2 server: sudo service apache2 stop, after stopping to access localhost in the browser will not appear any pages. Install Mod_wsgi
MOD_WSGI installation is simple: sudo apt-get install libapache2-mod-wsgi install python, install flask in a virtual environment The Flask development team recommended that we use Python2 (Flask's support for Python3 is not good enough) to install Python is simple: sudo apt-get install python Installing the Python virtual environment and installing flask in a virtual environment can refer to my other article: Install Flask Configuration site in a python virtual environment
We know how to configure a virtual environment, the following to build a test site, Apache has a default site storage directory:/var/www, after the first successful installation of apache2, this directory has a default site called HTML directory, There's only one file: Index.html, which is the page that opens the localhost when we test it. The default structure for this site's hosting directory is as follows:
www
├──html
└──index.html
Now we want to create a new test site named Test in the WWW folder, where we created a directory named Test in the WWW folder (requires root permissions): sudo mkdir test, where the site is now hosting the directory is as follows:
www
├──html
│ └──index.html
├──test
After the test Site directory is created, to not affect the Python environment in the system, we need to install a Python virtual environment under the test directory: sudo virtualenv venv
Activating the virtual environment: source Venv/bin/activate
and install Flask:pip install flask in the virtual environment
Then we add two files to the test directory, one is APP.WSGI and the other is hello.py.
1, hello.py:
From flask import flask
app = Flask (__name__)
@app. Route ('/")
def Index (): Return
'
2, App.wsgi
Import sys
activate_this = '/var/www/mlfans/venv/bin/activate_this.py ' # startup script execfile for virtual environment
(activate_this , Dict (__file__=activate_this)) # Execute Virtual Environment boot script
sys.path.insert (0, '/var/www/mlfans ') from
Hello Import app as Application
The following configuration apache2: Open apache2 Site Configuration directory: cd/etc/apache2/sites-avalible Create a site profile named test.conf: sudo gedit test.conf Enter the following in test.conf:
<virtualhost *:80>
ServerName www.test.com
serveralias *.test.com wsgidaemonprocess Mlfans
Threads=5
wsgiscriptalias//var/www/mlfans/app.wsgi
<directory/var/www/mlfans>
Wsgiprocessgroup Mlfans
wsgiapplicationgroup%{global}
wsgiscriptreloading on order
Deny,allow
Allow from all
</directory>
</virtualhost>
Save the file test.conf, and then modify the Hosts file for the system: sudo gedit/etc/hosts
Add the following: 127.0.0.1 www.test.com, the modified Hosts file as shown in the following figure:
Then enter the command A2ensite test to take the site into effect
Restart Apahce2:sudo Service Apache2 Reload
Open the browser, enter test.com, if the following page appears, your test site configuration success.
Summary
Finally, let's comb through the structure of the entire test site:
First, the user uses the browser to initiate the access to the test.com, Apache receives the access request, discovers the test.com site configuration information in its own configuration file, test.conf file contains the location of the directory where the site resides, and the location of the Wsgi script, Apache2 calls the App.wsgi script, and the App.wsgi script calls hello.py and completes the request. After the production response is flask, it is returned to Apache2, which then apache2 the response back to the requesting client to complete a full access request. (The general process is so, the details may be different from the real situation)
Although this article is about the configuration of a local site, it is also useful for configuring a site that can be publicly accessed on a remote server.
In addition, if you do not have access to a configured local site, you can view the Apache2 error log, carefully troubleshoot where the error occurred, and view the error log commands as follows:
Cat/var/log/apache2/error.log
Carefully read the Traceback can be phased out to find out the cause of the error.
If there is still a problem, welcome to join Q Group 532232743, mutual exchange of learning, such as the text in the wrong place, welcome to comment criticism.