CLO-ubuntu20.04+python3+mysql+Django+uwsgi+nginx

1 Item Description

Secondary structure:
uwsgi (web server) + Django (web application)
 
Tertiary structure (this article):
nginx+uwsgi (web server) + web application (web application)

root@hecs-356455:~# pwd #超级用户管理目录
/root
root@hecs-356455:~# ls #此目录下有一个Django项目,和一个Django项目静态资源路径
mytest  mytest_static
root@hecs-356455:~# tree #显示该目录下结构
├── mytest #Django项目文件夹
│   ├── db.sqlite3
│   ├── manage.py #Django项目主函数,python3执行一些操作都是对此文件执行
│   ├── mytest #Django项目子文件夹
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── settings.cpython-38.pyc
│   │   │   ├── urls.cpython-38.pyc
│   │   │   └── wsgi.cpython-38.pyc
│   │   ├── settings.py #Django项目配置设置文件
│   │   ├── urls.py #Django项目一级url文件
│   │   └── wsgi.py #Django项目wsgi交互文件
│   ├── news #Django项目应用程序app文件夹
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-38.pyc
│   │   │       └── __init__.cpython-38.pyc
│   │   ├── models.py #Django项目创建数据库表文件
│   │   ├── __pycache__
│   │   │   ├── admin.cpython-38.pyc
│   │   │   ├── apps.cpython-38.pyc
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── models.cpython-38.pyc
│   │   │   ├── tests.cpython-38.pyc
│   │   │   ├── urls.cpython-38.pyc
│   │   │   └── views.cpython-38.pyc
│   │   ├── tests.py #Django项目和数据库的交互文件
│   │   ├── urls.py #Django项目二级url文件
│   │   └── views.py #Django项目视图文件
│   ├── run.log #uwsgi日志文件
│   ├── uwsgi.ini #uwsgi配置文件
│   └── uwsgi.pid #uwsgi pid,当重启uwsgi进程有时需要删掉此文件
└── mytest_static #Django项目静态资源文件夹

2 ubuntu 20.04

root@hecs-356455:~# cat /etc/issue
Ubuntu 20.04.5 LTS \n \l

root@hecs-356455:~# 

The cloud server reserves tcp8001 to provide http services externally.

3 python 3.8.10

root@hecs-356455:~# python3 --version
Python 3.8.10
root@hecs-356455:~# python3
Python 3.8.10 (default, Nov 14 2022, 12:59:47) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
root@hecs-356455:~# 

4 Django

4.1 Installation

root@hecs-356455:~# sudo pip3 install django
root@hecs-356455:~# django-admin --version
4.1.4
root@hecs-356455:~# 

4.2 Create project mytest

root@hecs-356455:~# django-admin startproject mytest
root@hecs-356455:~# cd /root/mytest/
root@hecs-356455:~/mytest# pwd
/root/mytest
root@hecs-356455:~/mytest#
root@hecs-356455:~/mytest# tree
.
├── manage.py
└── mytest
    ├── asgi.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 6 files
root@hecs-356455:~/mytest# 

4.3 Start the project mytest

root@hecs-356455:~/mytest# pwd
/root/mytest
root@hecs-356455:~/mytest# ls
manage.py  mytest
root@hecs-356455:~/mytest# python3 manage.py runserver 0.0.0.0:8001

4.4 Configuration project mytest

 4.4.1 Modify the visited IP/URL

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim settings.py 
ALLOWED_HOSTS = ["*"] #配置/root/mytest/mytest/settings.py文件的这条,双引号中间*表示通配

4.4.2 Create APP application

root@hecs-356455:~/mytest# pwd #在项目mytest的目录下
/root/mytest
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest
root@hecs-356455:~/mytest# python3 manage.py startapp news #在项目mytest的目录下执行此命令表示在项目mytest下创建app应用news
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news
root@hecs-356455:~/mytest# cd mytest/
root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim settings.py 
#在/root/mytest/mytest/settings.py文件中INSTALLED_APPS增加一条APP规则'news',
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'news',
]

4.4.3 Configure the view file views.py of the APP application

root@hecs-356455:~/mytest/news# pwd
/root/mytest/news
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  views.py
root@hecs-356455:~/mytest/news# vim views.py 

The path is /root/mytest/news/views.py

/root is the super management account file path

/mytest is the Django project created

/news is the app application under the created Django project mytest

/views.py is the view file under app application news

root@hecs-356455:~/mytest/news# cat views.py 
from django.shortcuts import render
from django.http import HttpResponse #引入函数

# Create your views here.
def index(request): #定义函数
    return HttpResponse('hello world!') #返回请求值
root@hecs-356455:~/mytest/news# 

4.4.4 Configuring URL Mapping

