Yunohost is a self hosting solution for users who want to run there own servers without have knowledge of the server administration. To know more about Yunohost
see here.
I am using
custom web app for the installation of the
Django because
Yunohost do not have an official package for
installing Django.
- Install the custom web app:
$ yunohost app install https://github.com/YunoHost-Apps/my_webapp_ynh --debug
- In-order to have Django within virtual environment we need to execute virtualenv command. We can install virtualenv through pip. First we install pip and then trough pip install virtualenv.
$ sudo apt-get install python-pip
$ sudo pip install virtualenv
- We have our virtualenv ready, so we can go to our project directory to execute it.
$ cd /var/www/my_webapp/www/
- Remove the index.html in the project directory.
$ rm index.html
- Within this project directory, create a Python virtual environment for Python by typing:
$ virtualenv myproject_env
- By executing this command a folder named myproject_env will be created in the directory. It will be used to install and configure different python environments.
Now to activate this virtual environment, execute following command:
$ source myproject_env/bin/activate
- With your virtual environment activated. We can move forward and install, Gunicorn.
$ pip install django gunicorn
- Now with virtual environment been activated. Start Django project using Django management command.
$ django-admin.py startproject myproject .
Note: Don't ignore the trailing dot in the end.
- Now we edit the settings.py file located inside myproject folder. We use Nano to do it.
$ cd myproject
$ nano settings.py
Replace the following code inside settings.py.
ALLOWED_HOSTS = ['domain_where_custom_web_app_installed.tld',] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Use ctrl+x to close the Nano. Save the file by typing "y" when asked.
- We will need to install the Python and MySQL development headers and libraries:
$ sudo apt-get install python-dev libmysqlclient-dev
$ pip install mysqlclient
- Open again settings.py file located under myproject folder.
Enter your database details which are available in /var/www/my_webapp/db_access.txt
$ cat /var/www/my_webapp/db_access.txt
Copy the database password from here.
Open the settings.py again.
$ sudo nano settings.py
Replace the password which you got from above command at the place of PASSWORD.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my_webapp', 'USER': 'my_webapp', 'PASSWORD': 'password in db_access.txt', 'HOST': 'localhost', 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", }, 'PORT': '', } }
- Run commands:
$ ./manage.py makemigrations ./manage.py migrate ./manage.py createsuperuser ./manage.py runserver 0.0.0.0:8000
- For testing in web browser, enter example.com:8000 and press enter or in command line do:
$ curl example.com:8000
Once you had finished exploring recently created project, hit ctrl+c.
- Now let’s test Gunicorn interface.
$ gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
This will start Gunicorn on same interface on which our earlier development server was working.Test the Gunicorn as described in step 13.
- Now type this command to deactivate the virtual environment:
$ deactivate
- Now we have to create Gunicorn systemd service file.This service file will be used to start or stop our application server. Open the gunicorn systemd service file using this command.
$ sudo nano /etc/systemd/system/gunicorn.service
- Add these lines to the service file.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/my_webapp/www
ExecStart=//var/www/my_webapp/www/myproject_env/bin/gunicorn --workers 3 --bind unix:/var/www/my_webapp/www/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
- Save and close the file. Start the Gunicorn service using following command.
$ sudo systemctl start gunicorn
$ sudo systemctl enable gunicorn
This will create socket file inside your project directory. Always check that the socket file has www-data group and this group should have ownership for the group.
- Edit etc/nginx/conf.d/domain_where_custom_web_app_installed.tld.d/my_webapp.conf
location / {
include proxy_params;
proxy_pass http://unix:/var/www/my_webapp/www/myproject.sock;
# Prevent useless logs
location = /favicon.ico {
log_not_found off;
access_log off;
}
location /static/ {
root /var/www/my_webapp/www;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#Deny access to hidden files and directories
# location ~ ^/(.+/|)\.(?!well-known\/) {
# deny all;
}
# Include SSOWAT user panel.
include conf.d/yunohost_panel.conf.inc;
}
- Test Nginx configuration for syntax errors by typing:
$ sudo nginx -t
If no error is found, restart Nginx by typing:
$ sudo systemctl restart nginx
That's it!Now you should have a running instance of the
Django on Yunohost.
Note: Components which are installed under
virtual environment are isolated from other os global environments. After adding each static file in your app migrate it with:
$ sudo python manage.py collectstatic
$ sudo service gunicorn restart
I have not tested the backup and restore script. So backup manually all your project before trying the web app backup and restore.#
yunohost #
Django #
Python #
Hosting