dodo

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

ubuntu-server-20.04 + python3.8.2 + mysql8.0 + uwsgi + nginx服务器配置

一、设置root密码

dodo@dodo:~$ sudo passwd root
[sudo] password for dodo:
New password:
Retype new password:
passwd: password updated successfully

二、修改时区

dodo@dodo:~$ tzselect
......
You can make this change permanent for yourself by appending the line
        TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
dodo@dodo:~$ sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
dodo@dodo:~$ date -R
Fri, 19 Feb 2021 22:05:00 +0800

 修改时间

  sudo date -s MM/DD/YY //修改日期 sudo date -s hh:mm:ss //修改时间

  在修改时间以后,修改硬件CMOS的时间

  sudo hwclock --systohc //非常重要,如果没有这一步的话,后面时间还是不准

  另外一种命令更新时间

  sudo ntpdate us.pool.ntp.org

三、修改IP地址

dodo@dodo:~$ sudo vi /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:
            addresses: 
        - 192.168.146.133/24 gateway4: 192.168.146.1 nameservers: addresses: - 202.101.224.69 - 202.101.226.69 version: 2
dodo@dodo:~$ sudo netplan apply#使配置生效  
dodo@dodo:~$ ip address #查看IP

  1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
  2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  link/ether 00:0c:29:a6:25:5d brd ff:ff:ff:ff:ff:ff
  inet 192.168.146.133/24 brd 192.168.146.255 scope global ens33
  valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fea6:255d/64 scope link
  valid_lft forever preferred_lft forever

四、配置SSH

dodo@dodo:~$ sudo vi /etc/ssh/sshd_config
#最后一行添加
PermitRootLogin yes
dodo@dodo:~$ sudo service ssh restart

再尝试以 root 账户登录即可正常登录

五、更新国内ubuntu镜像源

dodo@dodo:~$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份原文件
root@dodo:/home/dodo# echo ''> /etc/apt/sources.list #清空sources.list文件
root@dodo:/home/dodo# vi /etc/apt/sources.list #添加以下内容
#(中科大镜像源):
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
#清华镜像源:
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
#阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#网易源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
#ubuntu官方源
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu focal main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu focal-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu focal universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal universe
deb http://cn.archive.ubuntu.com/ubuntu focal-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu focal multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal multiverse
deb http://cn.archive.ubuntu.com/ubuntu focal-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://cn.archive.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu focal-security universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security universe
deb http://cn.archive.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security multiverse

dodo@dodo:~$ sudo apt-get update #更新索引

dodo@dodo:~$ sudo apt-get list --upgradable #列出需要更新的软件包

dodo@dodo:~$ sudo apt-get upgrade #更新已安装的软件包

 六、安装软件

dodo@dodo:~$ sudo apt-get install make
dodo@dodo:~$ sudo apt-get install gcc
dodo@dodo:~$ sudo apt-get install python3
dodo@dodo:~$ sudo apt=get install python3-pip
dodo@dodo:~$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django==3.1.5
Successfully installed asgiref-3.3.1 django-3.1.7 pytz-2021.1 sqlparse-0.4.1
dodo@dodo:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__version__
'3.1.7'
>>>exit()
dodo@dodo:~$ sudo apt install mysql-server mysql-common mysql-client

  1.安装好mysql,但是在用命令登陆数据库的时候报错:

dodo@dodo:~$ mysql
ERROR 1045 (28000): Access denied for user 'dodo'@'localhost' (using password: NO)

  2.于是我在mysqld.cnf文件末尾添加skip-grant-tables,保存之后重启mysql 服务并登录,直接按enter键进入了数据库

dodo@dodo:~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
skip-grant-tables #文件末尾 添加
dodo@dodo:~$ sudo service mysql restart
dodo@dodo:~$ mysql #登录,令authentication_string=’’,将skip-grant-tables注释重启mysql服务,然后也可以登陆数据库

  mysql> update user set authentication_string='' where user='root';#不设置密码

  mysql>exit;

  dodo@dodo:~$ sudo service mysql restart #在这之前记得要注释掉skip-grant-tables

  dodo@dodo:~$ mysql -uroot

  mysql> alter user 'root'@'localhost' identfied by 'dodo';#设置密码

  mysql> flush privileges;

  mysql>exit;

        3.然后就可以使用密码登陆了(mysql 的 root用户中有几个 password="", 为了安全起见用 delete 命令删除就行)

   4.新建mysql用户

dodo@dodo:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, 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> create user 'dodo'@'localhost' identified by 'dodo'; #创建用户
Query OK, 0 rows affected (0.14 sec)

