Use bottlepy and php in the same computer

2020-08-01 16:21发布

问题:

I use ubuntu 12.04 with apache2 and mod_wsgi installed. I want to use bottlepy and php in my local computer. I know such an issue is already asked by someone else as in Apache mod_wsgi and php in the same domain. But someone suggest me to make a new question since my problem could be different.

I've change /etc/apache2/sites-available/default into this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

<VirtualHost *>
    DocumentRoot /home/gofrendi/workspace/kokoropy
    ServerName oraiso
    WSGIDaemonProcess kokoropy user=www-data group=www-data processes=1 threads=5
    WSGIScriptAlias /kokoropy /home/gofrendi/workspace/kokoropy/kokoro.wsgi
    <Directory /home/gofrendi/workspace/kokoropy>
        WSGIProcessGroup kokoropy
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

The first virtual host is for PHP, and the second one is for bottlepy. I put my bottlepy application in /home/gofrendi/workspace/kokoropy. And I have kokoro.wsgi in the same directory which contains this script:

import os
sys.path = [os.path.dirname(__file__)] + sys.path

from kokoropy import kokoro_init
PWD = os.path.dirname(os.path.abspath(__file__))
APP_DIRECTORY = 'applications'
APPLICATION_PATH = os.path.join(PWD, APP_DIRECTORY)    
application = kokoro_init(application_path = APPLICATION_PATH, run = False)

I've do enable the configuration by using

sudo a2ensite default
sudo service apache2 restart

My PHP scripts are still work as expected. But, whenever I don't know how to access my bottlepy script.

I've also try to remove the PHP part of /etc/apache2/sites-available/default, so that it only consists of

<VirtualHost *>
    DocumentRoot /home/gofrendi/workspace/kokoropy
    ServerName oraiso
    WSGIDaemonProcess kokoropy user=www-data group=www-data processes=1 threads=5
    WSGIScriptAlias /kokoropy /home/gofrendi/workspace/kokoropy/kokoro.wsgi
    <Directory /home/gofrendi/workspace/kokoropy>
        WSGIProcessGroup kokoropy
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

But I still can't get bottlepy work. It just simply show 404 not found.

Do anybody has the same experience? How to make it work? Thanks.

回答1:

The problem has been solved. First I change httpd.conf setting (or in my case /etc/apache2/sites-available/default) nto this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>


    # THIS IS WHERE I START TO EDIT IT:
    # It tells apache about wsgi existance
    WSGIDaemonProcess kokoropy user=www-data group=www-data processes=1 threads=5   
    WSGIScriptAlias /kokoropy /home/gofrendi/workspace/kokoropy/kokoro.wsgi
    <Directory /home/gofrendi/workspace/kokoropy>
        WSGIProcessGroup kokoropy
        WSGIApplicationGroup %{GLOBAL}
        Options ExecCGI
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

This configuration say that localhost/kokoropy should be handled by the wsgi.

My wsgi script is located at /home/gofrendi/workspace/kokoropy/kokoro.wsgi. kokoro.wsgi content is as follow:

import os, sys
os.chdir(os.path.dirname(__file__))
sys.path = [os.path.dirname(__file__)] + sys.path

from bottle import default_app
@route('/')
def index():
   return 'run with apache'
application = default_app()

If you found internal sever error, when accessing bottlepy, but find no error when accessing php, that is probably some mistake in you wsgi. Just open up the apache log (in my case it is here: /var/log/apache2/error.log)