Linux 
命令主要针对 Linux CentOS 7 版本
 
认识 Linux 目录结构: 
Linux 中一切皆文件,它只有一个根目录 /  所有的文件都挂载在这个节点下 
 
目录 
作用 
 
 
bin 
是 Binary 的缩写存放着最经常使用的命令 
 
boot 
存放 Linux 启动时使用的一些核心文件,包括连接文件和镜像文件 
 
dev 
Linux 的外部设备,在 Linux 中访问设备的方式和访问文件的方式 
 
etc  
存放所有系统管理所需的配置文件和子目录 
 
home  
用户的主目录,在 Linux 中,每个用户都有一个自己的目录 
 
lib 
系统最基本的动态连接共享库(类似 Windows 的 DLL) 
 
lost+found 
存放系统突然关机时的一些文件 
 
media 
自动识别一些设备如U盘、光驱等,自动挂载到该目录下 
 
mnt 
让用户临时挂载文件系统(如挂载光驱) 
 
opt  
安装额外软件摆放目录 
 
proc 
系统内存映射虚拟目录,可通过它获取系统信息 
 
root  
系统管理员主目录 
 
sbin 
系统管理员使用的系统管理程序 
 
srv 
存放一些服务启动之后需要提取的数据 
 
sys 
安装了 Linux 2.6 内核新出现的一个文件系统 sysfs 
 
tmp  
存放临时文件目录(安装包等) 
 
usr  
系统默认软件放置目录 
 
run 
临时文件系统存储系统启动以来的信息,重启时会被清空 
 
var 
存放不断扩充的东西(可存放经常修改的目录文件如日志等) 
 
常用命令操作 命令格式: 
命令格式:命令 [-选项] [参数] 
多个选项可以写在一起共用一个 ‘-‘  
Linux中选项前加单杠(-)是为简写,双杠(–)为全写,效果一致 
 
简单命令: 
关机(shutdown)重启(reboot)命令: 
 
1 2 3 4 5 6 7 8 sync  shutdown  shutdown -h 10  shutdown -h 20:25  reboot  shutdown -r +10  
 
ls:直接使用时表示列出当前目录下的所有非隐藏文件 
 
 
pwd:显示目前的目录 
clear:清空命令行 
cd 目录名:切换到指定目录 
cat /etc/redhat-release:系统版本 
修改时区为上海: sudo timedatectl set-timezone Asia/Shanghai 
 
目录命令: 
1 2 3 4 5 mkdir  目录名        mkdir  -p 多级目录   rmdir  目录名        rmdir  -p 多级目录   
 
1 2 3 4 5 6 7 8 9 10 cp  [选项] 文件或目录 目标目录 cp  -r 原目录 目标目录        cp  -p 原目录 目标目录        
 
 
文件命令: 
1 2 rm  -f 文件或目录 rm  -rf  文件或目录 
 
1 2 3 4 5 6 7 8 9 cat  文件名 cat  -n 文件名 tac  文件名 less 文件名  head  -n 行数 文件名 tail  -n 行数 文件名 
 
touch 文件名(空格隔开创建多个):在当前目录下创建一个空文件,也可改为目录名在指定目录下创建文件 
 
文件属性: 文件属性含义: 
执行 ls -ll 后每行最前面的 10 位就代表了文件的属性: 
 
1 2 3 4 lrwxrwxrwx.  1  root root     7 Mar  7  2019 bin -> usr/bin dr-xr-xr-x.  5  root root  4096 Jul 28  2022 boot 
 
lrwxrwxrwx :分别以 0-9 为坐标,0 为表示 文件类型  ,剩下字符每3个为一组 1-3 表示主权限(root),4-6 表示组权限,7-9 表示其他用户权限。
文件类型: d  目录、 -  文件、 l  连接文档、 b  装置文件中可供存储的接口设备、 c  装置文件中串行端口设备(鼠标、键盘)
权限: r  可读、 w  可写、 x  可执行
修改文件属性: 
chgrp 修改文件属组: 
 
1 2 chgrp  属组名 文件或目录chgrp  -R 属组名 文件或目录 
 
chown 修改文件属主: 
 
1 2 chown  属组名 文件或目录 chown  [-R] 属主名:属组名 文件名 
 
chmod 修改文件权限: 
 
1 2 3 4 chmod  777 www chmod  -R 755 www 
 