mysql> grant all on expressway_new.* to 'dodo'@'localhost'; #权限设置
Query OK, 0 rows affected (0.13 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string='' where user='dodo'; #更改加密方式
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> alter user 'dodo'@'localhost' identified by'dodo';#重新设置密码
Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql>exit;
dodo@dodo:~$ sudo service mysql restart

  dodo@dodo:~$ mysql -udodo -p

  mysql> create database expressway_new;
  Query OK, 1 row affected (0.04 sec)

  mysql> exit;

  5.安装mysql-connector-python

dodo@dodo:~$ sudo pip3 install mysql-connector-python -i https://pypi.tuna.tsinghua.edu.cn/simple
Successfully installed mysql-connector-python-8.0.23 protobuf-3.15.1
dodo@dodo:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector as mc  #验证安装成功否
>>> mc.__version__
'8.0.23'

   6.安装nginx

dodo@dodo:~$ sudo apt-get install nginx #安装nginx
dodo@dodo:~$ sudo service nginx start #启动nginx
dodo@dodo:~$ nginx -v #版本检查
nginx version: nginx/1.18.0 (Ubuntu)

  7.安装uwsgi

dodo@dodo:~$ sudo pip3 install uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.19.1

  测试uwsgi,新建test.py

dodo@dodo:~$ sudo touch test.py
dodo@dodo:~$ sudo vi test.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b'Hello World']
dodo@dodo:~$ uwsgi --http :8000 --wsgi-file test.py

  然后打开浏览器访问http://192.168.146.132:8000/ ,显示如下表示安装成功。  

 

 

部署expressway_new项目

拷贝expressway_new项目到根文件夹下

dodo@dodo:~$ sudo cp -r ~/expressway_new/ /expressway_new
cd /expressway_new/expressway_new
dodo@dodo:/expressway_new/expressway_new$ sudo chmod 777 -R /expressway_new/media/ #设置media全部子目录及文件权限改为777,即每个人都有读和写以及执行的权限 

在/expressway_new/expressway_new下,新建uwsgi.ini配置文件

dodo@dodo:/expressway_new/expressway_new$ sudo touch uwsgi.ini
dodo@dodo:/expressway_new/expressway_new$ sudo vi uwsgi.ini
[uwsgi]

socket = :8800
chdir = /expressway_new
module = expressway_new.wsgi
master = true
processes = 2
threads = 1
thunder-lock = true

buffer-size = 65536
chmod-socket = 666
vacuum = true

nginx配置:

dodo@dodo:/expressway_new/expressway_new$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
dodo@dodo:/expressway_new/expressway_new$ sudo vi /etc/nginx/sites-available/default#写入以下内容
server {
        listen 80;
        server_name 127.0.0.1;
        charset UTF-8;
        # access_log /expressway_new/nginx_logs/expressway_access.log;
        # error_log /expressway_new/nginx_logs/expressway_error.log;

        #max upload size
        client_max_body_size 200M;

        location / {
                include /etc/nginx/uwsgi_params;
                uwsgi_pass 127.0.0.1:8800;
                uwsgi_send_timeout 600;
                uwsgi_connect_timeout 600;
                uwsgi_read_timeout 600;

        }

        location /media {
                alias /expressway_new/media; # your Django project's media files - amend as required
        }

        location /static {
                expires 30d;
                autoindex on;
                add_header Cache-Control private;
                alias /expressway_new/static/;
                }
        }

 


dodo@dodo:/expressway_new/expressway_new$ sudo rm /etc/nginx/sites-enabled/default
dodo@dodo:/expressway_new/expressway_new$ sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
dodo@dodo:/expressway_new/expressway_new$ sudo service nginx restart

设置uwsgi开机启动

dodo@dodo:/expressway_new/expressway_new$ sudo vi /etc/systemd/system/expressway_new.service #写入以下内容
[Unit]
Description=uWSGI to service expressway_new
After=syslog.target
#After=mysql.service

[Service]
User=dodo
Group=www-data
WorkingDirectory=/expressway_new/expressway_new
Environment="PATH=/usr/bin/"
ExecStart=/usr/local/bin/uwsgi --ini uwsgi.ini
Restart=always
[Install]
WantedBy=multi-user.target
dodo@dodo:/expressway_new/expressway_new$ sudo systemctl start expressway_new.service
dodo@dodo:/expressway_new/expressway_new$ sudo systemctl enable expressway_new.service
Created symlink /etc/systemd/system/multi-user.target.wants/expressway_new.service → /etc/systemd/system/expressway_new.service.

 导入数据

dodo@dodo:~$ sudo systemctl stop expressway_new.service
dodo@dodo:~$ mysql -udodo -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, 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;
+--------------------+
| Database           |
+--------------------+
| expressway_new     |
| information_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> use expressway_new;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> source ~/expressway202007211522.sql; #导入数据
mysql> exit;
Bye
dodo@dodo:~$ sudo systemctl start expressway_new.service #开启expressway_new服务
dodo@dodo:~$ sudo reboot

打开浏览器访问http://192.168.146.132 ,正常打开部署成功!

 

其他命令

/var/www目录更改权限:
$sudo chown -R ‘你的用户名’ /var/www(把www改成自己的私有目录)
$sudo chown -R ‘dodo’ /var/www
Mysql数据导出至sql:
$mysqldump -u root -p expressway > expressway20160326.sql

 

posted on 2021-02-20 12:41  dodo‘s  阅读(54)  评论(0)    收藏  举报