django + apache2 + ssl: route URLs to PHP file? - php

I have django running with wsgi and apache.
I want to route some URLs to PHP part of the website. Because both the django/wsgi and PHP content requires SSL, I can't use virtual name hosting. How can I do this?
RewriteEngine in Apache config doesn't work, because there is no alternate NameVirtualHost to redirect to?
Can I have urls.py redirect to a PHP file, instead of a django application view?
Thanks!

You can put an alias to the php areas before your WSGIScriptAlias line in the virtual host section to get the desired result. I've just tested it:
alias /somefolder/ /srv/www.site.com/www/somefolder/
WSGIScriptAlias / /srv/www.site.com/myapp/app.wsgi
I can put php files into /srv/www.site.com/www/somefolder/ and they run as PHP.

Seems like it could be a major security issue as all requests are passed though Django when the Apache vhost has WGSI enabled. Just as it is not recommended to serve media though Django in production, this is likely not recommended.
That said, you might want to look at handling this like static media in PHP. Not sure that it will work, and I really would not recommend it, but you can give this a try:
urlpatterns = patterns(
(r'^php/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/abs/path/to/php'}),
)

Related

How to get redirect working (.htaccess) when using PHP -S localhost server?

I used xampp in the past in order to develop websites on a local computer, but I recently found it less cumbersome to simply start a server using the PHP -S localserver:8000 where 8000 is the port number. I managed to get phpmyadmin working that way and access databases. So all was fine ... until I tried to re-use redirecting rules within a .htaccess file.
The rules work well on the my web provider's servers, but it doesn't work on localhost. I assume there's something to do in order to turn the re-write rules on but since I am not longer using xampp, I don't know if there's still something like Apache running in the background, etc. If someone has experience with this setup, I would like to know how or if this can be be done.
EDIT
The redirect is basic:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
And to make the question more meaningful (addressing some of the comments below), I think Finwe answered my question rightly, that is: "this doesn't work with the PHP approach, .htaccess is specific to Apache".
My question wasn't about "what alternative solution do I have". The solution I would use is probably to go through a combination of AJAX/PHP to solve the limitation but that's surely not the most straightforward workaround.
PHP's internal server does not recognize .htaccess files. That is a privilege of using Apache server.
The redirects must be a part of your PHP application to retain them across all platforms.
With PHP internal server, you can run it with a PHP file parameter.
If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.
PHP -S localserver:8000 router.php
The script could then replace the htaccess file checking and "routing" requests to lessons/.
For more complex applications I would suggest using a "real" webserver such as Apache or nginx as the built-in server has more limitations, such as hanging on external HTTP requests, as it is only single threaded.
This is what I do to get htaccess working properly on localhost. I create a virtual host for each app am building
<VirtualHost *:8000>
ServerName app.local
DocumentRoot "C:/path/htdocs/app"
<Directory "C:/path/htdocs/app">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
With this, I can now access my app from my browser using the local domain name app.local. I found out that htaccess works well following this which is apparently the same thing with online server you access with a domain name

Django and PHP simultaneous use

I have an Apache server with PHP support. I also installed Python with mod_wsgi and with mysql-connector. Besides I installed Django. Now, I want to try to use PHP and Python simultaneously at the server side. The catch is, I worked with PHP for a couple of years and I see that it is becoming less and less popular, so I plan to port some of my PHP-code to Python-code, or just to try it, to see how they work together. So, I now have a site located at C:\Apache\htdocs and I created a first Django project at C:\WebPython\djsite. Inside djsite I have djsite folder and four files _init_.py, settings.py, urls.py and wsgi.py. In my site I want to address both to PHP handlers (or scripts) and to Python scripts, so, I guess, the problem is in how to config httpd.conf. I looked through many forum threads here at stackoverflow and outside, but still I can't make it work. Now, my httpd.conf looks like this:
...
ServerName localhost
<Directory "c:/Apache/htdocs">
Options Indexes FollowSymLinks
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
...
You should see this question then:
PHP script inside Django template
It has a link to this:
http://animuchan.net/django_php/
Running PHP with Django would be a mess though.
Hello Like Every Body Else Said Its A terrible idea but Refer To Django Documentation adding this to http.conf on your apache2 and tweek
the wsgi.py file will work
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
and change
If multiple Django sites are run in a single mod_wsgi process, all of them will use the settings of whichever one happens to run first. This can be solved by changing:
in wsgi.py, to:
os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
or by using mod_wsgi daemon mode and ensuring that each site runs in its own daemon process.
Fixing UnicodeEncodeError for file uploads
If you get a UnicodeEncodeError when uploading files with file names that contain non-ASCII characters, make sure Apache is configured to accept non-ASCII file names:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
A common location to put this configuration is /etc/apache2/envvars.
See the Files section of the Unicode reference guide for details.
See More At https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/

