Hello, everyone. This is the latest edition of Learning Python. What can YOU do? Django tutorials from scratch to finally successfully deploy live projects. This section, it’s all dry stuff! It’s all dry! Suggest collection and preservation!! Because we go into super detail on how to deploy our website to the server.
Peekpa.com official address: Peekpa.com
PI ye each article, are configured with the corresponding code. There is no code for this article. hahahaha
In the last section, we have successfully purchased ali Cloud server, and the server system is CentOS 7.7 64-bit system.
In this section, we will use 21 steps to deploy our Django project to an online server using Nginx + uWsgi + Supervisor.
Deployment steps tedious trouble, we must follow every step, this article is the most complete deployment on the web article, and step by step to show you how to deploy. So, please study hard.
1/21 Log in to the server
In the last article we purchased a server with a public IP of 39.104.203.202:
So, we use SSH to log in to the server, Mac OS and Linux users, directly with the system’s own terminal can be; For Windows users, you can use putty to log in:
$SSH [email protected]
Copy the code
A successful login appears like this:
2/21 install Git
Git is not installed on CentOS. The purpose of installing Git is to download our code from Github.
$ sudo yum install -y git
Copy the code
After a successful installation, it looks like this:
3/21 Download the source code
Once Git is installed, we’ll download our PeekpaCom project from Github.
The PeekpaCom project is located at github.com/SwyftG/Peek…
To download the source code, run the following command:
$ git clone https://github.com/SwyftG/PeekpaCom.git
Copy the code
When the download is complete, it looks like this:
4/21 to install Python
Our project is running in Python 3, but the Python version in the system is 2.7.5
So we use the following command to install Python 3
#Install Python
$ sudo yum install -y python3
#Checking the Python version
$ python3 -V
Copy the code
As you can see, after the installation is complete, we check the following Python 3 version, which is 3.6.8
5/21 Installing the virtual environment
As we explained in the first lesson, we developed the entire project using the Python virtual environment, so we needed to install the virtual environment on the server to maintain consistency. Enter them one by one using the following commands:
#Update the PIP
$ sudo pip3 install --upgrade pip
Copy the code
#Install virtualenv
$ sudo pip3 install virtualenv
Copy the code
#Install PBR first and then VirtualenWrapper
$ sudo pip3 install pbr
$ sudo pip3 install virtualenvwrapper
Copy the code
6/21 Set the virtualenv parameter
Virtualenv: bashrc: bashrc: bashrc: bashrc: bashrc: bashrc: bashrc: bashrc
$ sudo vi ~/.bashrc
Copy the code
Once inside, move the cursor to the end of the file and press “I” again to enter vi insert mode. Copy and paste the following contents to the end of the file:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
Copy the code
Press Esc, press:, and enter wq! And press Enter to exit vi.
Then use the following command to put the bashrc file into effect:
$ source ~/.bashrc
Copy the code
7/21 Installing dependencies
Install dependencies
yum install mysql-devel gcc gcc-devel python36-devel python-devel
Copy the code
8/21 Create a virtual environment
Then create the virtual environment with the following command:
#Create the virtual environment Peekpa
$ mkvirtualenv peekpa
#Switch to the virtual environment
$ workon peekpa
Copy the code
Notice (Peekpa) on the left, indicating that the current working environment is inside the virtual environment peekpa.
9/21 Installation works dependency
PeekpaCom > PeekpaCom > PeekpaCom > PeekpaCom
$ cd PeekpaCom/
$ pip install -r requirement.txt
Copy the code
Then we rolled out virtual environments:
$ deactivate
Copy the code
10/21 MySQL installation
In the system environment, we will install MySQL 8.0. Be sure to follow these commands, one by one:
#1> Set the RPM file
$ sudo rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
#2 > the repo
$ sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo
#3 > the MySQL installation
$ sudo yum --enablerepo=mysql80-community install mysql-community-server
Copy the code
#4> Start the MySQL service
$ sudo service mysqld start
#5> Obtain the initial password
$ sudo grep "A temporary password" /var/log/mysqld.log
Copy the code
The initial password will be displayed, and we will initialize MySQL
#6> Change the initial password
$ sudo mysql_secure_installation
Copy the code
You need to set the root password, input the initial password given by the above system, and then set a complex password of your own system, and complete the configuration step by step (basically Y), and finally prompt success.
MySQL > select * from ‘MySQL’;
#7 > restart MySQL
$ sudo service mysqld restart
#8 > login
$ sudo mysql -u root -p
Copy the code
We need to have a look at the contents of the database, and creates a we PeekpaCom/Peekpa/Settings. Py files in the corresponding database inside the DATABASES. Since our database name in this variable is PEekpa_db1, we will create a database named peekpa_db1
#9> Display the database
> show databases;
#10> Create peekpa_db1 database
> CREATE DATABASE peekpa_db1;
#11> Display the database
> show databases;
Copy the code
Next, we need to modify the MySQL configuration with the following command:
#12> Switch to mysql
> use mysql;
#13> Display root information
> select user,plugin from user where user='root';
#14> Change the root password to the password you set earlier'yourpasswordxxxxxx'That corresponds to your password
> alter user 'root'@'localhost' identified with mysql_native_password by 'yourpasswordxxxxxx';
#15> clear the cache
> flush privileges;
#16 exit MySQL >
> quit;
Copy the code
11/21 Modified projectsettings.py
Next, we need to change the DEBUG,ALLOWED_HOSTS, and DATABASES passwords in Django’s settings.py file
$ cd Peekpa
$ sudo vi settings.py
Copy the code
Modify the data in settings.py to look like this:
DEBUG = False
ALLOWED_HOSTS = ['peekpa.com'.'39.104.203.202'[DATABASES = {DATABASES}Copy the code
ALLOWED_HOSTS: Enter the domain name in the first field and the public IP address of your server in the second field. If the request comes from either of these places, Django will process it, otherwise Django will reject it.
Then we enter the virtual environment and need to execute the migrate command to map the Django models to the database:
#Enter the Virtual Environment
$ workon peekpa
#Go back to the PeekpaCom directory
$ cd.
#Execute migrate command
$ python manage.py migrate
Copy the code
12/21 to create a SuperUser
Next we need to create superuser:
$ python manage.py createsuperuser
Copy the code
13/21 Starting the service
Then we start the service:
$Python manage. Py runserver then executes 0.0.0.0:80
Copy the code
Enter your server’s public IP address http://39.104.203.202/ in your browser and see:
If you see this, everything is fine on the server, but our static resource file is missing.
14/ Collect static files
To exit djangos service, use the collectstatic command to collectstatic files:
$ python manage.py collectstatic
Copy the code
You can see that we have an additional static_dist file
14/21 installation uWsgi
Log out of the virtual environment and install usWGI via PIp3:
#Exiting a Virtual Environment
$ deactivate
#The installation
$ pip3 install uwsgi
Copy the code
15/21 to install Nginx
Install and start Nginx with the following command:
$ sudo yum install epel-release
$ sudo yum install nginx
$ sudo service nginx start
Copy the code
To check whether Nginx is enabled, simply enter your server’s public IP address in the browser:
This statement is a success.
16/21 configure Nginx
Next we configure Nginx. Go to /etc/nginx/conf.d/, create a peekpa.conf file and paste the following contents into it:
server { listen 80; Server_name peekpa.com, 39.104.203.202; charset utf-8; location /static {alias/root/PeekpaCom/static_dist; } location / { include /etc/nginx/uwsgi_params; uwsgi_pass unix:/root/peekpa.sock; }}Copy the code
/etc/nginx/ nginx.conf; To the user root; . It’s not very safe, but we can do it now.
Then run the command to restart the Nginx service.
#Restart the Nginx service
$ sudo service nginx restart
Copy the code
17/21 configuration uWsgi
In the PeekpaCom directory, create a peekpa.ini file:
$ touch peekpa.ini
Copy the code
Next we paste the following contents into the file:
[uwsgi]
chdir = /root/PeekpaCom
module = Peekpa.wsgi
home = /root/.virtualenvs/peekpa
master = true
processes = 10
socket = /root/peekpa.sock
chmod-socket = 777
Copy the code
Sock file will have to be set to 777.
Let’s go back to the PeekpaCom directory and start uWSGi with the command:
$ uwsgi --ini peekpa.ini
Copy the code
At this time, we went to the home page, found also accessible:
But the image still does not load, this is because of our static resources folder permissions. So we need to change the permissions of the static resources folder:
$ chmod 777 -R static_dist/
Copy the code
At this point, we see the static resource file displayed.
18/21 installation supervisor
We need to install the Supervisor in our system directory:
$ sudo pip3 install supervisor
Copy the code
19/21 configuration supervisor
Next, we create a peekpa_supervisor.conf file underneath the project to fill in the Supervisor configuration.
$ sudo touch peekpa_supervisor.conf
Copy the code
Then copy the following into the peekpa_supervisor.conf file:
[program:PeekpaComSite]
command = uwsgi --ini peekpa.ini
directory = /root/PeekpaCom
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
stdout_logfile=/root/PeekpaCom/log/supervisord.log
stderr_logfile=/root/PeekpaCom/log/supervisord.err [supervisord] loglevel=info [inet_http_server] port = :8000 username = admin password = 123 [supervisorctl] serverURL = http://127.0.0.1:8000 username = admin password = 123 [rpcInterface: Supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterfaceCopy the code
Next, we need to create a log folder under the PeekpaCom directory and turn on the permissions for the log folder.
$ mkdir log
$ chmod 777 -R log/
Copy the code
20/21 Start the project
The final step is to start the project:
#Start the project
$ supervisord -c peekpa_supervisor.conf
#Check the status
$ supervisorctl -c peekpa_supervisor.conf
Copy the code
As you can see, the page is all working properly:
21/21 Enable port 8000
When we filled in supervisor just now, we configured port 127.0.0.1:8000. At this time, we need to open port 8000 of the server to access it.
Go to alicloud console, more -> Network and Security Group -> Security Group Configuration
After coming in, select the profile configuration rule
Then click Add Manually
Then write destination 8000/8000, source 0.0.0.0/0, and click Save.
This completes the addition.
At this point, let’s go back to the page and refresh:
Fill in admin and 123 that you just configured in supervisor, and you will see the page management page:
This is the Supervisor daemon page.
At this point, PeekpaCom’s website configuration is complete. Congratulations, you have graduated.
Now that the course is over, I’m sure you can successfully build your own website.
In the next few days we will walk through how Django connects to MongoDB, how to use Redius as a cache, how to use Celery for timed tasks, etc. Senior SAO operation, looking forward to doing things together.
The only way to get the code: follow “PI Ye Lu code” and reply “code” to get it.
Long press the two-dimensional code below to pay attention to, if the article is inspiring to you, welcome to look at and forward.