Digital Ocean Django To Apache Server Full Setup Step By Step

Apache to Django Devlopment Server Configaration

Posted by indranil on Jan. 3, 2021, 1:08 a.m.

Apache To Django Server Installation

On Windows Terminal Enter Cmd:

ssh your_id@server_ip

pass: your_pass

After Login Your Server

Step: 1 (Apache Server Installation)

sudo apt install

sudo apt update

sudo apt install apache2

sudo ufw app info

sudo ufw allow in "Apache Full"

________Server_Insatalled______

Step: 2 (Django Project Installation)

apt install python3-pip

apt install libapache2-mod-wsgi-py3

ufw enable

ufw allow ssh     /*For Terminal Access*/

sudo pip3 install virtualenv

Step: 3 (Setup Django Project )

cd /

cd var/www/

mkdir myproject

cd myproject/

virtualenv projectenv

source projectenv/bin/activate

pip install django

____Complete___

Step: 4 (Create Custom Django Project)

mkdir django-project

cd django-project/

django-admin startproject djangoproject  /Or/ Upload Your Project On FileZilla

pip install django-taggit

 

Step: 5 (Edit Settings.py For Server Installation)

DEBUG = False

ALLOWED_HOSTS = ['enter your server_ip']

Change your Template Directory

'DIRS': [

            os.path.join(BASE_DIR, "templates"),
            os.path.join(BASE_DIR, "djangoproject/templates")

 

        ],

Change your DataBase Directory

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': BASE_DIR / 'db.sqlite3',
        'NAME': '/var/www/myproject/django-project/db.sqlite3',
    }
}

Change your Static and Media

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')


MEDIA_URL="/media/"
#MEDIA_ROOT= os.path.join(BASE_DIR, "media")
MEDIA_ROOT= os.path.join(BASE_DIR, '/var/www/myproject/django-project/media')
#MEDIA_ROOT= ('/var/www/sitefly/django-project/media')

ON CMD: 

sudo ufw allow 8000

python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

python manage.py runserver 0.0.0.0:8000

______Django Successfully Install On 8000 Port___

Step: 6 (Configure Django To Apache Server)

Edit Your 000-default.conf(etc/apache2/sites-available/000-default.conf)

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    #DocumentRoot /var/www/html
    

    Alias /static /var/www/myproject/django-project/static
    Alias /media/ /var/www/myproject/django-project/media/

    <Directory /var/www/myproject/django-project/static>
       
        Require all granted
     
    </Directory>

    <Directory /var/www/myproject/django-project/media>

        Require all granted

    </Directory>


    <Directory /var/www/myproject/django-project/djangoproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>



    WSGIDaemonProcess var/www/myproject python-home=/var/www/myproject/projectenv python-path=/var/www/myproject/django-project 
    WSGIProcessGroup var/www/myproject
    WSGIScriptAlias / /var/www/myproject/django-project/djangoproject/wsgi.py

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
 

Step: 7 (Configure Apache Server)

cd /

cd etc/apache2/sites-available/

sudo a2dissite 000-default.conf

sudo a2ensite 000-default.conf

sudo service apache2 restart

_____Apache Installation Complete____

Step: 8(Permission Grantted)

cd /

cd var/www/myproject/

source projectenv/bin/activate

cd django-project/

python manage.py collectstatic

chmod 664 db.sqlite3

sudo chown www-data: db.sqlite3

cd ../

sudo chown www-data: django-project/db.sqlite3

sudo chown www-data: django-project/media/

sudo chown www-data: django-project/

sudo service apache2 restart

______XXXXXXX_______

Go to Django Project File

Delete static Folder

Change Rename staticfiles to static

Any Error Problem Please

      Leave A message

_______XXXXXX_______

noimg Visit Link: digitalocean.com