Go to: Compute Engine > VM instances > Create account as your need
- In Firewall allow HTTP traffic and HTTPS traffic
- Create
- Metadata > SSH KEYS
ssh-keygen -f ~/.ssh/key_name -t rsa -b 4096
- Enter with you passphrase
cat ~/.ssh/key_name.pub
- Copy and past on GCP - SSH KEYS
- Open the server ssh User@External IP
- Are you sure you want continu connecting (yes/no)? yes
Download PuTTY - https://www.putty.org/ (When you do dawnload it installs 2 app, PuTTY and PuTTYgen)
- Open PuTTYgen > Generate (you need to keep moving your cursor mouse)
- In Key comment add you User
- Copy and past on GCP - SSH KEYS
- In PuTTYgen click in Save private key
- GCP > VM instances > (copy)External IP
- Open PuTTY > In host Name put your User@External IP
- In Saved Sessions put GOOGLE SERVER and Save
- Will be generated an app, when you access will open the server
- VM instances > More actions > View network details
- IP addresses > RESERVE (reseve a static ip) > Put you VM instance name > RESERVE
- In settings.py add:
STATIC_ROOT = os.path.join('static')
- In Terminal:
python manage.py collectstatic
ssh User@External IP
- Install Libraries
sudo apt update -y
sudo apt upgrade -y
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3-pip python3.10 python3.10-dev python3.10-venv gcc default-libmysqlclient-dev libssl-dev nginx curl
python3.10 -m pip install --upgrade pip setuptools wheel --user
export PATH="/home/$USER/.local/bin:$PATH"
python3.10 -m pip install pipenv --user
mkdir contactlist
- Open directory
cd contactlist
python3.10 -m venv venv
source venv/bin/activate (To exit the virtual environment just type deactivate)
python3.10 -m pip install django gunicorn pillow
Download FileZilla (https://filezilla-project.org/download.php?type=client)
- FileZilla > Host: sftp://User@External IP
- In the right area look for your server folder
- In the left area look for the project folder on your computer
- Testing gunicorn:
gunicorn --bind 0.0.0.0:8000 contact_list.wsgi
- Ctrl + C to stop the test
sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=#########USER############
Group=www-data
WorkingDirectory=/home/#########USER############/#########PROJECT############
ExecStart=/home/#########USER############/#########PROJECT############/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
#########PROJECT############.wsgi:application
[Install]
WantedBy=multi-user.target
![print8](https://github.com/luiswolski/Make_Your_First_Django_Deploy/blob/main/Prints/print8.png)
- Ctrn+O > Enter > Ctrl+X
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
sudo systemctl status gunicorn.socket
sudo systemctl status gunicorn (if is dead try the next command, you will try this again)
curl --unix-socket /run/gunicorn.sock localhost
sudo systemctl status gunicorn
sudo nano /etc/nginx/sites-enabled/sitedjango
- Edit before pasting
server {
listen 80;
server_name localhost;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/luiswolski/contactlist;
}
location /media {
alias /home/luiswolski/contactlist/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx
sudo systemctl restart gunicorn
nano contactlist/contact_list/settings.py
- Put you ip in ALLOWED_HOSTS
- Ctrn+O > Enter > Ctrl+X
sudo systemctl restart gunicorn
- First install mysql/mariadb
cd your_project/your_project
sudo apt-get update
sudo apt-get install mariadb-server python3.10-dev libmysqlclient-dev
- If does't work try this:
sudo apt-get -f install
sudo apt-get update
sudo apt-get -f install
sudo dpkg --configure -a
sudo apt-get clean
- And try again
sudo apt-get install mariadb-server python3.10-dev libmysqlclient-dev
- User cursopython, password curs0Pyth0n@169, bd sitedjango
sudo mysql -u root
USE mysql;
UPDATE user SET plugin='' WHERE User='root';
CREATE DATABASE sitedjango CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'cursopython'@'%' IDENTIFIED BY 'curs0Pyth0n@169';
GRANT ALL PRIVILEGES ON sitedjango.* TO 'cursopython'@'%';
FLUSH PRIVILEGES; Exit;
source /home/$USER/contactlist/venv/bin/activate
pip install mysqlclient
cd /home/$USER/contactlist/
python manage.py dumpdata > db.json
nano /home/$USER/contactlist/contact_list/settings.py
- Ctrn+O > Enter > Ctrl+X
cd /home/$USER/contactlist/
python manage.py migrate
python manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
python manage.py loaddata db.json
sudo systemctl restart nginx && sudo systemctl restart gunicorn
rm db*