How to do it...

Follow these steps to deploy a Django project on a Linux server with Virtualmin:

  1. Log into Virtualmin as the root user and set bash (instead of sh) as the default shell for the server's users. This can be done by navigating to Virtualmin | System Customization | Custom Shells, as shown in the following screenshot:

  1. Create a virtual server for your project by navigating to Virtualmin | Create Virtual Server. Enable the following features: Setup website for domain? and Create MySQL database?. The Custom username and Administration password that you set for the domain will also be used for the SSH connections, FTP, and MySQL database access, as follows:

  1. Log into your domain administration panel and set the A record for your domain to the IP address of your dedicated server.
Due to the delays related to DNS propagation, it can be several hours before a new domain mapping takes effect in all parts of the globe. In the interim, it may only be accessible via the IP address, directly.
  1. Connect to the dedicated server via Secure Shell (SSH) as the root user, and install the Python libraries, pip, virtualenv, MySQLdb, and Pillow, system wide.
  2. Ensure that the default MySQL database encoding is UTF-8. First, we must edit the MySQL configuration file on the remote server. For example, we can connect via SSH and open the configuration file using the nano editor, as follows:
$ ssh [email protected]
[email protected]'s password:
<[email protected]>$ nano /etc/mysql/my.cnf

Once it has opened, we have to add (or edit) the following configurations:

# /etc/mysql/my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server=utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server=utf8

Press Ctrl + O to save the changes, and Ctrl + X to exit the nano editor. Once the configuration is saved, restart the MySQL server, as follows:

<[email protected]>$ /etc/init.d/mysql restart

Finally, press Ctrl + D to exit the SSH connection, or type the exit command, as follows:

<[email protected]>$ exit
$
  1. When you create a domain with Virtualmin, the user for that domain is created automatically. Connect to the dedicated server via SSH as a user of your Django project and create a virtual environment for your project, as follows:
$ ssh [email protected] 
[email protected]'s password:
<[email protected]>$ virtualenv . --system-site-packages <[email protected]>$ echo source ~/bin/activate >> .bashrc <[email protected]>$ source ~/bin/activate (myproject)myproject@server$
The .bashrc script will be called each time you connect to your Django project via SSH as a user related to the domain. The .bashrc script will automatically activate the virtual environment for this project.
  1. If you host your project code on Bitbucket, you will have to set up SSH keys, in order to avoid password prompts when pulling from or pushing to the Git repository. To do so, execute the following commands, one by one:
(myproject)myproject@server$ ssh-keygen 
(myproject)myproject@server$ ssh-agent /bin/bash 
(myproject)myproject@server$ ssh-add ~/.ssh/id_rsa 
(myproject)myproject@server$ cat ~/.ssh/id_rsa.pub 

The last command prints your SSH public key, which you need to copy and paste into the form, under Settings | General | Access Keys | Add Key, for your repository on the Bitbucket website, as shown in the following screenshot:

  1. Create a project directory, go to it, and clone your project's code, as follows:
(myproject)myproject@server$ git clone 
> [email protected]:somebitbucketuser/myproject.git myproject
Now, your project path should be something similar to the following:
/home/myproject/project/myproject.
  1. Install the Python requirements for your project, including a specified version of Django 2.1 (or newer), as follows:
(myproject)myproject@server$ pip install -r requirements.txt 
  1. Create the media, tmp, and static directories, under your project's directory.
  2. Also, create local_settings.py, with settings similar to the following, or use one of the other approaches to environment-specific settings that were mentioned in Chapter 1, Getting Started with Django 2.1:
# /home/myproject/project/myproject/myproject/local_settings.py
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "myproject",
"USER": "myproject",
"PASSWORD": "mypassword",
}
}
PREPEND_WWW = True
DEBUG = False
ALLOWED_HOSTS = ["myproject.com"]
  1. Import the database dump that you created locally. If you are using a macOS, you can do that with an app such as Sequel Pro (http://www.sequelpro.com/), using an SSH connection. You can also upload the database dump to the server by FTP, and then run the following in SSH:
(myproject)myproject@server$ python manage.py 
> dbshell <
~/db_backups/db.sql
  1. Collect static files, as follows:
(myproject)myproject@server$ python manage.py collectstatic --noinput 
  1. Go to the ~/public_html directory and create a wsgi file, using the nano editor (or an editor of your choice):
# /home/myproject/public_html/my.wsgi
#!/home/myproject/bin/python

import os, sys, site

django_path = os.path.abspath(
os.path.join(os.path.dirname(__file__),
"../lib/python2.6/site-packages/"),
)
site.addsitedir(django_path)

project_path = os.path.abspath(
os.path.join(os.path.dirname(__file__),
"../project/myproject"),
)
sys.path += [project_path]

os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  1. Then, create the .htaccess file in the same directory. The .htaccess file will redirect all of the requests to your Django project set in the wsgi file, as follows:
# /home/myproject/public_html/.htaccess
AddHandler wsgi-script .wsgi
DirectoryIndex index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ /my.wsgi/$1 [QSA,L]
  1. Copy .htaccess as .htaccess_live.
  2. Then, create .htaccess_maintenace for maintenance cases. This new Apache configuration file will show temporarily-offline.html for all of the users (except for you, recognized by the IP address of your LAN or computer). The following code snippet shows how the .htaccess_maintenance will look:
# /home/myproject/public_html/.htaccess_maintenance
AddHandler wsgi-script .wsgi
DirectoryIndex index.html

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.2.3.4$
RewriteCond %{REQUEST_URI} !/temporarily-offline.html
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule .* /temporarily-offline.html [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ /my.wsgi/$1 [QSA,L]
Replace the IP digits in this file, 1.2.3.4, with your own IP. You can check your IP address by googling "what's my IP," as in https://www.google.com/search?q=whats+my+ip
  1. Then, create an HTML file that will be shown when your website is down. The following is a very simple version:
<!-- /home/myproject/public_html/temporarily-offline.html -->
The site is being updated... Please come back later.
  1. Log into the server as the root user via SSH, and edit the Apache configuration. To do so, open the domain configuration file, as follows:
<[email protected]>$ nano 
> /etc/apache2/sites-available/myproject.mydomain.conf

Add the following lines before </VirtualHost>:

Options -Indexes
AliasMatch ^/static/d+/(.*) "/home/myproject/project/myproject/static/$1"
AliasMatch ^/media/(.*) "/home/myproject/project/myproject/media/$1"
<FilesMatch ".(ico|pdf|flv|jpe?g|png|gif|js|css|swf)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FilesMatch>

Restart Apache for the changes to take effect:

<[email protected]>$ /etc/init.d/apache2 restart 
  1. Set the default scheduled cron jobs. For more information on how to do this, refer to the Setting up cron jobs for regular tasks recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.103.77