发现每次自己拿到一个新的Linux系统(通常是ubuntu)都有一些程序性的工作,于是写了这个文档。等我有时间写个脚本出来。

I found that every time I got a new Linux system(usually ubuntu), there was always some procedural work, so I wrote this document. I might write a script when I have time.

一、apt换源

可以先备份一下/etc/apt/sources.list,这一步不做也行。

sudo cp /etc/apt/sources.list /etc/apt/sources_copy.list

然后编辑原来的这个文件,

去这个网站找对应的代码,换成阿里云的源即可(https://developer.aliyun.com/mirror/ubuntu

例如我用ARM Ubuntu16.04,用的是这段

# 默认注释了源码仓库,如有需要可自行取消注释
deb https://mirrors.aliyun.com/ubuntu-ports/ xenial main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-security main restricted universe multiverse

二、修改每次登录时的默认目录

vi /etc/passwd

找到用户所在行,把后面的路径改成想要的即可

三、查看当前ip

hostname -I

四、配置静态ip

编辑/etc/dhcpcd.conf文件,加入以下内容,重启即可。

#设置网口的静态 IP 地址
interface eth0
static ip_address=192.168.60.242/24
static routers=192.168.60.1
static domain_name_servers=192.168.60.1

#设置 WiFi 的静态 IP 地址
interface wlan0
static ip_address=192.168.60.242/24
static routers=192.168.60.1
static domain_name_servers=192.168.60.1

routers是路由器,domain_name_servers是DNS服务器,一般二者填一样的。

五、配置使用帮助tldr

ubuntu下安装很简单,执行以下三条命令:

sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install -g tldr

如果出现了警告,有可能是因为nodejs版本问题,用一下命令升级nodejs:

sudo apt-get install nodejs-legacy
sudo npm install -g n
sudo n latest

如果升级nodejs失败,可能是Ubuntu版本问题,用以下命令安装稳定版nodejs:

sudo n stable 
sudo node -v

然后再安装tldr

sudo npm install -g tldr

用一下命令可以对tldr更新

tldr --update

六、查看系统架构:

uname -m

x86:Intel;x64:AMD

提示:

/ /中的内容均为本人瞎扯,无视即可。

官方文档:https://docs.docker.com/

中文文档:https://yeasy.gitbook.io/docker_practice/

零、常用命令

#查看已创建的镜像
sudo docker images
#启动
systemctl start docker
#查看docker运行状态
systemctl status docker
#登录
docker login
#拉取镜像 docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
docker pull python:3.8-slim

一、本地打包Django应用

1.dockerfile

例如我要部署一个Django程序,先生成这个项目的requirements.txt,在项目的根目录执行

#那个点的意思是当前目录
pipreqs . --encoding=utf-8

在项目根目录下新建文件:dockerfile

FROM python:3.8-slim-buster
WORKDIR /app
COPY . .
RUN pip3 install -r requirements.txt

dockerfile中可以定制的方面有很多,使用的时候查看官方文档。

2.国内镜像加速

阿里云镜像获取地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

编辑daemon.json,添加一行:

"registry-mirrors":["阿里云提供的加速地址"];

重启docker服务即可

3.创建和运行

/ 这个过程报错的概率还蛮大的,一步一步根据提示&谷歌改就好了。/

创建:

docker build -f dockerfile路径 . -t 镜像名

创建并运行,此命名将该容器映射到本地80端口:

#docker run -it -d -p 80:8000 --name mysite django_docker_img:v1
docker run -it -d -p 80:8000 --name 容器名 镜像名

mysite是容器名,django_docker_img:v1是镜像名

-t:让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上

-i:让容器的标准输入保持打开

-p:这是将容器中的某一个端口映射到本地主机上,前一个数字是本地主机上的端口,后面的才是容器上的端口。

-d:让容器在后台运行(--detached),这样容器的输出就不会显示在终端了。

可以用docker ps查看运行情况。

进入容器内部:

docker exec -it mysite bash

会发现此刻处于Dockerfile中设置的工作目录,该目录下内容即为项目文件内容。输入exit即可退出。

然后进去用python3 manage.py runserver 0.0.0.0:8000运行Django项目即可,如果有数据库记得迁移。这里如果不用0.0.0.0:8000启动,而是用默认的127.0.0.1:8000,本地主机是访问不了的。

/ 感动!!!!! /

二、在Ubuntu上使用NGINX+uwsgi+Django

0.私有仓库的配置

你可以使用官方 registry 镜像来运行。

docker run -d -p 5000:5000 --restart=always --name registry registry

这将使用官方的 registry 镜像来启动私有仓库。默认情况下,仓库会被创建在容器的 /var/lib/registry 目录下。

创建好私有仓库之后,就可以使用 docker tag 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为 127.0.0.1:5000

先在本机查看已有的镜像。

使用 docker tagubuntu:latest 这个镜像标记为 127.0.0.1:5000/ubuntu:latest

格式为 docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]

使用 docker push 上传标记的镜像。

curl 查看仓库中的镜像。

curl 127.0.0.1:5000/v2/_catalog

这里可以看到 {"repositories":["python"]},表明镜像已经被成功上传了。

1.安装

环境为Ubuntu20.04

安装docker略过,自行查看https://docs.docker.com/engine/install/ubuntu/

安装完后记得修改加速地址:

sudo vim /etc/docker/daemon.json

加入:(阿里云镜像获取地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

{"registry-mirrors": ["阿里云加速地址"]}

保存退出,重启docker:

sudo systemctl reload docker
sudo systemctl restart docker

如果重启docker服务时遇到 docker.service is not active,cannot reload问题需要重启ubuntu系统。

sudo docker info

使用命令之后在最后几行出现以下信息代表配置成功:

Registry Mirrors:
  https://xxxx.mirror.aliyuncs.com/

完成后可以尝试运行一下sudo docker run hello-world看看docker是否安装成功。

2.Python+uwsgi

这里nginx单独一个容器,uwsgi+django为一个容器。

先生成requirements.txt。

先拉取镜像:

docker pull ubuntu:20.04

创建容器并运行:

docker run -it -d --name myubuntu ubuntu:20.04

其中history为镜像名字。

把Django的程序文件拷贝到容器:

docker cp /home/docker/history/ myubuntu:/home/

用如下命令进入容器:

docker exec -it myubuntu bash

给pip和apt换源:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

然后安装requirements和django:

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple

我安装uwsgi的时候报错:Exception: you need a C compiler to build uWSGI

查资料得知需要安装python-dev,于是:

apt-get update
apt-get install python-dev

进度:

创建了Python3.8容器,但是无法安装uwsgi;另外无法用vi编辑文件;

#安装xrdp
sudo apt-get install xrdp

#开启xrdp服务
sudo service xrdp restart

然后用Windows自带的远程连接就可以了,进去的时候要输入用户名和服务器登入的密码。

Then you could use the remote desktop connection in Windows. Username and password is required when logging in.

从某海鲜市场50块钱买了一台小米4,准备刷成Ubuntu作为服务器。这是捯饬小米4和Ubuntu的记录。

/ /的内容都是我瞎说的

零、常用命令

0.信息记录

Ubuntu版本:16.04 TLS

处理器:ARMv7 Processor rev 1 (v7l)

1.关闭/启动GUI

sudo service lightdm stop
sudo service lightdm start

+∞、报错和解决

0.无法ssh连接

应该是什么安全设置禁止使用密码登录,所以只能采用ssh秘钥进行无密码登录。

首先登录到服务器后,在命令行输入命令用来生成秘钥:

 ssh-keygen

其中第一步是确认保存秘钥的位置,一般使用默认的位置即:

/home/timer/.ssh/id_rsa #此处timer为用户名

第二步是为秘钥设置一个密码:如果输入的话以为只即使被人有你的秘钥没有你的密码也是无法登录你的服务器的这样会比较保险但也比较繁琐,直接回车表示不设置密码; 第三步是确认密码,如果没有设置的话可以直接回车。后面的信息是给出秘钥保存的位置 和秘钥信息。 最终我们可以看到在 /home目录下的 time目录中生成了一个隐藏目录 .ssh。里面包含两个密钥文件,id_rsa 为私钥,id_rsa.pub 为公钥。

配置SSH,打开秘钥登录功能:

使用vi编辑 /etc/ssh/sshd_config 文件

sudo vi /etc/ssh/sshd_config

然后按 i进入编辑模式,在空白位置输入:

RSAAuthentication yes
PubkeyAuthentication yes

注意此处需要留意root 用户能否通过 SSH 登录, 如果需要进行如下设置:

PermitRootLogin yes

此处便已经设置好了使用秘钥登录了,但是如果需要禁用密码登录可以进行如下设置:

PasswordAuthentication no

这一步最好是在完成前面的全部设置,然后能够用秘钥登录的前提下设置,不然又不能用密码登录,秘钥又没法登录就尴尬了。 编辑完文本后按 ESC:wq 保存文件并退出。

最后,输入如下指令重启 SSH 服务:

service sshd restart

然后用ssh软件连接时,把私钥文件传上去即可。

1.

E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates Release' does not have a Release file.
......

换成阿里云的源即可(https://developer.aliyun.com/mirror/ubuntu

2.

Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
 bleachbit : Depends: gir1.2-gtk-3.0 but it is not installable
             Depends: gir1.2-notify-0.7 but it is not installable
E: Unmet dependencies. Try using -f.

运行

apt -f -y install

3.

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

首先查看是否有apt-get这个程序在运行ps aux|grep apt-get

如果发现存在这样的程序在运行那么就kill掉,没有就继续往下执行

直接删除锁文件:

sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock

4.

You don‘t have enough free space in /var/cache/apt/archives

网上简单粗暴的解决办法通常是这个:

sudo apt autoclean
sudo apt autoclean
sudo apt autoremove 
docker system prune

但是不管用啊。由于本身硬盘空间只有16G,所以不管用,唯一的办法就是给var/cache/apt/archives创建一个软链接。

先用df -h查看存储使用情况:

root@ubuntu-phablet:/dev# df -h
Filesystem                      Size  Used Avail Use% Mounted on
......
/dev/mmcblk0p25                  13G  2.2G   11G  18% /userdata
/dev/loop0                      2.0G  1.8G   36M  99% /
/dev/loop1                      168M  164M  3.2M  99% /android/system
......

可以看到/目录下只有36M的空间,但是在/userdata下还有11G/ 真够多的。。。。/,于是我们就可以在/userdata目录下创建软链接。命令如下:

sudo mkdir -p "/userdata/partial"
sudo rm -rf /var/cache/apt/archives
sudo ln -s "/userdata" /var/cache/apt/archives

然后再执行sudo apt-get upgrade就不会报错了。

/ 这个煞笔错误困扰了我两个下午,百度和Google快被我翻个底朝天了。。。/

5.

dpkg: error: unable to access dpkg status area: Read-only file system

/ 这个错误至今没有找到解决方案,直接导致用小米4做服务器的计划失败/

*此文档主要内容写于2022下半年,部分命令已经过时

部署环境:Ubuntu20.04;x86_64;Python3.8

零、常用命令

1.查看网站访问日志

tail -f /www/wwwlogs/main.helloworld-yyds.top.log

2.在models.py中 创建/修改 表后同步到数据库

python3.8 manage.py makemigrations
python3.8 manage.py migrate

一、安装宝塔和升级

1. 下载宝塔面板

这个是老命令,我用这个安装的时候一直报下面第一个错。

wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && sudo bash install.sh

建议用新命令安装:

wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh ed8484bec

安装完成之后,账户和密码在宝塔安装成功的时候会显示出来,记得记下来

1)报错
E: Package 'python-pip' has no installation candidate
        Note, selecting 'python-dev-is-python2' instead of 'python-dev'
Package python-pip is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  python3-pip

E: Package 'python-pip' has no installation candidate

换成如下命令安装

wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh ed8484bec
2)报错
Traceback (most recent call last):
File "setup.py",line 19,in <module>
from setuptools import Extension,setup,find_packages
ImportError:No module named setuptools
======================
Pillow installation failed.

