If you plan to use Python for your web backend then you might want to consider Django as there are many good libraries that you need for web development.
Installing Django
Django is used in your project as any other python module. The easiest way would be to get it from your operating system repository or pip.Check if and what version of django you have installed.Source code viewer
# From your operating systems repositories. yaourt -S python-django # From Python's package manager. pip install djangoProgramming Language: Bash
Source code viewer
python -c 'import django; print(django.get_version())'Programming Language: Bash
Creating a project
Go to your project directory and run django-admin command on your command line to create the skeleton for your web development project.Source code viewer
django-admin startproject mysiteProgramming Language: Bash
Apache
- Make sure you have enabled mod_wsgi, you can see "Running Python on Apache" for more information.
- I have "/var/www/python" for my python instance. In that folder I have manage.py from the last step.
- I added "127.0.0.1 python" to my "/etc/hosts". Then my instance opens from http://python/
- I have configuration for my python instance in vhosts.
When this was done and Apache restarted. I had error 500. From the apache log I found out that database is requirement for Django.Source code viewer
<VirtualHost python:80> ServerAdmin info@browse-tutorials.com ServerName python ServerAlias python WSGIDaemonProcess mysite python-path=/var/www/python:/usr/lib/python3.5/site-packages WSGIProcessGroup mysite WSGIScriptAlias / /var/www/python/mysite/wsgi.py <Directory /var/www/python> <Files wsgi.py> Order allow,deny Allow from all </Files> </Directory> ErrorLog /var/log/httpd/.python-error_log CustomLog /var/log/httpd/.python-access-log combined </VirtualHost>Programming Language: Apache configuration
MySQL
In "/var/www/python/mysite/" I have "settings.py". The directory "mysite" was created by django-admin. In that file is DATABASES variable, that contains the information.Source code viewer
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'python', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '', }}Programming Language: Python
- So you could connect to database. "pip install mysql-python"
- Run "python manage.py migrate" to create neccessary database changes.
- Everything should be up and running now. There should be a welcome page.