链接: 
硬链接:以文件副本的形式存在,不占用实际空间,不允许给目录创建硬链接,同一个文件系统中才能创建 
软链接:链接文件路径,能链接目录文件,也可以跨越文件系统进行链接。当原始文件被删除后,链接文件也将失效 
 
1 2 3 4 5 6 7 8 9 10 11 ln  [选项] 文件 链接名 touch  f1.txt echo  "hello"  >> f1.txt ln  f1.txt fy ln  -s f1.txt fr 
 
磁盘管理: 
 
 
1 2 mount /dev/hdc6 /mnt/hdc6  umount /dev/hdc6  
 
用户管理 账号管理: 
1 2 3 4 5 useradd 选项 用户名 useradd –d /home/sam -m sam  
 
 
 
1 2 3 4 5 passwd  passwd 用户名  passwd -d 用户名  
 
用户组管理: 
 用户组的增加、删除和修改实际上就是对/etc/group文件的更新
 
1 2 3 4 5 6 7 8 9 10 groupadd group1  groupadd -g 101 group2  groupdel group1  groupmod -g 102 group2  groupmod –g 100 -n group3 group2  newgrp root  
 
用户文件 
/etc/passwd 文件: 
 
每个用户都在/etc/passwd文件中有一个对应的记录行,它记录了这个用户的一些基本属性
1 2 3 4 5 $ cat  /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin 
 
系统管理 进程管理: 
| : 管道符, grep : 指定文本内容进行过滤,并返回符合条件的行 
 
ps 查看正在服务器上执行的进程相关信息的列表 
 
1 2 3 4 5 ps -aux ps -aux|grep nginx  
 
pstree 以树状图的方式展现进程之间的派生关系 
 
 
kill 终止指定进程 
 
 
nohup 使命令在后台运行而不受当前终端断开的影响 
 
1 2 3 4 5 nohup  java -jar file.jar > output.log &ps aux | grep file.jar  
 
端口管理: 
CentOS  端口管理依赖 firewalld  防火墙 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 systemctl start firewalld  firewall-cmd --list-ports  firewall-cmd --zone=public --add-port=8080/tcp --permanent  systemctl restart firewalld.service  firewall-cmd --reload  netstat -ntlp  netstat -ntulp |grep 8080  
 
Ubuntu  一般默认有 iptables  
 
1 2 3 4 5 6 7 8 9 sudo apt-get install iptables  iptables -I INPUT -p tcp --dport 8080 -j ACCEPT  iptables-save  sudo apt-get install iptables-persistent  sudo netfilter-persistent save  sudo netfilter-persistent reload  
 
软件安装: 安装工具: 
功能 
CentOS 
Ubuntu 
 
 
更新软件包列表 
yum update 
apt update 
 
安装软件包 
yum install <package> 
apt install <package> 
 
查看已安装软件包列表 
yum list installed 
dpkg -l 或 apt list 
 
卸载软件包 
yum remove <package> 
apt remove <package> 
 
清理不再使用的依赖 
yum autoremove 
apt autoremove 
 
一般Linux 系统基本上分两大类:
 
软件包管理器 dpkg/apt  是针对基于 Debian 的发行版,而  yum/rpm  则是针对基于 RedHat 的发行版。
 
apt 是基于 dpkg 的软件包管理器,dpkg 是一个底层工具,可以处理 .deb  格式的软件包文件。
 
yum 是基于 rpm 包管理器的软件包管理器,用于处理 .rpm  格式的软件包文件。
 
 
安装方法: 
ubuntu 清华软件镜像源 :https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu 
ubuntu 阿里软件镜像源:https://developer.aliyun.com/mirror/ubuntu 
 
1. yum 安装: 1 2 3 4 5 6 7 8 9 10 11 yum check-update  yum update  yum update <package_name>  yum remove <package_name>  yum clean all  yum -y install <package_name>  yum list  yum search <keyword>  
 
yum 与 rpm 类似也能安装本地 rpm 包,它能自动的解决包之间的依赖问题,而 rpm 需要手动解决 rpm 包之间的依赖问题 
yum 换源: 
 
1 2 3 4 5 6 7 8 9 10 mv  /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backupwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum clean all yum makecache yum update 
 
2. rpm 安装: 1 2 3 4 5 rpm -qa|grep jdk  rpm -e --nodeps jdk1.8-1.8.0_361-fcs.x86_64  rpm -ivh jdk-8u361-linux-x64.rpm  
 