When an external browser accesses the Django project, it first visits the /root/mytest/mytest/urls.py primary mapping, and then visits the /root/mytest/news/urls.py secondary mapping for url addressing.

[First configure the /root/mytest/news/urls.py secondary mapping file]

root@hecs-356455:~/mytest/news# pwd
/root/mytest/news
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  views.py
root@hecs-356455:~/mytest/news# touch urls.py #默认app下是没有urls.py的,需要创建一个
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  urls.py  views.py
root@hecs-356455:~/mytest/news# vim urls.py 
root@hecs-356455:~/mytest/news# cat urls.py 
from django.urls import path
from . import views

urlpatterns = [
        path('',views.index,name='index'),
        ]
root@hecs-356455:~/mytest/news# 

[Secondly configure the /root/mytest/mytest/urls.py first-level mapping file]

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim urls.py 
root@hecs-356455:~/mytest/mytest# cat urls.py 
"""mytest URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('news.urls')),
]
root@hecs-356455:~/mytest/mytest# 

 

5 whiskeys

5.1 Install uwsgi

Be sure to use pip3 to install, not apt-get install, otherwise there will be many dependency errors.

root@hecs-356455:~# pip3 install uwsgi
root@hecs-356455:~# sudo apt-get install python3-dev
root@hecs-356455:~# sudo apt-get install libpcre3 libpcre3-dev
root@hecs-356455:~# pip3 install uwsgi --no-cache-dir

5.2 Configure uwsgi

Under the folder with the same name as the Django project, create run.log (logging) and uwsgi.ini (uwsgi and django interaction files) and configure them.

root@hecs-356455:~# cd /root/mytest/
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news
root@hecs-356455:~/mytest# touch run.log
root@hecs-356455:~/mytest# touch uwsgi.ini
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news  run.log  uwsgi.ini
root@hecs-356455:~/mytest# cat uwsgi.ini 
[uwsgi]
socket=0.0.0.0:8002
chdir=/root/mytest
wsgi-file=mytest/wsgi.py
processes=4
threads=100
master=true
pidfile=uwsgi.pid
daemonize=/root/mytest/run.log
buffer-size=65535
root@hecs-356455:~/mytest# 
root@hecs-356455:/# cd /root/mytest/
root@hecs-356455:~/mytest# pwd #必须在此路径下才能执行uwsgi的命令
/root/mytest
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news  run.log  uwsgi.ini
root@hecs-356455:~/mytest# uwsgi --ini uwsgi.ini #启动
[uWSGI] getting INI configuration from uwsgi.ini
root@hecs-356455:~/mytest# netstat -nplt #看到uwsgi占用tcp8002的端口号
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      98571/uwsgi 
root@hecs-356455:~/mytest# uwsgi --stop uwsgi.pid #停止
root@hecs-356455:~/mytest# 

6 nginx

When using the following server, browser access returns WSGIServer/0.2 CPython/3.8.10.

root@hecs-356455:~/mytest# pwd
/root/mytest
root@hecs-356455:~/mytest# python3 manage.py runserver 0.0.0.0:8001

The following configuration uses nginx server.

6.1 install nginx

root@hecs-356455:~# sudo apt-get install nginx  

 6.2 Configure nginx

root@hecs-356455:/etc/nginx/sites-enabled# pwd
/etc/nginx/sites-enabled
root@hecs-356455:/etc/nginx/sites-enabled# ls
default
root@hecs-356455:/etc/nginx/sites-enabled# vim default 
server {
        listen 8001 default_server; #nginx监听IPv4:tcp8001
        listen [::]:8001 default_server; #nginx监听IPv6:tcp8001

        location / { #动态资源
                        uwsgi_pass 127.0.0.1:8002; #外部访问0.0.0.0:8001先被nginx代理,再转给0.0.0.0:8002 uwsgi处理
                        include /etc/nginx/uwsgi_params;
        }
        location /static { #静态资源
                root /root/mytest_static;
}
}

Create a static resource path

root@hecs-356455:~# pwd
/root
root@hecs-356455:~# ls
mytest  test
root@hecs-356455:~# mkdir mytest_static #创建静态资源路径
root@hecs-356455:~# ls
mytest  mytest_static  test
root@hecs-356455:~# cd mytest_static/
root@hecs-356455:~/mytest_static# pwd #此路径和上图静态资源路径一致
/root/mytest_static
root@hecs-356455:~/mytest_static# ls
root@hecs-356455:~/mytest_static# 

Modify the /etc/nginx/nginx.conf user to root, otherwise there will be no permission to access static resources

root@hecs-356455:/etc/nginx# pwd
/etc/nginx
root@hecs-356455:/etc/nginx# ls
conf.d          koi-utf     modules-available  proxy_params     sites-enabled  win-utf
fastcgi.conf    koi-win     modules-enabled    scgi_params      snippets
fastcgi_params  mime.types  nginx.conf         sites-available  uwsgi_params
root@hecs-356455:/etc/nginx# vim nginx.conf 
root@hecs-356455:/etc/nginx# cat nginx.conf 
user root;

Add static resource path to settings file in Django project

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim settings.py 
root@hecs-356455:~/mytest/mytest# cat settings.py 
STATIC_ROOT='/root/mytest_static'

Perform static resource synchronization

root@hecs-356455:~/mytest# pwd
/root/mytest
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news  run.log  uwsgi.ini  uwsgi.pid
root@hecs-356455:~/mytest# python3 manage.py collectstatic #静态资源同步
130 static files copied to '/root/mytest_static'.
root@hecs-356455:~/mytest# 

6.3 Start/stop/restart nginx service

root@hecs-356455:/# cd /
root@hecs-356455:/# pwd
/
root@hecs-356455:/# ls
bin  boot  CloudResetPwdUpdateAgent  CloudrResetPwdAgent  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  swapfile  sys  tmp  usr  var
root@hecs-356455:/# 
root@hecs-356455:/# ./etc/init.d/nginx start
Starting nginx (via systemctl): nginx.service.
root@hecs-356455:/# ./etc/init.d/nginx stop
Stopping nginx (via systemctl): nginx.service
root@hecs-356455:/# ./etc/init.d/nginx restart
Restarting nginx (via systemctl): nginx.service.
root@hecs-356455:/#   

Access the file, the browser returns Server: nginx/1.18.0 (Ubuntu)

root@hecs-356455:/# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      103048/nginx: maste 
tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      103061/uwsgi   

 7 mysql

CLO-apache2+phpmyadmin manages mysql_rfc2544's blog - CSDN Blog

Mysql configured user name root, password password

root@hecs-356455:/# mysql --version
mysql  Ver 8.0.31-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

7.1 install mysql

root@hecs-356455:/# sudo apt-get install mysql-server
root@hecs-356455:/# sudo apt-get install mysql-client
root@hecs-356455:/# sudo apt-get install libmysqlclient-dev    

7.2 Install the pymysql module

python3 link mysql use pymysql
python2 link mysql use MySQLdb

root@hecs-356455:/# pip3 install pymysql

7.3 Configure Django to use mysql database

Django's default database is sqlite3, so you need to modify the following to link to the mysql database

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim settings.py 
root@hecs-356455:~/mytest/mytest# cat settings.py 
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.mysql',
        'NAME':'django_mysql',
        'USER':'root',
        'PASSWORD':"password",
        'HOST':'127.0.0.1',
        'PORT':'3306',
    }
}  

7.4 Configure the __init__.py of the Django project and add two lines of configuration

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim __init__.py 
root@hecs-356455:~/mytest/mytest# cat __init__.py 
import pymysql
pymysql.install_as_MySQLdb()
root@hecs-356455:~/mytest/mytest# 

7.5 Configure mysql to add a new database django_mysql

root@hecs-356455:~/mytest/mytest# mysql -u root -p #登录mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 230
Server version: 8.0.31-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; #查看mysql所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database django_mysql; #新增mysql一个数据库,名称django_mysql
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| django_mysql       |
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

7.6 Configure model.py in Django to generate database tables

Each model model in Django corresponds to a table in the database, and the fields in each model correspond to the columns in the database table. So we configure the model in Django to operate the database.

root@hecs-356455:~/mytest/news# pwd #注意是修改应用程序app下的model
/root/mytest/news
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  urls.py  views.py
root@hecs-356455:~/mytest/news# vim models.py 
root@hecs-356455:~/mytest/news# cat models.py 
from django.db import models

# Create your models here.
class Student(models.Model):
    studentNum = models.CharField('学号',primary_key=True,max_length=15)

    class Meta:
        db_table = 'student'
root@hecs-356455:~/mytest/news# 

Generate the migration file and apply it. As long as the model file model.py is modified later, the following two commands need to be executed

python3 manage.py makemigrations

python3 manage.py migrate

root@hecs-356455:~/mytest# pwd
/root/mytest
root@hecs-356455:~/mytest# ls
db.sqlite3  manage.py  mytest  news  run.log  uwsgi.ini  uwsgi.pid
root@hecs-356455:~/mytest# python3 manage.py makemigrations #将models文件生成一个迁移文件
Migrations for 'news':
  news/migrations/0001_initial.py
    - Create model Student
root@hecs-356455:~/mytest# python3 manage.py migrate #将迁移文件的内容作用到数据库中,生成表或者修改字段属性
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, news, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying news.0001_initial... OK
  Applying sessions.0001_initial... OK
root@hecs-356455:~/mytest# 

 7.7 Configure tests.py in Django to modify and query the contents of the mysql database

7.7.1 Configure tests.py

root@hecs-356455:~/mytest/news# pwd #注意是应用程序app下的tests.py
/root/mytest/news
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  urls.py  views.py
root@hecs-356455:~/mytest/news# vim tests.py 
root@hecs-356455:~/mytest/news# cat tests.py 
from django.test import TestCase

# Create your tests here.
from django.http import HttpResponse
from news.models import Student

def testdb(request): #定义添加函数
    test1 = Student(studentNum='111')
    test1.save()
    return HttpResponse("<p>数据添加成功!</p>")

def querry(request): #定义查询函数
    response = ""
    response1 = ""

    list = Student.objects.all()
    for var in list:
        response1 +=var.studentNum+ ""
    response = response1
    return HttpResponse("<p>"+"查询数据库内容返回:"+response + "</p>")
root@hecs-356455:~/mytest/news# 

7.7.2 Configuring URL mapping

[Configure the secondary mapping under the app folder]

root@hecs-356455:~/mytest/news# pwd
/root/mytest/news
root@hecs-356455:~/mytest/news# ls
admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  urls.py  views.py
root@hecs-356455:~/mytest/news# vim urls.py 
root@hecs-356455:~/mytest/news# cat urls.py 
from django.urls import path
from . import views
from news import tests

urlpatterns = [
        path('',views.index,name='index'),
        path('123',tests.testdb), #映射到添加函数
        path('456',tests.querry)  #映射到查询函数
        ]
root@hecs-356455:~/mytest/news# 

[Configure the first-level mapping under the project folder]

Since the two mysql links use the second-level mapping, the first-level mapping does not need to be configured.

root@hecs-356455:~/mytest/mytest# pwd
/root/mytest/mytest
root@hecs-356455:~/mytest/mytest# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py
root@hecs-356455:~/mytest/mytest# vim urls.py 
root@hecs-356455:~/mytest/mytest# cat urls.py #由于两个mysql的链接用的二级映射,所以一级映射无需配置即可
"""mytest URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('news.urls')),
]
root@hecs-356455:~/mytest/mytest# 

