Linux_Ubuntu的相关问题

安装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/
相关推荐
熊猫钓鱼>_>18 小时前
ArkTS 方舟编程语言 · 原创快速入门教程
运维·架构·ts·harmonyos·arkts·鸿蒙·js
β添砖java19 小时前
黑马Linux笔记
linux·运维·笔记
在水一缸19 小时前
当 AI 拥有了“核按钮”:深入解析 MCP 服务器与命令执行护栏
运维·服务器·人工智能·命令执行·智能体·ai安全·mcp
雪的季节19 小时前
【无标题】
linux·服务器·python
爱莉希雅&&&20 小时前
Prometheus高可用(alertmanager+node_exporter+grafana)
运维·服务器·grafana·prometheus
增量星球20 小时前
《持续交付2.0系列六》业务需求协作管理
java·运维·自动化·devops·持续部署·持续集成
ly-2725320 小时前
Ubuntu重启之后挂载盘符变更应该怎么修复
linux·ubuntu
运维大师20 小时前
【K8S 运维实战】03-网络模型实战CNI
运维·网络·kubernetes
Huangjin007_20 小时前
【Linux 系统篇(四)】权限详解(一)
linux·运维·服务器