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/
Related
I am new to this and I searched everywhere for a server routing method to be able to store 2 sites at different paths to a single domain. Most help I found refers to using virtual host to multiple domains, subdomains, IP etc. So far non of them fits what I need.
So I have the latest stack of Apache2, PHP, MySQL, on Debian-8 with 2 functional sites (databases, users, ...all OK) installed on sub folders of /html:
crm_site under /var/www/html/crm (storing a php script for project tracking)
and
wp_site under /var/www/html/wordpress (storing a wordpress website).
I want to be able to access them using the only domain I own (say www.example.com) which I already have set up to reach my server.
I was hoping that I would only need to add the path to my domain (www.example.com/crm/index.php or www.example.com/wordpress/index.php) and they will be served, but no matter what I add after the domain, the browser leads to the same place, showing the directory list in html (that is crm, and wordpress).
Can anyone tell me how can this be done? Thank you.
it sounds like you have everything setup correctly with the exception of your starting Directory.
It may be best to reword your question. This seems to be your situation (speaking from your perspective):
have a website at http://machine.domain.com
I want to set /var/www/html as my default directory
I want to set index.php as my default document
Once you view your issue this way, it is greatly simplified. A search engine can help you at this point.
To further guide you, doing said search for "apache set default directory" on Google, for instance, has the answer in bold. ... change the root directory of Apache or move the project to /var/www/html
You've already done this, right? So 1. and 2. above are done. If not, look at the following (which assumes apache2 package on Ubuntu latest. i've tested this in a docker container):
file: /etc/apache2/apache2.conf
contents:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change the above /var/www/ to /var/www/html/
Next, the .php serving issue. This is controlled by the DirectoryIndex directive as referenced here. Looking at it's contents shows that index.php is enabled by default.
example:
root#b62dsa09327e:/# grep -rnw '/etc/apache2/' -e "DirectoryIndex"
/etc/apache2/mods-available/dir.conf:2: DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
This takes care of 3. And now we're all out of issues in your original question.
Note: you may have to chown the directory to whatever apache2 is running as in the event you dropped files in there as root. You'll also have to restart apache in order to have the changes above reflect in the service.
I am working with a colleague to set up their local environment on a MAC in XAMPP, on windows my vhost looks like the one below.
When I access a URL like http://domain.local/cgi-bin/handler.php the web server processes the PHP correctly but on his we get a 500 server error and this message...
The server encountered an internal error and was unable to complete your request.
Error message:
Premature end of script headers:
We tried changing the name of the cgi-bin folder to something else as I noticed there was another alias in httpd.conf but this had no effect...so it seems to me like the issue is permissions related.
We believe the alias is setup ok as accessing http://domain.local/cgi-bin/filenothere.php which doesn't exist throws a 404 as expected, any .html/.pl files fail to execute.
The permissions that exist on the cgi-bin folder are...
rwxrwxrwx dave staff
and is owned by the user and group....
dave staff
Vhost
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName www.domain.local
ServerAlias domain.local
ServerAlias api.domain.local
# Indexes + Directory Root.
DirectoryIndex index.php
DocumentRoot E:/home/www/www.domain.co.uk/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ E:/home/www/www.domain.co.uk/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog E:/home/www/www.domain.co.uk/logs/error.log
CustomLog E:/home/www/www.domain.co.uk/logs/access.log combined
</VirtualHost>
Any idea what is causing this PHP file to not be executed?
UPDATE
Tried adding a header to say that the content is PHP to the start of the PHP file and this now simply outputs the PHP code.
It's as if any path specified in as an Alias is accessible but the server doesn't know how to execute the content, this is for HTML as well as PHP
I think you need a section for your cgi-bin.
The fact that your server can show you the script sources means the server has at least read permissions on /file/system/path/to/cgi-bin and IIRC that clashes with ScriptAlias b/c ScriptAlias expects /file/system/path/to/cgi-bin to be unaccessible for security reasons. I think your solution should look something along the lines of:
<Directory /file/system/path/to/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory
There is (very) rarely a need to run PHP scripts as CGIs given that the PHP module for Apache can execute them directly. Try adding this to your Apache config:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Afterwards simply place the PHP scripts into the document root for the site and see if they work. You'll want to remove the /cgi-bin/ part of the URL.
You say you're setting XAMMP on a Mac, but you have a drive letter (E:) prefixing your paths. OS X does not have drive letters like Windows, and this may also be causing (part of) your issue.
I don't know much about settings used. But I think you should go through following links. Might get some help.
http://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
How to create Mac OS X dev environment for legacy PHP app?
Assuming that PHP is running in Safe Mode you may need to "open" your cgi-bin directory, as the execution of user (PHP) scripts is limited to the DocumentRoot and it's subfolders.
For all I know you could do that in two ways
1. Edit your php.ini
Locate the line containing open_basedir. If there's a comment at the beginning of the line - a semicolon - remove it. Then add your cgi-bin directory.
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\"
If you need to open more than one directories you can use semicolon ; as a separator. On Linux based server, use a colon :
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\;E:\home\www\www.domain.co.uk\another_dir\"
In cases like mine, where your server is hosted by third party, you'd need the second option (well sort of)
2. Edit your VirtualHost
Add the following to your VirtualHost, i.e. after DocumentRoot:
php_admin_value open_basedir "E:\home\www\www.domain.co.uk\cgi-bin\"
Same rules apply here for multiple directories and difference between Linux and Windows systems as above.
Hope that helps
Do you know whether PHP is running as a CGI program or as a webserver module? You should be able to find this out if you can get a phpinfo() page working (maybe from a regular folder inside the website root). If you're running as a webserver module then you should have a section near the top with a heading of Server API which says Apache 2.0 Handler (or equivalent).
From these pages:
https://bugs.php.net/bug.php?id=13316
http://php.net/manual/en/install.unix.commandline.php
http://gallery.menalto.com/node/8955
... it seems that it may be either due to PHP running as a CGI script, or else a conflict between PHP and another CGI handler.
One of the posters on the third linked page found that their similar-sounding end of script headers issue was resolved by removing / commenting out the Options +ExecCGI line in their .htconfig / vhosts file.
Might be worth having a read through the above links to see if your problem is related.
There are Apache 2 + mod_wsgi + Python 2.6 on server.
I would like to run scripts from Python like PHP scripts. The idea may seem silly, but I'm so accustomed (at least at first learning Python).
Example:
PHP - http://example.com/script.php
Python - http://example.com/script.py
P.S. I know about mod_rewrite and other similar tricks that can do that. But it is only a disguise, and not a direct run.
UPD: My .htaccess file. index.py works, but other python scripts get 404 error.
<Files *.py>
SetHandler wsgi-script
Options ExecCGI FollowSymLinks
</Files>
DirectoryIndex index.py
That doesn't look as cool as having a wsgi app running, so I recommend that you use the flask framework which is as simple as can be a sane framework.
Here's a link describing the install procedure on mod_wsgi.
Later on, you might want to consider a cool framework like Django, Pyramid, Grok ...
If you really want to use mod_wsgi like mod_php check Graham Dumpleton's great answer.
Technically what you are doing should work, but see AddHandler method for configuring mod_wsgi in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
That way you don't have to fiddle with Files directive.
SetHandler does similar thing but all files in context are treated as WSGI script files even if they may be static HTML or PHP files. You got away with it because qualified with Files, but better to just use AddHandler.
Do note that code reloading will not work like you are used to with PHP. See:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
I run Python scripts like PHP using mod_cgi
Here is a tutorial by Apache. And the cgi interface to use is here by Python.org
A second good tutorial that I used is here once your up and running.
I would add that there is a simiplier way to configure Apache.
Step 1: The first step is not mentioned in the guides above is to enable CGI processing in apache.
sudo a2enmod cgi
This will automatically enable mod_cgid if your server is configured with a multi-threaded MPM, which was the case for me.
Step 2: Edit your httpd.conf or whatever it is named in /etc/apache2/sites-enabled
in Linux Mint 19.2.
Enable a script for / with an index.py
<VirtualHost *:80>
DocumentRoot /your/www/html
DirectoryIndex index.py
</VirtualHost>
Step 3: Enable other python scripts so they can also run in the same folder or in subdirectories.
<Directory "/your/www/html/*">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .py
AddHandler default-handler .jpg .png .gif .css .js .ico
</Directory>
ScriptAlias / /your/www/html/
There are two caveats that I have encountered that must be adhered to to run python scripts successfully.
When running in linux, make sure the line endings of each python file.py are
unix line endings. Otherwise the python script will not run. For example,
Notepad++ has Edit, EOL Conversion, Linux (LF) in its menu,tool bar.
Ensure that the permission of each python file.py has execute permissions.
In Linux Mint 19.2 I right click the file, go to Properties, go to Permissions,
then check the checkbox at Execute: Allow executing program as file. Or just
run the command:
chmod a+x python_script.py
Last night I made some admin changes to my webserver. I use php. The php processor failed after the update and if someone went to my homepage, the php page would simply download and show the proprietary code and password to anyone visiting. So I was wondering if there is a way to prevent any form of download for php files using .htaccess -- but still allow for normal viewing of the files.
A good pattern to follow during development is to use a minimal initialization file, which invokes the actual application which resides outside the webroot. That way only a minimal stub with no critical information is exposed in a case like this.
Simplified example:
/
/app
critical_code.php
/webroot
.htaccess <- rewrites all requests to index.php
index.php <- invokes ../app/critical_code.php (or other files as requested)
The trouble here is that either .htaccess is serving your files to the user or it's not. You can't tell it to deny access to the .php files, because then access will be denied during normal use, as well. There is no fallback behavior for the PHP processor simply not running correctly.
Maybe it's worth temporarily moving the web root to point to an "under maintenance" site when doing big things like that, to minimize risk as much as possible.
Assuming you're using Apache, your .htaccess file would look something like this.
<FilesMatch ".*\.php">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
<IfModule php5_module>
<FilesMatch ".*\.php">
Allow from all
Satisfy All
</FilesMatch>
</IfModule>
The first rule denies access to all .php files. By default, the user will see a 403 (Forbidden) error.
If the PHP5 module successfully loads, the second rule will take affect, which grants access.
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.