<\/span><\/h2>\r\n\r\n\r\n\r\nDjango has been installed under user “rose”, now let’s create a project<\/p>\r\n\r\n\r\n\r\n
cd ~\r\ndjango-admin startproject one<\/pre>\r\n\r\n\r\n\r\nThe command will create a project named “one”, and this will also create a “one” directory in \/home\/rose\/ Next, run the following commands to start the new project<\/p>\r\n\r\n\r\n\r\n
cd one\r\npython manage.py migrate\r\npython manage.py createsuperuser\r\npython manage.py runserver 0.0.0.0:8000<\/pre>\r\n\r\n\r\n\r\n\r\n
<\/figure>\r\n<\/div>\r\n\r\n\r\n\r\nWhen creating the superuser, you will be asked for a password and email, please provide them with the credentials as you wish.<\/p>\r\n\r\n\r\n\r\n
Now, open your favorite web browser and navigate to your IP_address:8000. In this example, we install Django on 192.168.1.231 so we need to go to http:\/\/192.168.1.231:8000<\/p>\r\n\r\n\r\n\r\n
\r\n
<\/figure>\r\n<\/div>\r\n\r\n\r\n\r\nIf you see an error message like the following, then you need to edit the ALLOWED_HOSTS value in settings.py file<\/p>\r\n\r\n\r\n\r\n
DisallowedHost at \/ Invalid HTTP_HOST header: '192.168.1.231'. You may need to add u'192.168.1.231' to ALLOWED_HOSTS.<\/pre>\r\n\r\n\r\n\r\nTo edit the ALLOWED_HOSTS value in settings.py file, you can run the following command.<\/p>\r\n\r\n\r\n\r\n
nano ~\/one\/one\/settings.py<\/pre>\r\n\r\n\r\n\r\nALLOWED_HOSTS = ['192.168.1.231','127.0.0.1','yourdomain.com']<\/pre>\r\n\r\n\r\n\r\nSave the file and exit, then rerun the application again:<\/p>\r\n\r\n\r\n\r\n
python manage.py runserver 0.0.0.0:8000<\/pre>\r\n\r\n\r\n\r\nYou can reach Django administration page at http:\/\/192.168.1.231:8000\/admin, use the credentials you chose when creating Django superuser<\/p>\r\n\r\n\r\n\r\n
To manage the Django application better, we need to install gunicorn. Gunicorn is a python web server gateway interface HTTP server. It is a pre-fork worker model, ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources, and fairly fast.<\/p>\r\n\r\n\r\n\r\n
pip install gunicorn\r\ndeactivate\r\nexit<\/pre>\r\n\r\n\r\n\r\nNow we will create a systemd service file to start and stop the application server.<\/p>\r\n\r\n\r\n\r\n
nano \/etc\/systemd\/system\/gunicorn.service<\/pre>\r\n\r\n\r\n\r\nThen, insert the following lines to the systemd service file.<\/p>\r\n\r\n\r\n\r\n
[Unit]\r\nDescription=gunicorn daemon\r\nAfter=network.target\r\n\r\n[Service]\r\nUser=rose\r\nGroup=nginx\r\nWorkingDirectory=\/home\/rose\/one\r\nExecStart=\/home\/rose\/django\/bin\/gunicorn --access-logfile - --workers 3 --bind unix:\/home\/rose\/one\/one.sock one.wsgi:application\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\r\n\r\n\r\n\r\nSave and exit nano, then issue the following command to reload systemd service file.<\/p>\r\n\r\n\r\n\r\n
systemctl daemon-reload<\/pre>\r\n\r\n\r\n\r\nNow, we can start-stop-restart Django application using systemctl command<\/p>\r\n\r\n\r\n\r\n
systemctl start gunicorn<\/pre>\r\n\r\n\r\n\r\nIf you want to run it on boot, we need to enable it<\/p>\r\n\r\n\r\n\r\n
systemctl enable gunicorn<\/pre>\r\n\r\n\r\n\r\nGunicorn has been successfully configured, now if you want to access the application using your domain name and remove the port number in your favorite web browser address bar, we need to install and configure a webserver. This time, we will install and configure nginx to proxy pass to gunicorn.<\/p>\r\n\r\n\r\n\r\n
yum install nginx<\/pre>\r\n\r\n\r\n\r\nLet’s create an nginx server block file, make sure you change yourdomain.com to your actual domain name.<\/p>\r\n\r\n\r\n\r\n
nano \/etc\/nginx\/conf.d\/yourdomain.com.conf<\/pre>\r\n\r\n\r\n\r\nserver {\r\nlisten 80;\r\nserver_name yourdomain.com;\r\n\r\nlocation = \/favicon.ico { access_log off; log_not_found off; }\r\nlocation \/static\/ {\r\nroot \/home\/rose\/one;\r\n}\r\n\r\nlocation \/ {\r\nproxy_set_header Host $http_host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nproxy_set_header X-Forwarded-Proto $scheme;\r\nproxy_pass http:\/\/unix:\/home\/rose\/one\/one.sock;\r\n}\r\n}\r\n<\/pre>\r\n\r\n\r\n\r\nSave and exit once finished.<\/p>\r\n\r\n\r\n\r\n
nginx -t<\/pre<\/pre>\r\n\r\n\r\n\r\nsystemctl start nginx\r\nsystemctl enable nginx<\/pre>\r\n\r\n\r\n\r\nTo give permissions to nginx to access your Django application, we have to add nginx user to a user group that run Django<\/p>\r\n\r\n\r\n\r\n
usermod -a -G rose nginx<\/pre>\r\n\r\n\r\n\r\nThen, we also need to change the directory permission of Django user’s home.<\/p>\r\n\r\n\r\n\r\n
chmod 710 \/home\/rose<\/pre>\r\n\r\n\r\n\r\nsystemctl restart nginx<\/pre>\r\n\r\n\r\n\r\n