Ubuntu 使用备查
环境
华为云 HECS
Ubuntu 22.04 server 64bit
常用命令
# 添加用户
sudo useradd -m aaa -g admin
sudo passwd aaa
# 修改用户解释器
sudo vi /etc/passwd
最后一条添加或修改为 aaa:x:1000:1000::/home/aaa:/bin/bash
# 删除用户
sudo userdel -r aaa
# 查找文件
sudo find /usr -iname "python*"
# 软连接
sudo ln -s /usr/bin/python3.9 /usr/bin/python3
# 端口占用情况
lsof -i:8000
netstat -tunlp | grep 端口号
# 查看环境变量
echo $PATH
printenv | more
# 添加环境变量
vim /etc/environment
PATH=$PATH:/usr/local/lib/python3.9/dist-packages
source /etc/environment
docker
# 安装
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动
sudo service docker start
# 测试
sudo docker run hello-world
安装 pyenv
# 通过 https://www.ip138.com/ 查下面两个域名的IP地址
github.com
github.global.SSL.fastly.net
# 在服务器 ping 通后将添加 host
sudo vim /etc/hosts
# GitHub Start
140.82.113.3 github.com
140.82.114.20 gist.github.com
151.101.184.133 assets-cdn.github.com
185.199.111.133 raw.githubusercontent.com
# 151.101.184.133 raw.githubusercontent.com
151.101.184.133 gist.githubusercontent.com
151.101.184.133 cloud.githubusercontent.com
151.101.184.133 camo.githubusercontent.com
151.101.184.133 avatars0.githubusercontent.com
199.232.68.133 avatars0.githubusercontent.com
199.232.28.133 avatars1.githubusercontent.com
151.101.184.133 avatars1.githubusercontent.com
151.101.184.133 avatars2.githubusercontent.com
199.232.28.133 avatars2.githubusercontent.com
151.101.184.133 avatars3.githubusercontent.com
199.232.68.133 avatars3.githubusercontent.com
151.101.184.133 avatars4.githubusercontent.com
199.232.68.133 avatars4.githubusercontent.com
151.101.184.133 avatars5.githubusercontent.com
199.232.68.133 avatars5.githubusercontent.com
151.101.184.133 avatars6.githubusercontent.com
199.232.68.133 avatars6.githubusercontent.com
151.101.184.133 avatars7.githubusercontent.com
199.232.68.133 avatars7.githubusercontent.com
151.101.184.133 avatars8.githubusercontent.com
199.232.68.133 avatars8.githubusercontent.com
# GitHub End
# 重启网络
sudo systemctl restart NetworkManager
# 自动安装, 需要科学. 不容易成功
curl https://pyenv.run | bash
# 手动安装
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
# 添加配置
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
# 查看可安装版本
pyenv install -l
# 安装
pyenv install 3.9.16
# 查看已安装版本
pyenv versions
# 切换
pyenv global 3.9.16
# 创建虚拟环境
pyenv virtualenv 3.9.16 test
# 激活环境
pyenv activate test
pyenv deactivate test
安装 python
sudo apt update
sudo apt upgrade
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.9
sudo apt install python-is-python3
python --version
pip --version
编译安装python源码
下载python源码, 如:
https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz
解压源码包:
tar -zxvf Python-3.9.16.tgz
cd Python-3.9.16
配置安装目录
./configure --prefix=/usr/local
使用 make 工具进行编译:
sudo make install
安装 pip
sudo apt-get install python3.9-distutils
sudo apt install python3-pip
python -m pip install django
安装 redis
sudo apt install redis
1、启动redis服务
redis-server # 启动redis服务
redis-server 配置文件 # 按特定配置文件启动redis
2、查看redis服务
ps -aux | grep redis
3、打开服务:
sudo sever redis start
4、关闭服务
sudo server redis stop
安装 nginx
https://zhuanlan.zhihu.com/p/539284516
# 安装依赖
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
# 解压
tar -zxvf nginx-1.23.3.tar.gz
cd nginx-1.23.3
# 配置
./configure --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module
# 编译安装
sudo make -j32
sudo make install
# 查看 nginx 版本 验证是否安装成功:
/usr/local/nginx/sbin/nginx -v
# 添加软连接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
# 启动 nginx 服务:
sudo /usr/local/nginx/sbin/nginx
# 打开浏览器,访问服务器ip
出现 Welcome to nginx! 页面, 启动成功
# 查进程
ps -ef|grep nginx
# 停止服务
sudo /usr/local/nginx/sbin/nginx -s stop
# 重启服务
sudo /usr/local/nginx/sbin/nginx -s reload
部署 uwsgi
# 安装
apt-get install python-dev
pip install uwsgi
# 或
sudo apt-get install uwsgi
sudo apt-get install uwsgi-plugin-python3
# 加载配置
uwsgi --ini uwsgi.ini --plugin=python3
# 重启
sudo service uwsgi restart
# 全杀
sudo pkill -f uwsgi -9
sudo service uwsgi start
部署 django
pip install -U setuptools
pip install django
# 功能测试启动
nohup python manage.py runserver 0.0.0.0:8001 > logs/8001.log 2>&1 &
报错
pip 报 ERROR: Can not execute setup.py 错
执行
pip install -U setuptools
mysql
安装
sudo apt install mysql-server
启动
sudo systemctl start mysql
自动启动
sudo systemctl enable mysql
查版本
mysql --version
修改配置文件, 如端口号
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
允许外部访问
注释 #bind-address = 127.0.0.1
重启
sudo systemctl restart mysql