8 Restart the service and access the web page

8.1 Killing a process

root@hecs-356455:/# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      104945/nginx: maste #nginx进程提供tcp8001服务
tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      104957/uwsgi #uwsgi提供tcp8002服务        
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      45159/systemd-resol 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      50278/sshd: /usr/sb 
tcp6       0      0 :::8001                 :::*                    LISTEN      104945/nginx: maste 
tcp6       0      0 :::8003                 :::*                    LISTEN      78976/apache2       
tcp6       0      0 :::22                   :::*                    LISTEN      50278/sshd: /usr/sb 
root@hecs-356455:/# 
root@hecs-356455:/# kill -9 104945 #杀掉nginx进程
root@hecs-356455:/# kill -9 104957 #杀掉uwsgi进程
root@hecs-356455:/# 
root@hecs-356455:/# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      45159/systemd-resol 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      50278/sshd: /usr/sb 
tcp6       0      0 :::8003                 :::*                    LISTEN      78976/apache2       
tcp6       0      0 :::22                   :::*                    LISTEN      50278/sshd: /usr/sb 
root@hecs-356455:/# 

8.2 Restart process

root@hecs-356455:/# cd /
root@hecs-356455:/# ./etc/init.d/nginx restart #重启nginx进程
Restarting nginx (via systemctl): nginx.service
cd /root/mytest/
.
root@hecs-356455:/# 
root@hecs-356455:/# cd /root/mytest/
root@hecs-356455:~/mytest# uwsgi --ini uwsgi.ini #启动uwsgi进程
[uWSGI] getting INI configuration from uwsgi.ini
root@hecs-356455:~/mytest# 
root@hecs-356455:~/mytest# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      105385/nginx: maste 
tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      105397/uwsgi        
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      16661/mysqld        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      45159/systemd-resol 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      50278/sshd: /usr/sb 
tcp6       0      0 :::8001                 :::*                    LISTEN      105385/nginx: maste 
tcp6       0      0 :::8003                 :::*                    LISTEN      78976/apache2       
tcp6       0      0 :::22                   :::*                    LISTEN      50278/sshd: /usr/sb 
root@hecs-356455:~/mytest# 

8.3 Accessing web pages

【http://114.115.155.109:8001/】

【http://114.115.155.109:8001/123】

【http://114.115.155.109:8001/456】

Guess you like

Origin blog.csdn.net/rfc2544/article/details/128519658