执行

wget --no-check-certificate https://www.moerats.com/usr/down/setuptools-0.6c11.tar.gz
tar -xvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install

然后再执行一遍安装命令。

3)报错
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

可以先不搭理它,笔者还没有遇到需要解决这个报错的情况。

2. 第一个命令默认安装的是宝塔5,需要升级宝塔6,第二个命令直接整成宝塔7了。。。。。

curl -sSO http://download.bt.cn/install/update_to_6.sh && bash update_to_6.sh

3. 宝塔用户名和密码

记得先去服务器提供商那里开放端口!!!

这个在宝塔安装成功的时候会显示出来,记得记下来

外网面板地址: http://your_ip:38398/b2babfca
username: ***
password: ***

如果忘记用户名和密码,可以执行这个查看

http://your_ip:8888/1ogin

二、安装环境

1. 安装软件

进入宝塔面板,把弹出来的关掉,去软件商店安装Python项目管理器、NGINX、mysql,注意版本。

2. 去Python项目管理器里面创建项目

看看有没有可选的Python版本,如果没有就安装3.9。安装完再回来创建项目,启动方式选择uwsgi,架构选Django,设置一个端口,注意不要和其他的冲突,下面两个都勾选上。

创建完之后会自动启动,看一下日志有没有报错,有的话就根据报错解决一下就好了。最后去模块里面下载Django和mysql。

