Skip to content

Instantly share code, notes, and snippets.

@alz-ahm
Last active October 19, 2021 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alz-ahm/65091cdd0e4b877c88165c0657ad964e to your computer and use it in GitHub Desktop.
Save alz-ahm/65091cdd0e4b877c88165c0657ad964e to your computer and use it in GitHub Desktop.
Running Django and Nginx on CentOS 7

How to run Django and Nginx on CentOS 7

I used this steps to install django on centOs for more than 10 times and it worked for me but it may not contain all the details you may need in your project.

Configure connections

nmtui

  • Edit a connection
  • fill connection information:
    • set IPv4 to Manual
    • set IPv4 address (172.16.20.xxx)
    • set Gateway to 172.16.20.2
    • check Automatically connect
  • go back
  • Enter Activate a connection
  • ( Deactivate & ) Activate the connection
  • Back & Quit

chkconfig sshd on service sshd start

it's done!

try to ssh to root@185.12.103.xxx

Setting proxy

If the server is in Iran set proxy setting to yum. Some of repositories blocked iran IPs due to US Sanctions.

add this line at the end of /etc/yum.conf

proxy=https://us.mybestport.com:443
proxy_username=user
proxy_password=pass

installing server packages

sudo yum -y update
sudo yum -y install yum-utils
sudo yum -y groupinstall development
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python36u
sudo yum -y install python36u-pip
sudo yum -y install python36u-devel
sudo yum -y install git
sudo yum -y install tmux
sudo pip3.6 install virtualenv

installing MySQL Server:

sudo yum -y install wget
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm
sudo yum -y install mysql-server
service mysqld start
sudo grep 'temporary password' /var/log/mysqld.log        ###copy root password from this command
sudo mysql_secure_installation                            ###after changing password, press Y & Enter for all questions

# Add user & database
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'username'@'localhost';
CREATE DATABASE db_name CHARACTER SET utf8 COLLATE utf8_general_ci; #make sure utf8 is set otherwise persian text can not be added.

installing project requirements

git clone <project-git-repo>
cd <project>
virtualenv -p python3.6 venv
. venv/bin/activate
pip3 install -r requirements.txt

add ports to firewall:

firewall-cmd --get-active-zones                             ###get zone it's probebly public
firewall-cmd --zone=public --add-port=<port>/tcp --permanent
firewall-cmd --reload

installing uWSGI

  • make sure python36u-devel is installed properly
  • go to virtualenv directory . venv/bin/activate
  • pip3 install uwsgi (make sure you are using pip3) which uwsgi should show the path to your virtual env
  • add project.ini file containing configs for uwsgi
[uwsgi]
chdir = /root/projects/ashpazi
home  = /root/projects/venv
module = ashpazi.wsgi:application

master = true
processes = 5

uid = root
gid = root
env = DJANGO_SETTINGS_MODULE=ashpazi.settings
socket = /root/projects/sockets/ashapazi.sock
chmod-socket = 777
vacuum = true
  • cd inside project root then run

  • uwsgi --http :8000 --module ashpazi.wsgi --ini ../ini_files/ashpazi.ini

  • open the browser and make sure uwsgi works

  • making sure uwsgi runs automatically on system startup by adding this line to /etc/rc.local

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

installing nginx

  • sudo yum install nginx

  • sudo systemctl start nginx

  • sudu chkconfig nginx on (making sure it starts on boot)

  • adding uwsgi_params to root directory of project. can be copied from here https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

  • adding website config file to /etc/nginx/conf.d/project.conf (add file and folder if not exists) sample config file

upstream django_4goosh {
    #server unix:///root/projects/sockets/ashpazi.sock;
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)

}

server {
    listen  8000;
    listen  [::]:8000 default_server ipv6only=on;

    #server_name 4goosh.ir;
    #charset     utf-8;

    client_max_body_size 5G;

    location /media  {
        alias /root/projects/ashpazi/media;
    }

    location /static {
        alias /root/projects/ashpazi/static;
    }

    location / {
        uwsgi_pass  django_4goosh;
        include     /root/projects/ashpazi/uwsgi_params;
    }
}
  • cd in /etc/nginx/conf.d/ and run setenforce 0 to avoid permission error on nginx
  • if puting config in config.d folder doesn't work put it inside /etc/nginx/site-enabled/ and then include this 2 lines after http block in /etc/nginx/nginx.conf
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
  • this makes sure our config file gets load by nginx

  • sudo systemctl restart nginx

  • run uwsgi with port specified

uwsgi —-socket :8001 —-module ashpazi.wsgi —-ini ../ini_files/ashpazi.ini  
  • add nginx to root group
sudo usermod -a -G user nginx
  • change owner of media and static directories if neccessary
chown -R nginx:nginx media
chown -R nginx:nginx static
  • restart nginx & uwsgi and run again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment