<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django Archives - LinuxTuto</title>
	<atom:link href="https://www.linuxtuto.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.linuxtuto.com/tag/django/</link>
	<description>Linux Sysadmin and DevOps blog</description>
	<lastBuildDate>Mon, 03 Oct 2022 15:00:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://www.linuxtuto.com/wp-content/uploads/2022/01/cropped-LT_faveicon-32x32.png</url>
	<title>Django Archives - LinuxTuto</title>
	<link>https://www.linuxtuto.com/tag/django/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">201456972</site>	<item>
		<title>How to Install Django with Apache on Ubuntu 22.04</title>
		<link>https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/</link>
					<comments>https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/#comments</comments>
		
		<dc:creator><![CDATA[LinuxTuto]]></dc:creator>
		<pubDate>Mon, 03 Oct 2022 15:00:32 +0000</pubDate>
				<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[MySQL]]></category>
		<guid isPermaLink="false">https://www.linuxtuto.com/?p=757</guid>

					<description><![CDATA[<p>Django is a python based full stack framework. This framework works based on the model-template-view architectural patterns. Django is considered to be one of the...</p>
<p>The post <a href="https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/">How to Install Django with Apache on Ubuntu 22.04</a> appeared first on <a href="https://www.linuxtuto.com">LinuxTuto</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Django is a python based full stack framework. This framework works based on the model-template-view architectural patterns. Django is considered to be one of the popular web based development frameworks for developing Python’s server applications.</p>
<p>The high profile websites that use Django are Disqus, Instagram, MacArthur Foundation, National Geographic channel, Knight Foundation, Pinterest, Open knowledge foundation, and open stack software.</p>
<p>We will be installing Django application in a Python virtual environment. It is very useful because it allows developers to run and develop an application with different python versions.</p>
<h2><span class="has-inline-color has-vivid-purple-color">Step 1: Update Operating System</span></h2>
<p>Update your <strong>Ubuntu</strong> <strong>22.04</strong> operating system to the latest version with the following command:</p>
<pre><code># apt update &amp;&amp; sudo apt upgrade -y</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 2: Install Apache with mod_wsgi Module</span></h2>
<p>To setup Django to production we will install Apache and the Apache <code>mod_wsgi</code> module.</p>
<p>You can install them via <code>apt</code> package manager by executing the following command:</p>
<pre><code># apt install apache2 libapache2-mod-wsgi-py3</code></pre>
<p>Once all the packages are installed, you can start the Apache service and configure it to run on startup by entering the following commands:</p>
<pre><code># systemctl start apache2
# systemctl enable apache2</code></pre>
<p>Verify the status of the <code>Apache</code> service using <code>systemctl status</code> command:</p>
<pre><code># systemctl status apache2</code></pre>
<p>Output:</p>
<pre><code>● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running)
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 39757 (apache2)
      Tasks: 55 (limit: 2200)
     Memory: 15.2M
        CPU: 206ms
     CGroup: /system.slice/apache2.service
             ├─39757 /usr/sbin/apache2 -k start
             ├─39761 /usr/sbin/apache2 -k start
             └─39762 /usr/sbin/apache2 -k start
</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 3: Install MySQL and create a database</span></h2>
<p>You can install the MySQL server and <code>libmysqlclient-dev</code> (MySQL database development files) with the following command:</p>
<pre><code># apt install mysql-server libmysqlclient-dev</code></pre>
<p>Start the database server daemon, and also enable it to start automatically at the next boot with the following commands:</p>
<pre><code># systemctl start mysql
# systemctl enable mysql</code></pre>
<p>Verify the status of the <code>MySQL</code> service using <code>systemctl status</code> command:</p>
<pre><code># systemctl status mysql</code></pre>
<p>Output:</p>
<pre><code>● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running)
   Main PID: 41039 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 2200)
     Memory: 358.9M
        CPU: 1.070s
     CGroup: /system.slice/mysql.service
             └─41039 /usr/sbin/mysqld
</code></pre>
<p>Once the database server is installed, log into the MySQL prompt:</p>
<pre><code>#  mysql -u root</code></pre>
<p>To create a database, database user, and grant all privileges to the database user run the following commands:</p>
<pre><code>mysql&gt; CREATE DATABASE django_db;
mysql&gt; CREATE USER 'django_user'@'localhost' IDENTIFIED BY 'Pa$$word';
mysql&gt; GRANT ALL ON django_db.* TO 'django_user'@'localhost';
mysql&gt; FLUSH PRIVILEGES;
mysql&gt; EXIT</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 4: Install Pip on Ubuntu 22.04</span></h2>
<p><a href="https://www.linuxtuto.com/how-to-install-python-3-10-on-ubuntu-20-04/"><strong>Python</strong></a> comes already installed by default on Ubuntu 22.04. You can verify it by checking its version:</p>
<pre><code># python3 -V</code></pre>
<pre><code>Output:
Python 3.10.6</code></pre>
<p>Use the following command to install <code>pip</code> and <code>venv</code> on Ubuntu 22.04:</p>
<pre><code># apt install python3-venv python3-pip</code></pre>
<p>Verify your Pip installation by checking its version:</p>
<pre><code># pip3 --version</code></pre>
<pre><code>Output:
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 5: Install Django Using Virtualenv</span></h2>
<p>First, create a directory and switch to it with the commands below:</p>
<pre><code># mkdir /var/www/django_project 
# cd /var/www/django_project</code></pre>
<p>Before you install Django, you first need to create a Python virtual environment.</p>
<pre><code># python3 -m venv django_env</code></pre>
<p>Next, activate the virtual environment with the following command:</p>
<pre><code># source django_env/bin/activate</code></pre>
<p>Next, install the latest  Django version using the following command:</p>
<pre><code>(django_env) # pip install django</code></pre>
<p>You will get the following output:</p>
<pre><code>Collecting django
  Downloading Django-4.1.1-py3-none-any.whl (8.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB 3.3 MB/s eta 0:00:00
Collecting asgiref&lt;4,&gt;=3.5.2
  Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)
Collecting sqlparse&gt;=0.2.2
  Downloading sqlparse-0.4.3-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.8/42.8 kB 2.0 MB/s eta 0:00:00
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.2 django-4.1.1 sqlparse-0.4.3</code></pre>
<p>Verify the Django version with the following command:</p>
<pre><code>(django_env) # django-admin --version</code></pre>
<p>You will get the following output:</p>
<pre><code>4.1.1</code></pre>
<p>Since you are setting a production site ensure you&#8217;ve python <code>mysqlclient</code> installed:</p>
<pre><code>(django_env) # pip install mysqlclient</code></pre>
<p>You will get the following output:</p>
<pre><code>Collecting mysqlclient
  Downloading mysqlclient-2.1.1.tar.gz (88 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.1/88.1 KB 1.3 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-2.1.1
</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 6: Creating your Django project</span></h2>
<p>Now you need to create Django project in <code>django_project</code> directory.</p>
<pre><code>(django_env) # django-admin startproject django_app .</code></pre>
<p>Next, modify <code>settings.py</code> file:</p>
<pre><code>(django_env) # nano django_app/settings.py</code></pre>
<p>Add the URL in <code>ALLOWED_HOST</code> List which is above <code>INSTALLED_APPS</code>.</p>
<pre><code>ALLOWED_HOSTS = ['your_server_ip', 'your-domain.com']</code></pre>
<p>The default database set in Django is SQLite. Replace SQLite database backend:</p>
<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}</code></pre>
<p>with MySQL database engine backend :</p>
<pre><code>DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'django_user',
'PASSWORD': 'Pa$$word',
'HOST': '127.0.0.1',
'PORT' : '3306',
}
}</code></pre>
<p>In order for our webserver to serve the static files add the following lines:</p>
<pre><code>import os

STATIC_URL='/static/'
STATIC_ROOT=os.path.join(BASE_DIR, 'static/') 

MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR, 'media/')</code></pre>
<p>Once done, save the file and exit the text editor.</p>
<p>Now, You can migrate the initial database schema to our MySQL database using the management script:</p>
<pre><code>(django_env) # ./manage.py  makemigrations
(django_env) # ./manage.py  migrate</code></pre>
<p>Create an administrative user for the project by typing:</p>
<pre><code>(django_env) # ./manage.py createsuperuser</code></pre>
<p>You will have to provide a username, an email address, and choose and confirm a password.</p>
<pre><code>Username (leave blank to use 'root'): <strong>admin</strong>
Email address: <strong>admin@your-domain.com</strong>
Password:
Password (again):
Superuser created successfully.</code></pre>
<p>You can collect all of the static content into the directory location you configured by running the command:</p>
<pre><code>(django_env) # ./manage.py collectstatic</code></pre>
<p>This will collect all the project static files in static directory. It will give some output like this:</p>
<pre><code>130 static files copied to '/var/www/django_project/static'.</code></pre>
<p>To deactivate the virtual environment run the following command:</p>
<pre><code>(django_env) # deactivate</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 7: Configure Apache Web Server for Django</span></h2>
<p>Navigate to <code>/etc/apache2/sites-available</code> directory and run the following command to create a configuration file for your installation:</p>
<pre><code class="hljs shell"><span class="bash"># nano /etc/apache2/sites-available/django.conf</span></code></pre>
<p>Add the following content:</p>
<pre><code>&lt;VirtualHost *:80&gt;

        ServerAdmin admin@your-domain.com
        ServerName your-domain.com
        ServerAlias www.your-domain.com

        DocumentRoot /var/www/django_project/

        ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
        CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

        Alias /static /var/www/django_project/static
        &lt;Directory /var/www/django_project/static&gt;
                Require all granted
        &lt;/Directory&gt;

        Alias /media /var/www/django_project/media
        &lt;Directory /var/www/django_project/media&gt;
                Require all granted
         &lt;/Directory&gt;

        &lt;Directory /var/www/django_project/django_app&gt;
                &lt;Files wsgi.py&gt;
                        Require all granted
                &lt;/Files&gt;
        &lt;/Directory&gt;

        WSGIDaemonProcess django_app python-path=/var/www/django_project python-home=/var/www/django_project/django_env
        WSGIProcessGroup django_app
        WSGIScriptAlias / /var/www/django_project/django_app/wsgi.py

&lt;/VirtualHost&gt;
</code></pre>
<p>Save the file and Exit.</p>
<p>Enable the Django virtual host:</p>
<pre><code># a2ensite django.conf</code></pre>
<p>Restart the Apache web server.</p>
<pre><code># systemctl restart apache2</code></pre>
<h2><span class="has-inline-color has-vivid-purple-color">Step 8: Access Django Project</span></h2>
<p>That’s it the production site has been setup. You can now access your application with your domain at e.g <code>http://your-domain.com</code></p>
<p><img fetchpriority="high" decoding="async" class="size-large wp-image-774 aligncenter" src="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-1024x486.jpg" alt="Django Project" width="1024" height="486" srcset="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-1024x486.jpg 1024w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-300x142.jpg 300w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-768x365.jpg 768w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-1536x729.jpg 1536w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-1222x580.jpg 1222w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-897x426.jpg 897w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project-684x325.jpg 684w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_project.jpg 1912w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<p>To access the admin dashboard, add <code>/admin/</code> to the end of your URL <code>http://your-domain.com/admin</code> This will take you to a log in screen:</p>
<p><img decoding="async" class="size-large wp-image-775 aligncenter" src="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-1024x339.jpg" alt="Django Admin" width="1024" height="339" srcset="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-1024x339.jpg 1024w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-300x99.jpg 300w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-768x254.jpg 768w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-1536x509.jpg 1536w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-1222x405.jpg 1222w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-897x297.jpg 897w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin-684x227.jpg 684w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_admin.jpg 1914w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<p>Enter your Django admin user, password and click on the<strong> Log in</strong>. You will get the Django administration dashboard in the following screen:</p>
<p><img decoding="async" class="size-large wp-image-777 aligncenter" src="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-1024x344.jpg" alt="Django Dashboard" width="1024" height="344" srcset="https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-1024x344.jpg 1024w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-300x101.jpg 300w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-768x258.jpg 768w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-1536x515.jpg 1536w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-1222x410.jpg 1222w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-897x301.jpg 897w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard-684x230.jpg 684w, https://www.linuxtuto.com/wp-content/uploads/2022/09/django_dashboard.jpg 1913w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<h2><span class="has-inline-color has-vivid-purple-color">Comments and Conclusion</span></h2>
<p>That’s it. You have successfully installed Django on Ubuntu 22.04 with Apache and WSGI. For additional information, you can check <a href="https://docs.djangoproject.com/" target="_blank" rel="noopener">the official Django documentation</a>.</p>
<p>If you have any questions please leave a comment below.</p>
<p>&nbsp;</p>
<p>The post <a href="https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/">How to Install Django with Apache on Ubuntu 22.04</a> appeared first on <a href="https://www.linuxtuto.com">LinuxTuto</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">757</post-id>	</item>
	</channel>
</rss>
