安装ssh
sudo apt update
sudo apt install openssh-server -y
sudo systemctl status ssh
修改远程连接配置文件
sudo vim /etc/ssh/sshd_config
设置PasswordAuthentication yes
重启服务sudo systemctl restart ssh
下载项目时,文件被锁
停止更新进程
sudo killall -9 apt apt-get
删除锁文件
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/dpkg/lock-frontend
修复配置
sudo dpkg --configure -a
忘记密码
sudo passwd root
N:源码安装nginx
安装环境
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
sudo apt install build-essential libpcre2-dev zlib1g-dev libssl-dev curl ca-certificates pkg-config -y
下载解压nginx源代码
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar -zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1
配置、编译安装
./configure --prefix=/usr/local/nginx
make -j4
sudo make install
启动验证
sudo /usr/local/nginx/sbin/nginx
ps aux | grep nginx
创建nginx运行所需的目录
# 创建缓存和临时目录
sudo install -d www-data -g www-data /var/cache/nginx/client_temp
sudo install -d www-data -g www-data /var/cache/nginx/proxy_temp
sudo install -d www-data -g www-data /var/cache/nginx/fastcgi_temp
# 给权限
# 修改目录归属为 www-data 用户
sudo chown -R www-data:www-data /var/www/html/
# 赋予正确的目录和文件权限
sudo chmod -R 755 /var/www/html/
# 创建配置和日志目录
sudo install -d /etc/nginx/conf.d /var/log/nginx
nginx的主配置文件
# 备份并编辑 nginx.conf
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig 2>/dev/null || true
sudo vim /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
sendfile on;
keepalive_timeout 65;
server_tokens off;
include /etc/nginx/conf.d/*.conf;
}
配置为系统服务
sudo vim /etc/systemd/system/nginx.service
内容如下
[Unit]
Description=Nginx built from source
Documentation=https://nginx.org/en/docs/
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
TimeoutStopSec=10
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存应用
sudo systemctl daemon-reload
# 检查 Nginx 配置语法是否正确
sudo /usr/local/sbin/nginx -t
sudo systemctl enable nginx
sudo systemctl start nginx
M:安装MariaDB
安装
sudo apt install -y mariadb-server mariadb-client
sudo systemctl enable --now mariadb
systemctl status mariadb --no-pager
mariadb -e "SELECT VERSION()"
设置数据库密码
sudo mariadb
# 进入数据库控制台后执行(请替换实际密码)
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Root@123456');
FLUSH PRIVILEGES;
EXIT;
测试
mariadb -u root -p
Root@123456
创建专用数据库与账户
sudo mariadb
CREATE DATABASE webdb CHARACTER SET utf8mb4 COLLATE
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'Web@123456';
GRANT ALL PRIVILEGES ON webdb.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
测试
mariadb -u webuser -p -D webdb
Webdb@123456
安装PHP-FPM
安装
sudo apt install -y php-fpm php-mysql php-gd php-curl php-mbstring php-xml php-zip
php -v
ls -l /run/php/
systemctl --type=service --state=running | grep -E 'php.*fpm'
修改监听地址
sudo vim /etc/php/8.1/fpm/pool.d/www.conf
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
重启
sudo systemctl restart php8.1-fpm
sudo systemctl status php8.1-fpm --no-pager
sudo ss -lntp | grep :9000 # 确认监听状态
配置Nginx处理PHP
创建站点
sudo vim /etc/nginx/conf.d/default.conf
内容
server {
listen 80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
启动Nginx:sudo systemctl start nginx
测试:
# 创建测试页
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php > /dev/null
DOCKER部署环境
环境准备或卸载旧版本
sudo apt remove -y docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc
# 安装依赖
sudo apt update
sudo apt install -y ca-certificates curl
# 添加官方源与软件源
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# 添加软件源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装核心软件包
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动并设置开机自启
sudo systemctl enable --now docker
sudo systemctl enable --now containerd
配置权限
sudo usermod -aG docker "$USER"
# 使权限立即生效
newgrp docker
配置镜像加速与代理
sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://你的镜像加速地址"],
"proxies": {
"http-proxy": "http://你的代理地址:端口",
"https-proxy": "http://你的代理地址:端口"
}
}
# 重启docker服务
sudo systemctl restart docker
测试
docker run --rm hello-world
结果
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 4f55086f7dd0: Pull complete d5e71e642bf5: Download complete Digest: sha256:c3cbe1cc1aa588a64951ac6286e0df7b27fe2e6324b1001c619bb358770c0178 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
配置VPN
# 创建配置目录
sudo mkdir -p /etc/docker
# 创建编辑配置文件
sudo vim /etc/docker/daemon.json
# 内容如下
{
"proxies": {
"http-proxy": "http://主机IP:VPN端口",
"https-proxy": "http://主机IP:VPN端口",
"no-proxy": "localhost,127.0.0.1,::1,.local"
}
}
# 检查json语法,需要安装jq
jq . /etc/docker/daemon.json
# 下载NC,测试连通性
apt install -y netcat-openbsd
nc -vz 主机ip VPN端口
# 重启服务
sudo systemctl restart docker
# 验证
docker info | grep -i proxy
内容如下:
HTTP Proxy: http://主机IP:VPN端口
HTTPS Proxy: http://主机IP:VPN端口
No Proxy: localhost,127.0.0.1,::1,.local
EnableUserlandProxy: true
UserlandProxyPath: /usr/bin/docker-proxy
root@q:~#
但是daemon.json无法连接,因此,采用systemd的配置文件
# 创建配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d
# 创建编写文件
sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
内容如下:
[Service]
Environment="HTTP_PROXY=http://主机IP:VPN端口"
Environment="HTTPS_PROXY=http://主机IP:VPN端口"
Environment="NO_PROXY=localhost,127.0.0.1,.local"
#重新加载启动
sudo systemctl daemon-reload
sudo systemctl restart docker
验证
# 检查配置是否生效
sudo systemctl show docker --property=Environment
# 测试HTTPS握手
# 使用代理访问 Docker Hub
curl -v -x http://192.168.x.x:7890 https://registry-1.docker.io/v2/