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

1 项目说明

二级结构:
uwsgi(web server)+ Django(web application)
 
三级结构(本文):
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:~# 

云服务器预留tcp8001对外提供http服务。

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 安装

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

4.2 创建项目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 启动项目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 配置项目mytest

 4.4.1 修改被访问的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 创建APP应用

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 配置APP应用的视图文件views.py

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 

路径是/root/mytest/news/views.py

/root是超管账号文件路径

/mytest是创建的Django项目

/news是创建的Django项目mytest下的app应用

/views.py是app应用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 配置URL映射

外部浏览器访问Django项目时,首先访问/root/mytest/mytest/urls.py一级映射,其次访问/root/mytest/news/urls.py二级映射进行url寻址。

【首先配置/root/mytest/news/urls.py二级映射文件】

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# 

【其次配置/root/mytest/mytest/urls.py一级映射文件】

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 uwsgi

5.1 安装uwsgi

一定要用pip3安装,不可用apt-get install安装,否则会有很多依赖错误。

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 配置uwsgi

在Django项目同名文件夹下,创建run.log(记录日志)和uwsgi.ini(uwsgi和django交互文件)并配置。

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

当使用以下server,浏览器访问返回的是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

以下配置使用nginx server。

6.1 安装nginx

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

 6.2 配置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;
}
}

创建静态资源路径

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# 

修改/etc/nginx/nginx.conf用户为root,否则会没有权限访问静态资源

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;

在Django项目中settings文件增加静态资源路径

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'

执行静态资源同步

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 启动/停止/重启nginx服务

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:/#   

访问文件,浏览器返回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管理mysql_rfc2544的博客-CSDN博客

mysql已配置的用户名root,密码password

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

7.1 安装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 安装pymysql模块

python3链接mysql用pymysql
python2链接mysql用MySQLdb

root@hecs-356455:/# pip3 install pymysql

7.3 配置Django采用mysql数据库

Django默认数据库是sqlite3,所以需要修改以下以链接mysql数据库

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 配置Django项目的__init__.py,增加两行配置

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 配置mysql新增数据库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 配置Django中的model.py生成数据库表

Django中每个模型model都对应数据库中的一张表,每个模型中的字段都对应数据库表中的列。所以我们配置Django中的model即可操作数据库。

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# 

生成迁移文件并应用,后续只要模型文件model.py有修改,都需要执行以下两条命令

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 配置Django中的tests.py对mysql数据库内容进行修改和查询

7.7.1 配置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 配置URL映射

【app文件夹下配置二级映射】

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# 

【项目文件夹下配置一级映射】

由于两个mysql的链接用的二级映射,所以一级映射无需配置即可。

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 重启服务并访问网页

8.1 杀掉进程

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 重启进程

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 访问网页

【http://114.115.155.109:8001/】

【http://114.115.155.109:8001/123】

【http://114.115.155.109:8001/456】

猜你喜欢

转载自blog.csdn.net/rfc2544/article/details/128519658