因为在 RPM 安装时,程序会自动将相关可执行文件安装到系统默认的路径中,而这些路径已经被添加到系统环境变量 PATH 中,所以可以直接在终端或命令提示符中使用相关命令,无需手动配置环境变量 
 
3. 压缩包安装: jdk 安装配置: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 tar -zxvf jdk-8u291-linux-x64.tar.gz -C /usr/local/java/  vim /etc/profile.d/jdkenv.sh  export  JAVA_HOME=/usr/local/java/jdk1.8.0_291export  CLASSPATH=.:$JAVA_HOME /lib/dt.jar:$JAVA_HOME /lib/tools.jarexport  PATH=$JAVA_HOME /bin:$PATH source  /etc/profile java -version  
 
安装细节: Tomcat 自启动: tomcat 启动前如端口冲突需修改 server.xml 文件中 Connector 与 Server 的 port ,默认重启后不会自启动需要添加自启动服务,同样它也能执行自启动脚本
创建自启动要执行的脚本: 
 
1 2 3 4 #!/bin/bash sudo /home/ruoxijun/apache-tomcat-9.0.73/bin/shutdown.sh sudo /home/ruoxijun/apache-tomcat-9.0.73/bin/startup.sh 
 
在 /etc/systemd/system/ 下创建自启动服务文件如 startup.service 内容如下: 
 
1 2 3 4 5 6 7 8 9 10 11 12 [Unit] Description=Startup Script [Service] Type=forking User=root Group=root ExecStart=/home/ruoxijun/apache-tomcat-9.0.73/bin/startup.sh [Install] WantedBy=multi-user.target 
 
开启自启动服务: 
 
1 2 3 systemctl daemon-reload  systemctl enable  startup.service  systemctl start startup.service  
 
MySQL 安装: 
下载:https://dev.mysql.com/downloads/mysql/  
 
centos 7 版本应选择的 MySQL 安装包为:
1 2 3 4 5 6 7 8 9 cat  /etc/redhat-release uname  -m Select Operating System: Red Hat Enterprise Linux / Oracle Linux  Select OS Version: Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)  (mysql-8.0.32-1.el7.x86_64.rpm-bundle.tar)  
 
删除自带的 mariadb  否则容易导致出错: 
 
1 2 rpm -qa|grep mariadb  rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64  
 
MySQL 压缩包解压后有大量的 rmp 包: 
 
1 2 3 4 5 6 7 8 9 10 11 12 mkdir  mysqltar -xvf mysql-8.0.32-1.el7.x86_64.rpm-bundle.tar -C ./mysql  rpm -ivh mysql-community-common-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-server-8.0.32-1.el7.x86_64.rpm 
 
启动 MySQL 服务: 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 systemctl status mysqld  systemctl start mysqld  systemctl enable  mysqld  cat  /var/log/mysqld.log|grep password mysql_secure_installation mysql -uroot -p  ALTER USER 'root' @'localhost'  IDENTIFIED BY '密码'   create user 'root' @'%'  identified with mysql_native_password by '密码' ; grant all privileges on *.* to 'root' @'%'  with grant option; flush privileges; 
 
Nginx 
Nginx 安装 
确保已安装以下软件 
 
1 yum install -y yum-utils net-tools epel-release 
 
添加 nginx 的软件包到 yum 存储库中 
 
