My Wiki!

Django setup on plesk hosting

Server Setting (2016-12-13)

Folder:

(vfossorg_env)[root@h2431351 vhosts]# tree -L 3
/var/www/vhosts
|-- chroot
|   |-- bin -> usr/bin
|   |-- dev
|   |   |-- null
|   |   |-- random
|   |   `-- urandom
|   |-- etc
|   |   |-- group
|   |   |-- passwd
|   |   `-- resolv.conf
|   |-- lib -> usr/lib
|   |-- lib64 -> usr/lib64
|   |-- sbin -> usr/sbin
|   |-- tmp
|   |-- usr
|   |   |-- bin
|   |   |-- lib
|   |   |-- lib64
|   |   `-- libexec
|   `-- var
|       `-- tmp
|-- default
|   |-- cgi-bin
|   |-- htdocs
|   |   |-- css
|   |   |-- favicon.ico
|   |   |-- img
|   |   `-- index.html
|   `-- httpsdocs
|-- system
|   `-- vfoss.org     <------------- Configurations here!
|       |-- conf
|       |-- etc
|       |-- logs
|       |-- pd
|       `-- statistics
`-- vfoss.org
    |-- error_docs
    |   |-- bad_gateway.html
    |   |-- bad_request.html
    |   |-- forbidden.html
    |   |-- internal_server_error.html
    |   |-- maintenance.html
    |   |-- method_not_allowed.html
    |   |-- not_acceptable.html
    |   |-- not_found.html
    |   |-- not_implemented.html
    |   |-- precondition_failed.html
    |   |-- proxy_authentication_required.html
    |   |-- request-uri_too_long.html
    |   |-- unauthorized.html
    |   `-- unsupported_media_type.html
    |-- httpdocs
    |   |-- cgi-bin
    |   |-- css
    |   |-- favicon.ico
    |   |-- img
    |   |-- index.html
    |   `-- test
    |-- logs
    |   |-- access_log
    |   |-- access_log.processed
    |   |-- access_log.processed.1.gz
    |   |-- access_log.webstat
    |   |-- access_ssl_log
    |   |-- access_ssl_log.processed
    |   |-- access_ssl_log.webstat
    |   |-- error_log
    |   `-- error_log.1.gz
    |-- pydocs
    |   `-- vfossorg_pj
    `-- vfossorg_env
        |-- bin
        |-- include
        |-- lib
        |-- lib64 -> lib
        `-- pip-selfcheck.json

solution 1

It turns out Plesk does allow overriding vhost directives and quite easily that is.

Just create or edit a vhost.conf file in the right directory;

sudo vim /home/www/vhosts/yourdomain.com/conf/vhost.conf

Need this?? Look like it works without this in plesk 12. Then simply add your vhost directives:

ServerAdmin  "bob@yourdomain.com"
DocumentRoot /var/www/vhosts/yourdomain.com/httpdocs
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
...

A working vhost.conf

# included from httpd.conf within <VirtualHost>

Alias /media/ /var/www/vhosts/vfoss.org/pydocs/vfoss_org/vfoss_org/media/
  Alias /static/ /var/www/vhosts/vfoss.org/pydocs/vfoss_org/vfoss_org/static/

  Alias /forum/ /var/www/vhosts/vfoss.org/httpdocs/forum/
	<Directory /var/www/vhosts/vfoss.org/httpdocs/forum>
	Options -Indexes
	Order deny,allow
	Allow from all
	</Directory>
	
	Alias /wiki/ /var/www/vhosts/vfoss.org/httpdocs/wiki/
	<Directory /var/www/vhosts/vfoss.org/httpdocs/wiki>
	Options -Indexes
	Order deny,allow
	Allow from all
	</Directory>


	<Directory /var/www/vhosts/vfoss.org/pydocs/vfoss_org/vfoss_org/media>
	Order deny,allow
	Allow from all
	</Directory>

	<Directory /var/www/vhosts/vfoss.org/pydocs/vfoss_org/vfoss_org/static>
	Order deny,allow
	Allow from all
	</Directory>

	WSGIScriptAlias / /var/www/vhosts/vfoss.org/pydocs/vfoss_org/apache/vfoss_org.wsgi

Then you just have to refresh Plesk's config and restart apache:

/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain vfoss.org

Obsolate:

/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=yourdomain.com
/etc/init.d/httpd graceful

or

/usr/local/psa/admin/sbin/websrvmng --reconfigure-all
service httpd restart

Et voila.

Plesk django virtualenv

Plesh Django Apache + mod_wsgi

Why mod_wsgi?

Prepare Server (Cent OS 7)

Allow ssh for domain user as here: http://devblog.plesk.com/2014/10/plesk-and-django/

ssh td@
su root ####

Install necessary packages:

  yum install epel-release.noarch
  yum update
  

Compile python 2.7 from source (Centos 6 only)

yum groupinstall -y development
yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel
cd /opt
wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local
make && make altinstall    

Configure Python

  echo $PATH
  /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin    

which python will look in /usr/local/sbin first. But the dir is not avalable.

  ln -s /usr/local/bin/python2.7 /usr/local/bin/python
  DONE

Install Pip

  /usr/local/bin/python2.7 ez_setup.py
  /usr/local/bin/easy_install-2.7 pip
  pip install virtualenv
  

Headline

  sudo yum install libxslt-devel libxml2-devel
  sudo yum install python-devel libjpeg-devel freetype-devel libpng-devel
  sudo yum install mysql-devel mysql-common mysql-libs   
  yum install python-setuptools <del>httpd</del> mod_wsgi
  yum install python-virtualenv.noarch python-pip.noarch
  

Additionally you will need to install a database system and a python driver for this database system. If you want to run the PostgreSQL database server issue the following command:

  yum install postgresql python-psycopg2

If you want to use the SQLite embedded database, issue the following command:

  yum install sqlite python-sqlite

If you want to run the MySQL database engine, download and install a more recent version of the MySQL-python package. Django requires at least version 1.2.1p2 of the Python MySQLdb adapter. We’ll download and install a later version from the upstream project First, install the tools needed to build this package:

  yum install python-devel mysql-devel gcc wget python-setuptools

Check the Python MySQLdb page for information regarding the latest release, then issue the following commands. This will download the archive of the source files, extract them from the archive, build the database adapter, and install the files on to your system.

  cd /opt/
  wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
  tar -zxvf MySQL-python-1.2.3.tar.gz
  cd MySQL-python-1.2.3/
  python setup.py build
  python setup.py install

Log out of root

Creating a virtual environment for the project

  pwd
  /var/www/vhosts/vfoss.org
  virtualenv vfossorg_env --no-site-packages
  mkdir pydocs
  source vfossorg_env/bin/activate
  
  git clone vfoss-pj
  su -c "yum install python-devel libjpeg-devel freetype-devel libpng-devel"
  pip install -r vfoss/requirements.txt
  

Workflow

  • When domain is activated, the directive in httpd.conf, vhost.conf will load a wsgi.py script.
  • In wsgi.py script the django application is activated. If virtualenv is used, it is loaded by this script.

wsgi.py

 cat vfoss_org/vfoss_org/wsgi.py 
