分类 Linux 下的文章

使用树莓配4B的 CentOS 8 Stream 系统尝试连接WIFI时查到的

nmcli device wifi list
# 显示可以搜索到的WiFi

nmcli device wifi connect SSID password PASSWORD
# 连接WiFi,SSID为WIFI名,PASSWORD为密码

nmcli device disconnect
# 断开连接

nmcli connect del SSID
# 删除WIFI,SSID为WIFI名

适用于CentOS 7 与 8.
防火墙的服务是 firewalld.service,使用 systemctl 管理即可。

systemctl enable firewalld
# 打开开机自启防火墙

systemctl disable firewalld
# 关闭开机自启防火墙

systemctl start firewalld
# 启动防火墙

systemctl reload firewalld
# 重载防火墙

systemctl status firewalld
# 查看状态

systemctl stop firewalld
# 关闭防火墙

firewall-cmd --list-ports
# 查看已放行的端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
# 放行端口,参数分别为:指定作用域、放行端口号与协议、设置永久生效

指令如下:

dnf --disablerepo '*' --enablerepo extras swap centos-linux-repos centos-stream-repos
dnf distro-sync

cat /etc/centos-release
# CentOS Stream release 8

123231124.png

# 压缩.tar文件
tar -cvf file.tar path

# 解压.tar文件
tar -xvf file.tar

# 压缩.gz文件
tar -zcvf file.tar.gz path

# 解压.gz文件
tar -zxvf file.tar.gz

# 压缩.bz2文件
tar -jcvf file.tar.gz path

# 解压.bz2文件
tar -jxvf file.tar.gz

# 参数说明:
# c 创建压缩文件;x 解压压缩文件;
# v 显示处理文件;f 指定文件名;
# z 使用gzip处理;j 使用bzip2处理;

脚本代码如下:

#!/bin/bash
echo -n "请设置MySQL密码:"
read password

echo "更新系统内核..."
dnf -y update

echo "正在安装 EPEL && Remi..."
dnf -y install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

echo "正在安装 Nginx-1.18..."
dnf -y module reset nginx
dnf -y module enable nginx:1.18
dnf -y install nginx

echo "正在设置 Nginx..."
echo 'user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    use epoll;
    worker_connections 102400;
}

http {
    log_format  main  '"'"'$remote_addr - $remote_user [$time_local] "$request" '"'"'
                      '"'"'$status $body_bytes_sent "$http_referer" '"'"'
                      '"'"'"$http_user_agent" "$http_x_forwarded_for"'"'"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_tokens off;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/php-fpm.conf;
    include /var/www/wwwconf/*.conf;
}' > /etc/nginx/nginx.conf

echo 'location ~ [^/]\.php(/|$){
    try_files $uri =404;
    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass   php-fpm;
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
        set $real_script_name $1;
        set $path_info $2;
    }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $path_info;
}' > /etc/nginx/default.d/php.conf

echo "正在安装 PHP-8.0"
dnf -y module reset php
dnf -y module enable php:remi-8.0
dnf -y install php php-fpm php-pdo php-mysqlnd php-mbstring

echo "正在安装 MySQL-8.0"
dnf -y module reset mysql
dnf -y module enable mysql:8.0
dnf -y install mysql mysql-server

echo "正在设置 MySQL..."
systemctl start mysqld
dnf -y install expect
echo '#!/usr/bin/expect
set timeout 60
set password [lindex $argv 0]
spawn mysql_secure_installation
expect {
"Press Y" { send "Y\r"; exp_continue}
"Press y" { send "y\r"; exp_continue}
"Please enter 0 = LOW" { send "0\r"; exp_continue}
"password" { send "$password\r"; exp_continue}
}
interact ' > mysql_secure_installation.exp 
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp $password 
rm -rf mysql_secure_installation.exp

echo "正在创建站点目录..."
rm -rf /var/www/*
mkdir /var/www/wwwconf
mkdir /var/www/wwwroot
mkdir /var/www/wwwssl
mkdir /var/www/wwwlog

echo '正在创建默认站点...'
echo 'server {
    listen   80 default_server;
    listen   [::]:80;
    server_name    127.0.0.1;
    index   index.php index.html;
    root   /var/www/wwwroot/default;
    access_log /var/www/wwwlog/default.log;
    include /etc/nginx/default.d/php.conf;
}' > /var/www/wwwconf/default.conf
mkdir /var/www/wwwroot/default
echo '<?php phpinfo(); ?>' > /var/www/wwwroot/default/index.php

echo "正在安装 phpmyadmin..."
wget -O phpmyadmin.zip https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip
unzip phpmyadmin.zip -d /var/www/wwwroot/default
mv /var/www/wwwroot/default/phpMyAdmin-5.0.4-all-languages /var/www/wwwroot/default/phpmyadmin
chmod -R 777 /var/www/wwwroot/default/*
rm -rf phpmyadmin.zip

echo "设置自动更新..."
echo "30 01 * * * dnf -y update >> /tmp/tmp.txt" >> /var/spool/cron/root
systemctl restart crond

echo "正在启动 Nginx..."
systemctl enable nginx
systemctl start nginx

echo "正在启动 PHP..."
systemctl enable php-fpm
systemctl start php-fpm

echo "正在启动 MySQL..."
systemctl enable mysqld
systemctl restart mysqld

echo "安装完成!"

- 阅读剩余部分 -

# 安装MySQL,CentOS 8 默认使用 MySQL8.0,直接安装即可
dnf -y install mysql mysql-server

# 设置自启 & 启动MySQL
systemctl enable mysqld
systemctl start mysqld

# 安全设置与root密码设置
mysql_secure_installation

# 查看所有服务状态(按 q 退出)
systemctl list-units

# 查看所有服务自启设置(按 q 退出)
systemctl list-unit-files

# 将服务设为开机自启
systemctl enable service

# 启动服务
systemctl start service

# 停止服务
systemctl stop service

# 查看服务状态
systemctl status service

# 重启服务
systemctl restart service

# 重载配置
systemctl stop service

# 将服务设为开机自启
systemctl enable service

# 禁用服务开机自启
systemctl disable service

# 查看当前用户的任务
crontab -l

# 编辑当前用户的任务
crontab -e

# 规则:
# 分 时 日 月 年 执行操作,以下内容即为 每天 01:30 执行 /root/backup.sh
# 30 01 * * * /bin/sh /root/backup.sh

# 重启 crontab
# CentOS 8 
systemctl restart crond

# CentOS 7
service crond restart