1 2 3 4 5 6 7 8 9 cat  > /etc/yum.repos.d/nginx.repo << EOF [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true EOF 
 
安装 nginx 
 
 
systemctl 启动 nginx 
 
1 2 3 4 5 6 systemctl start nginx  systemctl enable  nginx  systemctl status nginx  systemctl stop nginx  systemctl reload nginx  
 
Nginx 基础 常用命令: 1 2 3 4 5 6 7 nginx  nginx -s stop  nginx -s quit  nginx -s reload  nginx -t  nginx -s reopen  
 
基础配置: 
配置文件位于 /etc/nginx/nginx.conf 它的 http 中有这样一句: 
 
1 include  /etc/nginx/conf.d/*.conf ;
 
它表示会引用 /etc/nginx/conf.d 目录下所有的 .conf 文件,而 /etc/nginx/conf.d/default.conf 提供了默认配置: 
 
1 2 3 4 5 6 7 8 9 10 11 12 server  {    listen        80 ;      server_name   localhost;     location  / {         root    /usr/share/nginx/html;          index   index.html index.htm;      }     error_page    500  502  503  504   /50x.html;     location  = /50x.html {         root    /usr/share/nginx/html;     } } 
 
1 2 3 4 5 6 7 8 9 10 11 12 http  {  server{      listen  80 ;     server_name  localhost;     location  {}     location  {}    }   server{}  } 
 
多个 server 配置,listen + server_name 不能重复 
listen 需要监听的 IP:端口 
server_name 主要用于区分 server ,可以随便起 
当多个 server 监听同一端口,那么访问时会根据 server_name 与域名(主机名)匹配 
 
1 2 3 4 5 6 7 8 9 10 server  {    listen        80 ;     server_name   localhost; } server{     listen  80 ;     server_name  nginx-dev;  } 
 
详细配置 
location 后面的路径会追加到 root 路径后面,访问 location 配置的路径资源将匹配 root + location + 资源路径 
 
http 反向代理 
1 2 3 4 5 6 7 server  {  listen  8001 ;   server_name  ruoyi.localhost;   location  / {     proxy_pass  http://localhost:8088;    } } 
 
proxy_pass + location 与 root + location 类似,前者 http 后者静态代理 
 
1 2 3 4 5 6 7 8 9 location  / {         proxy_set_header  Host $http_host ;          proxy_set_header  X-Real-IP $remote_addr ;     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for ;     proxy_pass  http://localhost:8088; } 
 
修饰符: location 修饰符按优先级排序如下:
= 精确匹配
 
^~ 普通字符前缀匹配
 
~ 正则匹配,区分大小写
 
~* 正则匹配,不区分大小写
 
 
缓冲(buffer) 缓冲一般放在内存中,如果超过指定大小,则会将响应写入磁盘临时文件中
nginx 将响应体读取并缓冲到内存或磁盘中,然后再由 nginx 返回响应给客户端,这样既可以 提前释放服务端的连接   又可以避免请求连接耗时过长导致 连接积压 
proxy_buffering on; nginx 默认启用缓冲,关闭设置为 off 
 
1 2 3 4 5 6 7 location  / {    proxy_buffers  16  4k ;      proxy_buffer_size  2k ;                proxy_pass  http://localhost:8088; } 
 
缓存(cache) 1 2 3 4 5 6 7 8 9 10 11 12 13 http  {         proxy_cache_path  /nginx/cache keys_zone=nginxcache:10m ;     server  {         proxy_cache  nginxcache;          location  / {             proxy_cache_valid  200  302  10m ;             proxy_cache_valid  404       1m ;             proxy_cache_valid  any 5m ;             proxy_pass  http://localhost:8000;         }     } } 
 
proxy_cache_valid 设置不同状态码响应的缓存时间为多少分钟 
 
负载均衡 
轮询(默认使用) 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 upstream  my-server {    server  localhost:8080 ;     server  localhost:8081 ; } server  {  listen  8003 ;   server_name  server;   location  / {     proxy_pass  http://my-server;    } } 
 
最小连接(least-connected) 
 
1 2 3 4 5 upstream  my-server {    least_conn;     server  localhost:8080 ;     server  localhost:8081 ; } 
 
轮循和最小连接的负载平衡,每个客户端的请求都可能分发到不同的服务器
ip_hash 
 
1 2 3 4 5 upstream  my-server {    ip_hash;     server  localhost:8080 ;     server  localhost:8081 ; } 
 
客户端的 IP 地址将用作哈希键,来自同一个ip的请求会被转发到相同的服务器(除非此服务器不可用)
hash 
 
1 2 3 4 5 upstream  my-server {    hash  $request_uri  consistent;     server  localhost:8080 ;     server  localhost:8081 ; } 
 
自定义 hash key,key 可以是字符串、变量或组合
随机(random) 
 
1 2 3 4 5 upstream  my-server {    random  two least_conn;     server  localhost:8080 ;     server  localhost:8081 ; } 
 
每个请求都将传递到随机选择的服务器,two 是可选参数,在考虑服务器权重的情况下随机选择两台服务器,使用指定的方法选择其中一台,默认为选择连接数最少(least_conn)的服务器
权重(weight) 
 
1 2 3 4 upstream  my-server {    server  localhost:8080  weight=2 ;     server  localhost:8081 ; } 
 
每 3 个请求有两个会到 8080
健康检查 
 
1 2 3 4 upstream  my-server {    server  localhost:8080  max_fails=3  fail_timeout=30s ;     server  localhost:8081 ; } 
 
如果 nginx 无法向服务器发送请求或在 30 秒内请求失败次数超过 3 次,则会将服务器标记为不可用 30 秒