Centos常用知识合集 (持续更新)

5 篇文章 0 订阅
3 篇文章 0 订阅

Centos 常用小技巧

  • 查看系统中有哪些用户:cut -d : -f 1 /etc/passwd

  • 删除用户:userdel -rf name

  • 修改权限:chmod -R 777 dir (将dir整体变为777,常用0700)

  • 修改所有者: chown -R www:www /html (将html目录所有者变成www组的www用户)

  • 删除目录:rm -rf dir (整体删除dir)

  • 拷贝目录:cp -ar ./* /dir (将当前路径下所有文件包括子目录全部拷贝到dir)

  • zip解:unzip -x filename

  • zip压:zip -r filename.zip /dir (压缩/dir文件夹为filename)

  • tar压:tar -zcvf filename.tar.gz ./*

  • tar解:tar -xzvf filename.tar.gz

  • xz解为tar: xz -d filename.tar (再用tar解一次:tar -xvf filename.tar)

  • xz压tar: xz -z filename.tar (将tar二次压缩成xz)

  • 查看centos发行版本: cat /etc/centos-release

  • nohup后台运行程序不要任何输出:nohup ./program >/dev/null 2>&1 &

  • 查看dir目录占用了多少硬盘空间:du -h --max-depth=1 /dir

  • 查看整体硬盘空间占用情况:df -h

  • 关闭firewalld:

    systemctl stop firewalld
    systemctl disable firewalld
    systemctl mask firewalld

  • 关闭 selinux
    nano /etc/selinux/config

    SELINUX=disabled
    ​ 存盘退出, 需要重启。

  • 添加路径到系统PATH
    nano ~/.bash_profile

    在PATH=$PATH这行尾部加上:/usr/local/nginx/sbin
    ​存盘退出。

    让设置生效:source ~/.bash_profile

  • crontab自动定时执行,也可以用来设置自启动:

    输入 crontab -e 加一行:

    @reboot sleep 5;/root/autoexec.sh
    存盘退出

  • 最基础的nftables防火墙脚本:
    编辑脚本:nano basic.nft

    flush ruleset
    table inet filter {
            chain input {
                    type filter hook input priority 0; policy drop;
                    ct state established,related accept
                    ct state invalid drop
                    iif lo accept
                    ip protocol icmp icmp type { destination-unreachable, router-solicitation, router-advertisement, time-exceeded, parameter-problem } accept
                    ip protocol igmp accept
                    //放行3个端口:
                    tcp dport { ssh, http, https} accept
            }
            chain forward {
                    type filter hook forward priority 0; policy drop;
            }
            chain output {
                    type filter hook output priority 0; policy accept;
            }
    }
    
    存盘退出
    

    加载防火墙:nft -f basic.nft


MySQL小技巧

  • mysql备份与恢复
    无需登录:
    备份数据库:mysqldump -uroot -p'密码' 库名 > /文件名.sql
    还原数据库:mysql -uroot -p'密码' 库名 < /文件名.sql

  • 删库(需要先登录MySQL):
    DROP DATABASE 库名;

  • 建库:
    CREATE DATABASE 库名 CHARACTER SET utf8 COLLATE utf8_general_ci;


其他小技巧

  • 如何移植一个phpbb论坛?

    首先在新环境上安装好php和nginx,

    将旧的phpbb全部html文件拷贝到/usr/local/nginx/html

    通过上面的数据库还原方式,将老数据恢复好。

    然后修改html目录权限:
    chmod -R 0700 /usr/local/nginx/html
    chown -R www:www/usr/local/nginx/html

    能够登录后台后第一件事情是Purge the cache和Purge all sessions

    如果直接不能通过域名访问littledot.net,需要:

    nano /usr/local/nginx/conf/nginx.conf

    在index.html 前面加上 index.php,注意:https部分也许如此

    有时候浏览器无法访问网站,不是设置的问题,而是没有清空本地浏览器缓存导致,尝试整体清空一次问题即可解决。


  • 如何申请Let’s Encrypt证书

    参考网站1:https://letsencrypt.org/

    参考网站2:https://certbot.eff.org/lets-encrypt/centosrhel8-nginx

    依次输入以下命令:

    dnf -y install snapd (Snap包是新应用格式包, 通过snapd来安装使用snap应用)
    systemctl enable --now snapd.socket
    ln -s /var/lib/snapd/snap /snap
    snap version
    snap changes
    snap change 1
    snap list
    snap install core
    snap refresh core
    dnf -y remove certbot
    snap install --classic certbot
    ln -s /snap/bin/certbot /usr/bin/certbot
    certbot certonly --nginx --nginx-server-root /usr/local/nginx/conf/ --nginx-ctl /usr/local/nginx/sbin/nginx 
    

    –nginx-server-root为nginx设置文件路径,

    –nginx-ctl 为nginx可执行文件路径,

    如果是通过yum或者dnf安装的nginx,则这两个选项可以忽略。

    公钥默认路径: /etc/letsencrypt/live/littledot.net/fullchain.pem

    私钥默认路径: /etc/letsencrypt/live/littledot.net/privkey.pem

    测试证书自动更新:certbot renew --dry-run

    利用crontab自动更新证书,每天检查两次,在 01:00 和 13:00:

    输入:crontab -e 加一行:
    6 1,13 * * * certbot renew --renew-hook "systemctl restart nginx"

    通过这个命令可以查看各种定时器:systemctl list-timers

    有了证书之后,我们还需要为nginx设置https服务:
    nano /usr/local/nginx/conf/nginx.conf

    查找: HTTPS server 这部分,修改为:

      # HTTPS server
      #
      server {
          listen       443 ssl;
          server_name  localhost;
    
          ssl_certificate      /etc/letsencrypt/live/littledot.net/fullchain.pem;
          ssl_certificate_key  /etc/letsencrypt/live/littledot.net/privkey.pem;
        
          ssl_session_cache    shared:SSL:1m;
          ssl_session_timeout  5m;
        
          ssl_ciphers  HIGH:!aNULL:!MD5;
          ssl_prefer_server_ciphers  on;
        
          location / {
              root   html;
              index  index.php index.html index.htm;
          }
        
          location ~ \.php$ {
              root           html;
              fastcgi_pass   unix:/var/run/php-fpm.sock;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
              include        fastcgi_params;
          }
      }
    

    存盘退出,输入:systemctl restart nginx 重启nginx生效。


  • 如何利用nginx反向代理实现多域名映射到同一台服务器的80端口?

    nano /usr/local/nginx/conf/nginx.conf

    在这个文件里新开一个server,注意要放到http这个最外层的花括号内部,

    每一个域名单独用一段完整的server {} 包裹,当然,前提条件是这些域名都必须解析到同一个IP

        server {
           listen       80;  //一个nginx可以监听多个80端口
           server_name  abc.net; 
           //如果是浏览器输入的是abc.net,则在此做相应处理。
    
          location / {
              //设置根目录,注意如果网站放在/root内,需要做权限处理
              root   /root/website/public; 
              //如果网址不带任何参数,则访问默认文件helloworld.html
              index  helloworld.html;
          }
          //如果带参数,例如:abc.net/order,则映射到本机的其他地址
          //这些地址可以是Java的也可以是nodejs的API接口,也可以是静态页面,但必须是能够访问的页面。
          location /order {
               proxy_pass http://127.0.0.1:3000/order;
          }
        }
    

    如果你嫌一个一个路径去设置很麻烦,也可以在location里采用正则:

        location ~ /(.*) {
             proxy_pass http://127.0.0.1:3000/$1;
        }
    

    尽管不建议,但是如果你非要把网站设置放在/root里,要先执行chgrp www /root,否则会报权限错误(此www是指你安装php和nginx为系统添加的操作用户)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rockage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值