How can I put Pyramid in front of a PHP website using the same webserver?

The scenario is: I current have an old website that runs on PHP. Over time, that code has become hacked up and messy. It's due for a rewrite. However, I don't have time to do that rewrite yet. But I'd like to plan for it in the future.
What I need to do now is add a 'welcome' type page to the website. I'd like to code that in Python using the Pyramid framework.
The old sites URL structure is this:
http://website.com/XXXXXX
Where the X is the short URL id.
What I was thinking of doing was using Apaches LocationMatch config to catch the short URL. Something like:
<LocationMatch "/^([a-zA-Z0-9]{6})$">
This would then redirect the request to the PHP portion of the website. Everything else would be caught by Pyramid.
I'm not sure how to action this. Does Apache have an else type clause for LocationMatch? How would I tell it to serve the PHP files for /XXXXXX matches and send everything else to Pyramid?
Server Notes:
Apache2 + PHP (Debian package)
mod_wsgi 3.3
Python2.7
I am not sure about Apache configuration, but you could use wphp, a wsgi middleware for serving php.
http://pythonpaste.org/wphp/
Use recipes for using AddHandler described in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
Apply the AddHandler and the rewrite rule to your DocumentRoot directory. Then add the SCRIPT_NAME fixup to your WSGI script file.

Php - name of page parts

i need in naming like this:
site.com/about
site.com/contacts
Could i do it without .htaccess?
If you want a very simple implementation then you could structure your folders to allow you to do something like:
site.com/about/ - which will go to /about/index.php
site.com/contacts/ - which will go to /contacts/index.php
But obviously there's no room for any dynamic URLs, for that you would need a .htaccess implementation. They're very simple to do.
If you mean "Can I configure my webserver to parse /about and /contacts as PHP without using an .htaccess file", then no. Without further configuration (i.e. a local htaccess, or the global configuration files), Apache will not pass them through the PHP handler. You also won't be able to setup these URLs are redirects (internal or otherwise) without configuration (again via htaccess).
You could do it without a .htaccess file if you had access to the apache config files that define your site. However, if you can't use .htaccess files, you likely don't have access to the apache configs either. Might be wroth asking your host / sysadmin.
You might also be able to do it through the 404 handler if your host lets you have access to that.
that is rewriting
google search
similar to:
Options +FollowSymLinks
RewriteEngine on
Rewriterule ^about /menu/about.html
Rewriterule ^contacts /menu/contacts.html

Running a PHP script inside a Python WSGI enviroment

I have a simple PHP script that outputs a dir listing in XML format. I use it to let a flash slideshow know what files are available to show.
I've just added the flash to a website that's powered by Django and the PHP file is now served up as it is, not parsed.
It's in the directory with the images under my media directory.
The server I use runs plesk so I do my config for each domain in a vhost.conf file (which gets included into the main appache conf I think)
It looks like this:
WSGIScriptAlias / /var/www/vhosts/<domain>/conf/django.wsgi
Alias /media/ /var/www/vhosts/<domain>/httpdocs/media/
I thought this meant that requests for anything under / are passed django to handle.
Except when they are for /media/... then they are served by apache as normal from the specified dir.
That works for the images, but does not parse the PHP file.
What should I do?
Maybe read this thread, and port your PHP script to Python:
os.walk() python: xml representation of a directory structure, recursion
So it turns out the problem was two things, making it hard to find.
Thanks Ignacio Vazquez-Abrams, I had my lines the wrong way around.
Once that was solved, PHP would not serve my file because it was in a dir that was symlinked from outside the allowed path(s). I resolved this by turning off open_basedir restrictions for this vhost. My new vhost.conf is below.
<Directory /var/www/vhosts/<domain>/httpdocs>
php_admin_flag engine on
php_admin_value open_basedir none
</Directory>
Alias /media/ /var/www/vhosts/<domain>/httpdocs/media/
WSGIScriptAlias / /var/www/vhosts/<domain>/conf/django.wsgi
If you have not configured Apache so that it knows that .php files under the '/media' directory should be processed by PHP somehow, they will not be. So, the mod_wsgi configuration is fine, the problem is likely your PHP configuration.
How are you configuring PHP? Are you using mod_php, or PHP via fastcgi? How is Apache configured so that it knows to treat .php files as PHP and for what directories has that configuration been applied to?
The WSGIScriptAlias directive there swallows up URLs meant for Alias. Swap the order.

Categories