Linux常用命令
1、Cnetos7使用yum安装nginx
- 查看gcc版本
gcc -v
- gcc安装命令
yum -y install gcc
- 安装openssl
# openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔。。。。。。
yum install -y openssl openssl-devel
- 安装nginx
yum install -y nginx
- 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
2、开放防火墙端口
- 开启防火墙
systemctl start firewalld.service
- 关闭防火墙
systemctl stop firewalld.service
- 重启防火墙
firewall-cmd --reload
- 开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重新加载
firewall-cmd --reload
- 查看已开放的端口
firewall-cmd --list-ports
3、安装jdk
- 解压jdk安装包
# 解压
tar -zxvf jdk-8u271-linux-x64.tar.gz
# 修改解压后的文件名称为jdk1.8
mv xxx/ jdk1.8
- 配置环境变量
vim /etc/profile
# 在最后边加
# JAVA环境变量
JAVA_HOME=/xxx/xxx/xxx
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
- 测试是否安装成功
[root@iZ2ze7jmwvt3iec793aif4Z java]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[root@iZ2ze7jmwvt3iec793aif4Z java]# javac
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-parameters Generate metadata for reflection on method parameters
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-h <directory> Specify where to place generated native header files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-profile <profile> Check that API used is available in the specified profile
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file
4、Linux下创建用户
- 使用root用户登录,添加用户
# useradd -d /为此用户设置主目录 -m 用户名
useradd -d /xxx -m xxx
- 设置密码
[root@iZ2ze7jmwvt3iec793aif4Z shell]# passwd xxx
Changing password for user xxx.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
- 登录测试
5、netstat的用法
- 列出所有连接
netstat -a
- 只列出TCP或UDP协议的连接
#列出TCP协议的连接
netstat -at
#列出UDP协议的连接
netstat -au
- 禁用反向域名解析,加快查询速度
# 默认情况下 netstat 会通过反向域名解析技术查找每个 IP 地址对应的主机名。这会降低查找速度。如果你觉得 IP 地址已经足够,而没有必要知道主机名,就使用 -n 选项禁用域名解析功能。
netstat -ant
- 只列出监听中的连接
netstat -tnl
- 获取进程名、进程号以及用户id
sudo netstat -nlpt
- 打印统计数据
# netstat 可以打印出网络统计数据,包括某个协议下的收发包数量
# 如果想只打印出 TCP 或 UDP 协议的统计数据,只要加上对应的选项(-t 和 -u)即可
netstat -s
- 打印active状态的连接
netstat -atnp | grep ESTA
# 配合watch命令监视active状态的连接
watch -d -n0 "netstat -atnp | grep ESTA"
6、JVM常用调优参数
-Xmx #堆内存最大值 -Xmx2g
-Xms #堆内存最小值 -Xms2g
-Xmn #新生代大小 默认时堆的1/3
-xss #线程栈空间大小 -Xss256k
7、Centos7安装MySQL8
- 下载rpm包
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
- 安装mysql80-community-release-el7-1.noarch.rpm包
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm
- 安装mysql服务
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# sudo yum install mysql-server
安装过程中可能遇到如下报错无法安装,原因是Mysql的GPG升级了,需要重新获取
执行一下命令
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
- 启动并设置开机自启
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# systemctl start mysqld.service
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-11-22 20:33:07 CST; 8s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 31381 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 31459 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─31459 /usr/sbin/mysqld
Nov 22 20:33:00 iZ2ze7jmwvt3iec793aif4Z systemd[1]: Starting MySQL Server...
Nov 22 20:33:07 iZ2ze7jmwvt3iec793aif4Z systemd[1]: Started MySQL Server.
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# systemctl enable mysqld.service
- 查看初始密码
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# grep "A temporary password" /var/log/mysqld.log
- 登录
[root@iZ2ze7jmwvt3iec793aif4Z mysql]# mysql -uroot -p
Enter password:
# 查看当前的密码验证策略
mysql> SHOW VARIABLES LIKE 'validate_password.%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)
# 策略说明
# validate_password.length 是密码的最小长度,默认是8,我们把它改成6
# 输入:set global validate_password.length=6;
# validate_password.policy 验证密码的复杂程度,我们把它改成0
# 输入:set global validate_password.policy=0;
# validate_password.check_user_name 用户名检查,用户名和密码不能相同,我们也把它关掉
# 输入:set global validate_password.check_user_name=off;
# 再执行修改密码的命令
# 输入:ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';
# 密码设成功
- 修改密码
mysql> alter user 'root'@'localhost' IDENTIFIED BY 'xxxxx';
Query OK, 0 rows affected (0.01 sec)
- 设置允许远程登陆
mysql> select host, user, authentication_string, plugin from user;
mysql> update user set user.host='%'where user.user='root';
Query OK, 1 row affected (0.01 sec)
- MySQL8修改加密方式规则,caching_sha2_password需要改成mysql_native_password
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxx';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
8、npm换源(淘宝)
npm config set registry https://registry.npm.taobao.org
9、linux环境下使用logrotate工具实现nginx日志切割
一. 前提背景及需求
nginx运行日志默认保存在nginx安装目录下的 /usr/local/nginx/logs文件夹, 包含access.log和error.log两个文件.
(1) access.log记录了哪些用户、哪些页面以及用户浏览器、ip和其他的访问信息;
(2) error.log 则是记录服务器错误日志.
在所有时间内nginx产生的日志均保存在同一个文件下, 随着访问量的增加,尤其是access.log增长极快,服务器会很快消耗磁盘空间,影响服务器效率。
另外,当需要对日志文件里面记录的数据进行分析时,每次都要耗时很久才能下载这个庞大的日志文件,浪费不必要的时间。
因此急需一个处理方案能够自动化的实现按天或者按文件大小来切割nginx日志记录.
二. 解决方案: 使用logrotate工具实现日志切割
1. logrotate工具的介绍
logrotate是一个linux系统日志的管理工具。可以对单个日志文件或者某个目录下的文件按时间/大小进行切割,压缩操作;指定日志保存数量;还可以在切割之后运行自定义命令。
logrotate是基于crontab运行的,所以这个时间点是由crontab控制的,具体可以查询crontab的配置文件/etc/anacrontab。系统会按照计划的频率运行logrotate,通常是每天。在大多数的Linux发行版本上,计划每天运行的脚本位于 /etc/cron.daily/logrotate。
主流Linux发行版上都默认安装有logrotate包,如果你的linux系统中找不到logrotate, 可以使用apt-get或yum命令来安装。
接下来,我们查看logrotate的配置文件
logrotate的配置文件是 /etc/logrotate.conf, 这个文件用来定义全局默认参数
[root@iZ2ze7jmwvt3iec793aif4Z nginx]# rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/etc/rwtab.d/logrotate
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.8.6
/usr/share/doc/logrotate-3.8.6/CHANGES
/usr/share/doc/logrotate-3.8.6/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate
/var/lib/logrotate/logrotate.status
[root@iZ2ze7jmwvt3iec793aif4Z nginx]#
其中, /etc/logrotate.d/ 是用于存储各种自定义应用的配置文件的目录。该目录里的所有文件都会被主动的读入到 /etc/logrotate.conf中执行。该目录下的应用配置文件继承所有**/etc/logrotate.conf 的**默认参数。
因此我们可以新建一个针对nginx日志文件的轮循配置的文件,然后将这个文件放在 /etc/logrotate.d 目录下, 它就会主动的读入到/etc/logrotate.conf中执行, 以达到按指定频率定时执行的需求。
2. 创建nginx日志分割文件 (路径: /etc/logrotate.d/nginx )
# 需要轮询日志路径
/var/log/nginx/logs/*.log {
# 日志文件分割频度。可选值为 daily,monthly,weekly,yearly
daily
# 一次将存储30个归档日志。对于第31个归档,时间最久的归档将被删除。
rotate 30
# 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
missingok
# 使用日期作为命名格式
dateext
# 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
compress
# 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
delaycompress
# 如果日志文件为空,轮循不会进行。
notifempty
# 表示postrotate脚本在压缩了日志之后只执行一次
sharedscripts
# 最通常的作用是让应用重启,以便切换到新的日志文件, 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。
postrotate
/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
3. 运行logrotate
logrotate/etc/logrotate.d/nginx
测试日志切割 (如果文件的时间小于一天,不会执行切割日志 )
logrotate -d /etc/logrotate.d/nginx
强制轮询切割日志 ( 为了便于我们直观的观察测试结果,建议大家手动试一下 )
logrotate -vf /etc/logrotate.d/nginx
10、将dos格式文本转化为unix格式
1. 安装dos2unix
yum install dos2unix
2. 执行转换
# dos2unix 文件路径/文件名称
dos2unix /etc/logrotate.d/nginx