"""
WSGI config for vfoss_org project.
 
It exposes the WSGI callable as a module-level variable named ``application``.
 
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
 
#import os
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vfoss_org.settings")
#
#from django.core.wsgi import get_wsgi_application
#application = get_wsgi_application()
 
import os
import sys
import site
 
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/www/vhosts/vfoss.org/vfossorg_env/lib/python2.7/site-packages')
 
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/vhosts/vfoss.org/pydocs/vfossorg_pj/vfoss_org')
sys.path.append('/var/www/vhosts/vfoss.org/pydocs/vfossorg_pj/vfoss_org/vfoss_org')
#sys.path.append('/home/django_projects/MyProject/myproject')
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'vfoss_org.settings'
 
# Activate your virtual env
activate_env=os.path.expanduser("/var/www/vhosts/vfoss.org/vfossorg_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
 
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

vhost.conf

Look above! Using the vhost.conf listed previosly

Restart httpd server or use mod-passenger

   apt-get install libapache2-mod-passenger
  service httpd restart
  

With passenger You need to create directory named “tmp” inside “django-app”:

  mkdir tmp

After that, to restart the app, you can use the following command:

  touch tmp/restart.txt

Create super user and database

./manage.py --help | grep super
./manage.py createsuperuser    

Smaple script

Origianl httpd.conf

#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
#IF YOU REQUIRE TO APPLY CUSTOM MODIFICATIONS, PERFORM THEM IN THE FOLLOWING FILES:
#/var/www/vhosts/system/vfoss.org/conf/vhost.conf
#/var/www/vhosts/system/vfoss.org/conf/vhost_ssl.conf
<IfModule mod_ssl.c>

	<VirtualHost 85.214.233.136:443 >
		ServerName "vfoss.org:443"
		ServerAlias "www.vfoss.org"
		ServerAlias "ipv4.vfoss.org"
		ServerAdmin "write.thuy@gmail.com"
		UseCanonicalName Off

		DocumentRoot "/var/www/vhosts/vfoss.org/httpdocs"
		CustomLog /var/www/vhosts/system/vfoss.org/logs/access_ssl_log plesklog
		ErrorLog "/var/www/vhosts/system/vfoss.org/logs/error_log"

		<IfModule mod_suexec.c>
			SuexecUserGroup "vfossadmin" "psacln"
		</IfModule>

		<IfModule mod_userdir.c>

			UserDir "/var/www/vhosts/vfoss.org/web_users/*"
		</IfModule>

		<IfModule mod_sysenv.c>
			SetSysEnv PP_VHOST_ID "d63a9b47-206f-4fa1-9e35-c8a29a5ef98c"
		</IfModule>

		ScriptAlias "/cgi-bin/" "/var/www/vhosts/vfoss.org/httpdocs/cgi-bin/"

		Alias "/plesk-stat" "/var/www/vhosts/system/vfoss.org/statistics"
		<Location  /plesk-stat/>
			Options +Indexes
		</Location>
		<Location  /plesk-stat/logs/>
			Require valid-user
		</Location>
		Alias /webstat /var/www/vhosts/system/vfoss.org/statistics/webstat
		Alias /webstat-ssl /var/www/vhosts/system/vfoss.org/statistics/webstat-ssl
		Alias /ftpstat /var/www/vhosts/system/vfoss.org/statistics/ftpstat
		Alias /anon_ftpstat /var/www/vhosts/system/vfoss.org/statistics/anon_ftpstat
		Alias /awstats-icon /var/www/html/awstats/icon

		SSLEngine on
		SSLVerifyClient none
		SSLCertificateFile /usr/local/psa/var/certificates/cert92NfLwE

		SetEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/vfoss.org/etc/php.ini
		SetEnv PP_CUSTOM_PHP_CGI_INDEX fastcgi

		<IfModule mod_fcgid.c>
			FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/vfoss.org/etc/php.ini
			FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX fastcgi
			FcgidMaxRequestLen 134217728
		</IfModule>

		<Directory /var/www/vhosts/vfoss.org/httpdocs>

			<IfModule mod_fcgid.c>
				<Files ~ (\.fcgi$)>
					SetHandler fcgid-script
					Options +ExecCGI
				</Files>
			</IfModule>
			<IfModule mod_fcgid.c>
				<Files ~ (\.php$)>
					SetHandler fcgid-script
					FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
					Options +ExecCGI
				</Files>
			</IfModule>

			SSLRequireSSL

			Options -Includes +ExecCGI

		</Directory>

		<Directory "/var/www/vhosts/system/vfoss.org/statistics">
			AuthType Basic
			AuthName "Domain statistics"
			AuthUserFile "/var/www/vhosts/system/vfoss.org/pd/d..httpdocs@plesk-stat"
			require valid-user
		</Directory>

		Alias /error_docs /var/www/vhosts/vfoss.org/error_docs
		ErrorDocument 400 /error_docs/bad_request.html
		ErrorDocument 401 /error_docs/unauthorized.html
		ErrorDocument 403 /error_docs/forbidden.html
		ErrorDocument 404 /error_docs/not_found.html
		ErrorDocument 500 /error_docs/internal_server_error.html
		ErrorDocument 405 /error_docs/method_not_allowed.html
		ErrorDocument 406 /error_docs/not_acceptable.html
		ErrorDocument 407 /error_docs/proxy_authentication_required.html
		ErrorDocument 412 /error_docs/precondition_failed.html
		ErrorDocument 414 /error_docs/request_uri_too_long.html
		ErrorDocument 415 /error_docs/unsupported_media_type.html
		ErrorDocument 501 /error_docs/not_implemented.html
		ErrorDocument 502 /error_docs/bad_gateway.html
		ErrorDocument 503 /error_docs/maintenance.html

		Include "/var/www/vhosts/system/vfoss.org/conf/vhost_ssl.conf"

		<IfModule mod_security2.c>
		</IfModule>

	</VirtualHost>

</IfModule>

<VirtualHost 85.214.233.136:80 >
	ServerName "vfoss.org:80"
	ServerAlias "www.vfoss.org"
	ServerAlias "ipv4.vfoss.org"
	ServerAdmin "write.thuy@gmail.com"
	UseCanonicalName Off

	DocumentRoot "/var/www/vhosts/vfoss.org/httpdocs"
	CustomLog /var/www/vhosts/system/vfoss.org/logs/access_log plesklog
	ErrorLog "/var/www/vhosts/system/vfoss.org/logs/error_log"

	<IfModule mod_suexec.c>
		SuexecUserGroup "vfossadmin" "psacln"
	</IfModule>

	<IfModule mod_userdir.c>

		UserDir "/var/www/vhosts/vfoss.org/web_users/*"
	</IfModule>

	<IfModule mod_sysenv.c>
		SetSysEnv PP_VHOST_ID "d63a9b47-206f-4fa1-9e35-c8a29a5ef98c"
	</IfModule>

	ScriptAlias "/cgi-bin/" "/var/www/vhosts/vfoss.org/httpdocs/cgi-bin/"

	Redirect permanent /plesk-stat https://vfoss.org/plesk-stat
	Redirect permanent /webstat https://vfoss.org/webstat
	Redirect permanent /webstat-ssl https://vfoss.org/webstat-ssl
	Redirect permanent /ftpstat https://vfoss.org/ftpstat
	Redirect permanent /anon_ftpstat https://vfoss.org/anon_ftpstat
	Redirect permanent /awstats-icon https://vfoss.org/awstats-icon

	<IfModule mod_ssl.c>
		SSLEngine off
	</IfModule>

	SetEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/vfoss.org/etc/php.ini
	SetEnv PP_CUSTOM_PHP_CGI_INDEX fastcgi

	<IfModule mod_fcgid.c>
		FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/vfoss.org/etc/php.ini
		FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX fastcgi
		FcgidMaxRequestLen 134217728
	</IfModule>

	<Directory /var/www/vhosts/vfoss.org/httpdocs>

		<IfModule mod_fcgid.c>
			<Files ~ (\.fcgi$)>
				SetHandler fcgid-script
				Options +ExecCGI
			</Files>
		</IfModule>
		<IfModule mod_fcgid.c>
			<Files ~ (\.php$)>
				SetHandler fcgid-script
				FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
				Options +ExecCGI
			</Files>
		</IfModule>

		Options -Includes +ExecCGI

	</Directory>

	<Directory "/var/www/vhosts/system/vfoss.org/statistics">
		AuthType Basic
		AuthName "Domain statistics"
		AuthUserFile "/var/www/vhosts/system/vfoss.org/pd/d..httpdocs@plesk-stat"
		require valid-user
	</Directory>

	Alias /error_docs /var/www/vhosts/vfoss.org/error_docs
	ErrorDocument 400 /error_docs/bad_request.html
	ErrorDocument 401 /error_docs/unauthorized.html
	ErrorDocument 403 /error_docs/forbidden.html
	ErrorDocument 404 /error_docs/not_found.html
	ErrorDocument 500 /error_docs/internal_server_error.html
	ErrorDocument 405 /error_docs/method_not_allowed.html
	ErrorDocument 406 /error_docs/not_acceptable.html
	ErrorDocument 407 /error_docs/proxy_authentication_required.html
	ErrorDocument 412 /error_docs/precondition_failed.html
	ErrorDocument 414 /error_docs/request_uri_too_long.html
	ErrorDocument 415 /error_docs/unsupported_media_type.html
	ErrorDocument 501 /error_docs/not_implemented.html
	ErrorDocument 502 /error_docs/bad_gateway.html
	ErrorDocument 503 /error_docs/maintenance.html

	Include "/var/www/vhosts/system/vfoss.org/conf/vhost.conf"

	<IfModule mod_security2.c>
	</IfModule>

</VirtualHost>

Sources


Navigation