Mac install Apache Mod_wsgi module error Summary and Hello World test

Source: Internet
Author: User
Tags fully qualified domain name

Mac system version is OS X EI Capitan 10.11.4
(In fact, the system has installed the administrator rights of Apache, under/etc/apache2, but we still want to install the user rights of Apache, convenient to change the file does not require permission)

The default is Apache with user rights installed on the Mac. (if not installed, Portal: Apache installing

Some minor problems with Apache installation

I'll briefly talk about the problems encountered during the installation of Apache:

    • If you encounter oserror or write errors, it is generally not the root directory of write permissions, using the sudo command to obtain permissions.
    • Configure:error:c compiler cannot create executables solution Portal
    • Usually in the process of installation will encounter a variety of tools missing, such as Apr,pcre, to the official website to download the source code down to extract, and then run the Configure file after the compilation is good. The command is as follows:
./configure --prefix=安装地址makemake install#如果想清除之前的.o文件可以使用make distclean

After installing Apache, the command line enters the bin directory to run./apachectl start to launch the Apache service.

    • The most common in the startup process is the 80 port is occupied, reflected in the socket can not bind the address, in order to save trouble, I directly to Apache in the Conf directory of the httpd.conf file in the listening port changed to 8000.
Listen 127.0.0.1:8000
    • And also encountered this error: Httpd:could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
      Here you need to change the/etc/hosts file and the ServerName property, the detailed solution to see the above link.

After you turn on browser input localhost and return to it works, Apache installs successfully.

Apache Install expansion Module MOD_WSGI

Installation Method Portal: Mod_wsgi Installation
The first method is the traditional Apache installation of the Extension module method, first download MOD_WSGI source code, configure and then compile the installation, however, because there is a root issue in make intall (with sudo can not be resolved, detailed discussion see here) , so I chose to install the Mod_wsgi module in the second way.

The second method is to install Mod_wsgi directly with PIP, and then configure the mod_wsgi-py27.so path in the httpd.conf file after installation. The command is as follows:

module-location#将输出的路径复制到httpd.conf中,假设输出的路径为/user/local/mod_wsgi-py27.so#在http.conf中写入以下命令:LoadModule wsgi_module /user/local/mod_wsgi-py27.so

Restart the Apache server and the Mod_wsgi module should be installed.

Hello World example interacting with MOD_WSGI modules

Next, try using a simple Hello World application to test the normal operation of the Mod_wsgi module.
Define a application that conforms to the WSGI standard and save the file as Hello.wsgi. (If not very clear WSGI standard can see one of my blog: Wsgi Preliminary Discussion and wsgiref simple implementation)

#hello.wsgidef application(environ, start_response):    "200 OK"    response_headers = [(‘Content-type‘‘text/plain‘)]    "hello world!"    start_response(status, response_headers)    return [output]

I tried two ways to configure Apache to call Hello.wsgi back to Hello World.

    1. The first approach is to simply place the HELLO.WSGI in the root directory of Apache, which is relatively simple to access.
    2. The second option is to configure a virtual host on Apache to access Hello.wsgi through the domain name you set.

Let's first look at the first method:

    1. First open the Conf directory under the httpd.conf file (that is, the Apache master server configuration file), find the DocumentRoot variable, its value is the root directory of the Apache server, drag your Hello.wsgi file into the directory. (Note that you should have confirmed that all the file suffix names in the Finder are open under Mac, Hello.wsgi suffix. Wsgi, of course, you don't have to use the suffix. Wsgi, you can also use the. py suffix)
    2. Then set the alias of the Wsgi script under the DocumentRoot variable (assumed to be/users/apache/www):
WSGIScriptAlias /hello.wsgi /Users/Apache/WWW/hello.wsgi

After opening the Apache service, we entered Localhost:8000/hello.wsgi in the browser (listening on port 8000), we can see "Hello world! ”。

It is important to note that if the Wsgiscriptalias is set to:

WSGIScriptAlias/Users/Apache/WWW/hello.wsgi

All static files in the WWW directory will be masked, and any access to localhost:8000/* (* for any character) will be converted to HELLO.WSGI access. If we also want to access static files under the WWW directory, we need to use the alias directive.

The second method:

1. First set up in the http.conf file to introduce files containing virtual hosts:

# Virtual hosts# Include conf/extra/httpd-vhosts.conf将上面的#去掉就可以引入虚拟主机的配置文件了

2. Locate the Http-ghosts.conf configuration virtual host in the extra directory, as in the configuration Master server:
(assuming that the root directory of the virtual host is/users/apache/virtualhost, the port being monitored is 8001)

<virtualhost *:8001>    DocumentRoot "/users/apache/virtualhost"    ServerNameWww.virtualhost.comServeraliasVirtualhost.comWsgiscriptalias/hello.wsgi/users/apache/virtualhost/hello.wsgiDirectoryIndexIndex.html<directory "/users/apache/virtualhost" >        OptionsIndexes FollowSymLinksallowoverrideNoneRequire  AllGranted</Directory></VirtualHost>

3. After setting up the configuration file of the virtual host, we can not test our new domain name in the browser immediately, we will also bind the new domain name and 127.0.0.1. So open the/etc/hosts file, bind the domain name and address (permissions are required to modify the file):

127.0.0.1      virtualhost.com

So why is/etc/hosts set up like this?
Generally speaking, when the browser gets the domain name, the browser first queries its IP address via the/etc/hosts file, and then queries the DNS server when the IP is not available. So, if we set up a global proxy for shadowsocks on our Mac, and all requests go directly to the proxy, then we won't get the IP address of the domain name we created. (so remember to turn off your circumvention tool)
At this point, we can open the browser and enter Virtualhost.com:8000/hello.wsgi to see "Hello world! The

About the Mod_wsgi module embedded mode (embedded mode) and daemon process (daemon)

The Mod_wsgi module has two different modes when loading the WSGI application, the default is embed mode, and the other is the daemon, and we'll tell you how to turn on the daemon.
What is the difference between these two modes? Embedding mode will load WSGI application directly in Apache's child processes, while daemons will be loaded in different processes with the Apache process, requiring inter-process communication mechanisms. Therefore, if the code for the WSGI application is modified, the Apache server needs to be restarted in embedded mode, and the server will not need to be restarted under the daemon.
And, if more than one Django project is placed on the same Apache server, it is also recommended daemon, otherwise the settings module in the process will be confused with each other, refer to here: How to uses Django with Apache and Mod_ Wsgi

Next is the VirtualHost configuration code for daemon process:

这里写代码片

Mac install Apache Mod_wsgi module error Summary and Hello World test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.