到这里如果一切顺利,Django就能运行了。但是你想啥呢,怎么会这么容易解决[Doge]

三、记录一下我踩过的坑

1.同步静态文件的时候报错:

django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

在settings.py中添加

import os
STATIC_ROOT = os.path.join(BASE_DIR,"static")

再执行载入的命令就行了

2.迁移数据库的时候报错

django.db.utils.OperationalError: (1050, "Table 'app01_log' already exists")

执行manage.py makemigrations 未提示错误信息,但manage.py migrate时进行同步数据库时出现问题

进入 apps 下 migrations 目录, 删除除了 init.py之外的文件,再重新执行那两个命令就好了

四、部署完成后的注意事项

1.如果你的项目里有用Django发邮件的

把settings.py发邮件的SMTP端口改成587,因不管是常用的25还是通常备用的465,全特么被sb阿里云封了。。。

血和泪的教训。。。为了解决这个bug把百度翻了个底朝天。。。。哈哈哈。。。。

总结:遇到这种情况首先要保证在本机上调试时代码是正确的,功能能成功,那么以后部署时出错,问题应该就出在设置上。在部署时出错,经过查找发现问题出在send_mail()这条上,那么就围绕它解决。有些公司的服务器对端口有限制,所以就只能查资料,然后25-465-587一个一个试下去。当然,这也要你的mail host支持这些端口,有些就不支持587。

2.将.sqlite3文件转换成.sql

sqlite3 database.sqlite .dump > sqlite_dump.sql

上面这个方法可能会出现问题,推荐使用Navicat等软件生成。

五、把本地代码上传上去之后

settings.py:

debug设置为False

改mysql账号密码,数据库名称(注意大小写)

改static路径

允许访问的主机'*'

六、部署完成后,下一次部署的步骤

  1. 关闭Python任务,NGINX等
  2. 在本地进入 apps 下 migrations 目录, 删除除了 init.py之外的文件,确保执行 python manage.py makemigrations 或者 migrate 不报错
  3. 上传代码
  4. 对项目内容进行修改,在settings.py中更改数据库名称、数据库用户名和密码、allow_host设置为"*",在static配置路径的后面加上一条:

    STATIC_ROOT = os.path.join(BASE_DIR, "static")
  5. 进入wwwroot目录执行

    python3.8 manage.py makemigrations
    python3.8 manage.py migrate
  6. 在manage.py同级目录下,进入虚拟环境

    source /www/wwwroot/www.helloworld-yyds.top/history01_venv/bin/activate

    执行下面的命令是为了让NGINX能找到静态文件

    python3 manage.py collectstatic

    然后输入yes就行了。

  7. 启动刚刚关闭的那些服务