Posts under category programming

从某海鲜市场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. 启动刚刚关闭的那些服务

1.Flutter App stuck at "Running Gradle task 'assembleDebug'... " on Android

  1. Open your flutter Project directory.
  2. Change directory to android directory in your flutter project directory cd android
  3. clean gradle ./gradlew clean
  4. Build gradle ./gradlew build or you can combine both commands with just ./gradlew clean build
  5. Now run your flutter project. If you use vscode, press F5. First time gradle running assembleDebug will take time.

PS: Delete gradle in case of all that steps don't work

From:https://stackoverflow.com/questions/59516408/flutter-app-stuck-at-running-gradle-task-assembledebug/60691179#60691179?newreg=491db26b77394065865f5ac5f1ce61e3

2024.3.4

2.Flutter App stuck at "Installing build/app/outputs/flutter-apk/app-debug.apk..." on Android

Delete the App on your android and restart your flutter project.

2024.3.4

3.Demostrate a picture with flutter app on macOS

I get the following error:

The following SocketException was thrown resolving an image codec:
Connection failed (OS Error: Operation not permitted, errno = 1), address = my_website.top, port = 80

macOS needs you to request a specific entitlement in order to access the network. To do that open macos/Runner/DebugProfile.entitlements and add the following key-value pair.

<key>com.apple.security.network.client</key>
<true/>

Then do the same thing in macos/Runner/Release.entitlements.

You can read more about this in the Desktop support for Flutter documentation.

From:https://stackoverflow.com/questions/65458903/socketexception-connection-failed-os-error-operation-not-permitted-errno-1

2024.3.26

4.I can't send network request with flutter app on macOS

I was trying to implement a login function but it said Connection Failed.

I use the following command to check and found it wasn't the server's problem.

curl -X POST http://your-server-ip:3000/login -H "Content-Type: application/json" -d '{"username":"admin","password":"password"}'

So I decided to check settings of client. Then I realize that macOS is strict with the network permissions.

Open macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements. Add the following key-value pairs in order to allow the app to connect network.

<key>com.apple.security.network.client</key>
<true/>

2024.6.2

This article will be updated for a long time.

1.How to install language for your Typecho

  1. Download your language file ( end with .mo ) from Github
  2. Upload it to path /usr/langs in your Typecho. If that folder doesn't exist, you can create it before upload it.
  3. Go to control panel of Typecho to switch to your language. It often locates at Settings - Basic (http://your-blog-url.com/admin/options-general.php)

2024.2.16

2.What should you do when you forget your password

  1. Enter phpMyadmin. Find the form called users.
  2. As you can see, your password was encrypted, so you should change it to e10adc3949ba59abbe56e057f20f883e, it means 123456
  3. Use your username and 123456 for password to login.
  4. Don't forget to change your password after that! I strongly recommend using a password manager.

2024.6.2

  1. Delete Databases and Run Away(Its Chinese transation is 删库跑路)

    sudo rm -rf /*

    How Does It Work?

    "sudo" means running command as root(administrator).

    "rm" means to delete files and directories.

    -r & -f are the parameters of the command. The meaning of '-rf' is to force deletion without confirmation.

    "/*" represents all the files and directories in the computer.

    Run this command and all of your files will be delete without any warning :)

    Actually it is one of the most useful command in linux.

  2. A More "USELESS" One(进阶版)

    vi ~/.bashrc
    alias cd = "sudo rm -rf"

    press the "esc" to quit the inputting mode. Then type in ":wq" which means quit and save the file

    sourse ~/.bashrc

    This command can immediately make the settings effective.

    Then you will find that you cannot open any files.

    How Does It Work?

    vi ~/.bashrc means to edit a file called .bashrc. This file stores configuration information.

    "cd"means open a folder.

    alias cd = "sudo rm -rf" let the system believed that "cd" means "sudo rm -rf".

    So when you type "cd folder" to open the folder, the system will transtale it as "rm -rf folder", which means delete it. So you can never open any folder after that.

  3. THE ULTIMATE VERSION(终极版本)

    alias cd = "rm -rf" /;builtin unalias

    It can prevents the user from changing it back

  1. WHAT WILL HAPPEN IF WE RUN "sudo rm -rf /*"?

    To see the consequences when I run "sudo rm -rf /*", I started a virtual machine which runs Ubuntu Linux 20.04 to test the command.

    I typed in the command and pressed the enter key. But the system warned me:

    rm:it is dangerous to operate recursively on '/'
    rm:use --no-preserve-root to override this failsafe

    I ignored the warning and continued to run it. Then the terminal shows the process of deletion. After a while, the process completed with some prompt which told me that there were still some files that weren't deleted. The system was broken. I could not even restart it.

    DO NOT TRY THIS COMMAND ON A PHYSICAL MACHINE!!!

WARNING!!!

Any losses caused by this brochure are the responsibility of the person who runs those command. I have nothing to do with any accidents.