Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 101t/074ebbe9e02ab1df6a7f to your computer and use it in GitHub Desktop.
Save 101t/074ebbe9e02ab1df6a7f to your computer and use it in GitHub Desktop.
Quick Start with Django Deployment on Ubuntu

Django Deployment on Ubuntu Server

Step 1:

install and edit django.wsgi.

sudo apt-get install libapache2-mod-wsgi
nano django.wsgi

Inside nano editor write the following codes

import os
import os.path
import sys
import site

site.addsitedir('/var/www/html/env/lib/python2.7/site-packages')

sys.path.append('/var/www/html')
sys.path.append('/var/www/html/main')

os.environ['PYTHON_EGG_CACHE'] = '/var/www/html/egg_cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'

# for Django <= 1.6
# import django.core.handlers.wsgi
# application = django.core.handlers.wsgi.WSGIHandler()

# for Django 1.8 and 1.7
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

[optinally] copy the same code to another code.

cp django.wsgi secondweb.wsgi

Step 2:

move to this directory cd /etc/apache2/sites-available/ and create new configure file to run your site by apache2.

nano 000-default.conf

write this code:

<VirtualHost *:80>
	# .. After Apache2 lines and comments .. #
	<Directory /var/www/html>
            <Files django.wsgi>
                Require all granted
            </Files>
        </Directory>
	WSGIDaemonProcess html python-path=/var/www/html:/var/www/html/env/lib/python2.7/site-packages
	WSGIProcessGroup html
	WSGIScriptAlias / /var/www/html/django.wsgi
	WSGIPassAuthorization On
	# important for subprocess.Popen() work
	WSGIApplicationGroup %{GLOBAL} 

	Alias /static /var/www/html/static/
	<Directory /var/www/html/static>
                Require all granted
        </Directory>
	#Alias /media /var/www/html/media/
</VirtualHost>

enable WSGI on apache2

sudo a2enmod wsgi

after that enable this config file on apache2

a2ensite 000-default.conf

then restart apache2 server

service apache2 restart

How to Install Python 3 on Ubuntu 18.04 / 20.04 / 22.04

Table Of Contents:

  1. Option 1: Install Python 3 Using apt (Easier)
  1. Option 2: Install Python 3.7 From Source Code (Latest Version)
  1. Using Different Versions of Python

Introduction

Python is a popular programming language often used to write scripts for operating systems. It’s versatile enough for use in web development and app design.

Prerequisites

  • A system running Ubuntu 18.04 or Ubuntu 20.04
  • A user account with sudo privileges
  • Access to a terminal window/command-line (Ctrl–Alt–T)
  • Make sure your environment is configured to use Python 3.8

Option 1: Install Python 3 Using apt (Easier)

This process uses the apt package manager to install Python. There are fewer steps, but it’s dependent on a third party hosting software updates. You may not see new releases as quickly on a third-party repository.

python --version

Step 1: Update and Refresh Repository Lists

Open a terminal window, and enter the following:

sudo apt update

Step 2: Install Supporting Software

The software-properties-common package gives you better control over your package manager by letting you add PPA (Personal Package Archive) repositories. Install the supporting software with the command:

sudo apt install software-properties-common

Step 3: Add Deadsnakes PPA

Deadsnakes is a PPA with newer releases than the default Ubuntu repositories. Add the PPA by entering the following:

sudo add-apt-repository ppa:deadsnakes/ppa

The system will prompt you to press enter to continue. Do so, and allow it to finish. Refresh the package lists again:

sudo apt update

Step 4: Install Python 3

Now you can start the installation of Python 3.6 with the command:

sudo apt install python3.6 python3.6-dev python3.6-venv

Troubleshooting accourding to new Ubuntu 22.04 (Jammy) version

to solve libssl1.1 no candidate to install to get ppa:deadsnakes/ppa repository works properly by install it manually:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1-1ubuntu2.1~18.04.22_amd64.deb
dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.22_amd64.deb

You may check suitable version for your system:

http://archive.ubuntu.com/ubuntu/pool/main/o/openssl

then retry install python3.6 again source: here

Option 2: Install Python 3.6 From Source Code (Latest Version)

Use this process to download and compile the source code from the developer. It’s a bit more complicated, but the trade-off is accessing a newer release of Python.

Step 1: Update Local Repositories

To update local repositories, use the command:

sudo apt update

Step 2: Install Supporting Software

Compiling a package from source code requires additional software. Enter the following to install the required packages for Python:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Step 3: Download the Latest Version of Python Source Code

To download the newest release of Python Source Code, navigate to the /tmp directory and use the wget command:

cd /tmp
wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz

Step 4: Extract Compressed Files

Next, you need to extract the tgz file you downloaded, with the command:

tar -xzvf Python-3.6.15.tgz

Step 5: Test System and Optimize Python

Before you install the software, make sure you test the system and optimize Python.

The ./configure command evaluates and prepares Python to install on your system. Using the ––optimization option speeds code execution by 10-20%.

cd Python-3.6.15/
./configure --enable-optimizations --enable-shared --with-ensurepip=install
make -j $(nproc)

This step can take up to 30 minutes to complete.

Step 6: Install a Second Instance of Python (recommended)

To create a second installation of Python 3.6.15, in addition to your current Python installation, enter the following:

sudo make altinstall

It is recommended that you use the altinstall method. Your Ubuntu system may have software packages dependent on Python 2.x.

Overwrite Default Python Installation (Option)

To install Python 3.6.15 over the top of your existing Python, enter the following:

sudo make install

Allow the process to complete.

Step 7: Verify Python Version

Enter the following:

python3 ––version

Using Different Versions of Python

If you used the altinstall method, you have two different versions of Python on your system at the same time. Each installation uses a different command.

Use the python command to run commands for any older Python 2.x version on your system. For example:

python ––version

To run a command using the newer version, use python3. For example:

python3 ––version

It is possible to have multiple major (3.x or 2.x) versions of Python on your system. If you have Python 3.6.x and Python 3.8.x both installed, use the second digit to specify which version you want to use:

python3.6 ––version
python3.8 ––version

Conclusion

You should now have a working installation of Python 3 on your Ubuntu system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment