How To Setup Django Site In Centos Vps

by abhilash - March 15, 2021 coding

1) Buy a VPS Server

for setting up django web app in centos vps system you have to buy vps server from vps provider it will give you server ip address and root password  also you will get  client web gui for management of server from that you get access to  reset server ,reinstall server or boot sever into safe mode and  from there you can even change the root password

2) ssh the server

now ssh the sever from any ssh client like putty ,cmd or mobxtrem,etc.

ssh root@192.168.47.101 ---> give root password of server

after that you will get the full access that server

3)Change the root password

once you login to ssh server you should change  your root password by passwd commnad

passwd root --> give new password two times

4) add a new user

add a new user so you dont needed to use root for all the times it will make your server more secure 

useradd test  

passwd test -->  give new password two times

now add that test user to wheel group so you can use sudo command by sudo command you can access all system  command by that new user 

usermod -G wheel test

5) Configure ssh

configure ssh so  no one can login to the system via root  , edit  ssh configuration file and write PermitRootLogin to No

 

vim   /etc/ssh/sshd_config

       PermitRootLogin to No

now reboot the system and login via ssh  by ssh user@server

 

6)update the system 

now update the system then reboot the system

e.g yum update && reboot

7)Install python3

after sucessfully update install python3

yum install python3

8)install and activate virtual enviroment

after installation of python3 we can use python pip package installer by using pip we can install and create virtual enviroment

by using virtual enviroment we can install python packages virtually that means they only available when we activate the virtual enviroment 

 

pip3 install virtualenv

9)activate virtual enviroment

for activation of virtual enviroment  create new directory use source command to activate it

mkdir /djangoenv

virtualenv /djangoenv

source /django/bin/activate

10)install python packages

first create the requirement.txt ,in that file write list of names of python packages

now install it by 

pip install -r requirements.txt

 

11) install sqlite3.8 

for django to work in python3  in centos it need the sqlite version 3.8

wget https://kojipkgs.fedoraproject.org//packages/sqlite/3.8.11/1.fc21/x86_64/sqlite-3.8.11-1.fc21.x86_64.rpm

sudo yum install sqlite-3.8.11-1.fc21.x86_64.rpm

 

12)copy django application to server

copy django application to server by using scp or sftp

scp djangoapplication.zip user@server

 

13)test the django application

python3 manage.py runserver

 

14)install apache webserver

install apache webserver

yum install httpd*

 

15) configure apache server

go to /etc/httpd/conf.d 

create web.conf file

type configuration

<virtual host *:80>

servername yoursitename

alias /static  /your static directory in django application

alias /media /your media directory in django application

<directory  /your static direcory >

require all granted

</directory>

<directory  /your media directory >

require all granted

</directory>

<directory /your main app directory where wsgi file located>

<files wsgi.py>

require all granted

</files>

</directory>

WSGIDaemonProcess name_of_your_project python-path = /directory of project

python-home = /directory of enviroment variables'

WSGIProcessGroup  name_of_project

WSGIScriptAlias / /your django app directory

</virtualhost>

save and exit

if there is numpy python package in your django app then it will gives time out so add following line to

/etc/httpd/conf/httpd.conf

WSGIApplicationGroup % {GLOBAL}

save and exit

 

16)Restart apache server

test apache config by 

apachectl configtest

restart apache server 

systemctl restart httpd

 

17)go to web browser check your site 

Done.