一.文件管理
1.目录操作
/bin:命令工具 /home:家目录 /etc:配置目录 /dev:设备目录 /root:管理员用户家目录
/usr:存放系统应用程序及文档 /mnt:临时挂载点 /tmp:存放临时文件 /var:存放系统运行中常改变文件(系统日志)
pwd:查看当前路径 clear:清空屏幕 poweroff:关机 reboot:重启
cd:切换目录 cd -:上一次访问目录 cd / :根目录 cd .. :上一级目录
绝对路径:从根开始说明
相对路径:从当前目录 ./ :从当前目录 ../ :从上一级目录
2.文件管理
ll:查看详细文件目录信息 ll -d:只查看此目录详细信息
ls:查看目录中文件名 ls -a:查看目录中文件名(包括隐藏文件)
touch:创建文件
mkdir:创建目录 mkdir -p :创建多个目录,以及父目录
cp:拷贝文件 (cp 源文件路径 目标目录) cp -r:复制目录 \cp -f:覆盖不询问
mv:移动文件或目录,以及重命名 mv -f:覆盖不询问
rm:删除文件或者目录(不可恢复) rm -r:删除目录以及目录下的内容 rm -rf:强制删除目录以及目录下内容
cat:查看文件内容 cat -n:显示行数
more:分屏显示文件,每次显示一页
less:分屏显示文件,每次显示一行
head:查看文件前十行,-x :显示文件前x行
tail:查看文件后十行,-x;显示文件后x行
grep:过滤关键字。grep ^root 文件位置 :指定文件以root开头的行
grep 'bash$' /etc/passwd 以bash结尾的内容行
sed -n ‘xp’ 文件名 :显示文件第几行,x为几
echo:写入 echo > 文件 :覆盖 echo >> 文件 :添加
echo $?:查看命令执行是否成功,成功为0
date :查看系统时间 date -s:修改系统时间
hwclock -r :查看硬件时间 hwclock -w:写入硬件
ntpdate ntp.aliyun.com:同步阿里云时间
ntpdate time.baidu.com:同步百度云时间
env:查看系统中已经存在的环境变量
3.vim编辑器
a,i,o:开始写入
esc :wq :保存退出
yy:复制 5yy:从此开始复制下五行 yy5:将此处复制五行
p:粘贴一行
gg:跳转至首位 G:跳转至尾部
:1,5 s/abc/ABC/g :将1-5行的abc替换为ABC (若替换全部文章的将1,5改为%)
x:删除光标处字符
u:撤销操作
/:查找关键字,按n选下一个
:set nu:设置行号 :set nonu:取消行号
ctrl v 进入可视块,若需要修改整列则 shift i 再加需要添加的字再按esc 即可
shift v 进入可视行
4.拓展
关闭防火墙与selinux,nginx
systemctl stop firewalld:关闭防火墙
systemctl disable firewald:永久关闭防火墙
systemctl status firewalld:查看防火墙状态
getenforce :查看selinux状态
vim /etc/sysconfig/selinux 将文件中 SELINUX=enforcing(改为disabled):永久关闭(需要重启)
systemctl start nginx :启动nginx
systemctl restart nginx :重启nginx
systemctl enable nginx:开机自启动nginx(其他与防火墙相同)
yum -y install lrzsz 安装lrzsz ,-y表示自动确认
rz:将本地文件上传至服务器
sz:从服务器下载至本地
scp 文件 桥接模式下ip:/路径 :从我电脑拷到ip电脑
ssh 桥接模式下ip :控制此ip的虚拟机
二.用户管理
1.创建用户组
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
-c:加上备注文字,备注文字保存在passwd的备注栏中。
-d:指定用户登入时的启始目录。
-D:变更预设值。
-e:指定账号的有效期限,缺省表示永久有效。
-f:指定在密码过期后多少天即关闭该账号。
-g:指定用户所属的起始群组。
-G:指定用户所属的附加群组。
-m:自动建立用户的登入目录。
-M:不要自动建立用户的登入目录。
-n:取消建立以用户名称为名的群组。
-r:建立系统账号。
-s:指定用户登入后所使用的shell。
-u:指定用户ID号。
使用语法:useradd “用户” -p “ 密码” -g “初始组” -G “附加组”
id:查看用户 exit:退出用户 passwd:修改用户密码
用户信息:/etc/passwd
用户密码信息:/etc/shadow
组信息:/etc/group
userdel:删除用户 userdel -r :同时删除用户文件夹
usermod:修改用户 usermod :指定用户登录shell -G:指定用户所属附加组
usermod -u :修改用户uid
格式:usermod -aG 组名 用户名 (修改用户附加组)
group :创建组 groupdel :删除组 groupmod:修改组
groupmod -g id 组名:修改组id groupmod -n 新组名 旧组名 :修改组名
在组中添加用户:usermod -aG 组名 用户
用户组
创建组
[root@qfedu ~]# groupadd hr #创建一个用户组叫hr
[root@qfedu ~]# groupadd it #创建一个用户组叫it
[root@qfedu ~]# groupadd net01 -g 2000 #创建组叫net01,并指定gid为2000
[root@qfedu ~]# grep 'net01' /etc/group #查看/etc/group中组net01信息
net01:x:2000:删除组
[root@qfedu ~]# groupdel net01 #删除组net01
[注意:用户的主组不能删除]
用户操作
1.修改UID
-u
[root@qfedu ~]# usermod -u new_id jack
[root@qfedu ~]# id jack
uid=1005(jack) gid=1004(jack) groups=1004(jack)
2.修改用户的登录shell
-s
[root@qfedu ~]# usermod -s /sbin/nologin user07 #修改用户的登录shellusermod -aG 添加用户到附属组
usermod -g 指定主组
usermod -G 指定附加组,会覆盖其他的附加组
仅限已经存在用户
1.给组添加账户
-a
[root@qfedu ~]# gpasswd -a user10 grp2
Adding user user10 to group grp2
2.同时添加多个用户到组
-M:members成员的缩写
[root@qfedu ~]# gpasswd -M tom,alice it
3.从组删除用户1
-d
[root@qfedu ~]# gpasswd -d user07 hr
Removing user user07 from group hr
2.批量创建用户
vim 文件 文件中竖着写出需要创建的用户名
cat 文件 | xargs -i useradd {}
修改其他用户密码
echo ‘新密码’ | passwd --stdin 用户
3.用户权限(UGO)
权限 | 对文件的影响 | 对目录的影响 |
r=4 | 可以读取文件的内容 | 可以列出目录的内容(文件名) |
w=2 | 可以更改文件的内容 | 可以创建或删除目录中的任一文件 |
x=1 | 可以作为命令执行文件 | 可以访问目录的内容(取决于目录文件中的权限) |
语法:chmod 对象(u/g/o/a) 赋值(+/-/=) 权限类(r/w/x) 文件或目录
数字: chmod 644 文件目录
chown:设置一个文件属于谁,属主(修改文件/文件夹的用户和用户组属性)
语法 :chown 用户名.组名 文件
chown -R:针对目录中所有文件
4.隐藏权限
stat 文件 :查看文件状态
lsattr :查看文件目录属性,隐藏属性
chattr :更改文件隐藏属性
[root@linux-server ~]# touch file1 file2 file3
1.查看文件属性
[root@linux-server ~]# lsattr file1 file2 file3
---------------- file1
---------------- file2
---------------- file3
2.设置权限
[root@linux-server ~]# chattr +a file1 #不允许修改,只允许追加
[root@linux-server ~]# chattr +i file2 #不允许做任何操作
[root@linux-server ~]# chattr +A file3
[root@linux-server ~]# lsattr file1 file2 file3
-----a---------- file1
----i----------- file2
-------A-------- file3
i:即Immutable,系统不允许对这个文件进行任何的修改
a:即Append Only,系统只允许在这个文件之后追加数据,不允许任何覆盖这个文件
A:即Atime,告诉系统不要修改对这个文件的最后访问时间
例子:
[root@linux-server ~]# echo 111 > file1 #覆盖,只允许追加
-bash: file1: Operation not permitted
[root@linux-server ~]# rm -rf file1 #不能删除
rm: cannot remove ‘file1’: Operation not permitted
[root@linux-server ~]# echo 111 >> file1 #追加[root@linux-server ~]# echo 111 > file2
-bash: file2: Permission denied
[root@linux-server ~]# echo 111 >> file2
-bash: file2: Permission denied
[root@linux-server ~]# rm -rf file2
rm: cannot remove ‘file2’: Operation not permitted
[root@linux-server ~]# mv file2 file4
mv: cannot move ‘file2’ to ‘file4’: Operation not permitted取消权限
[root@linux-server ~]# chattr -a file1
[root@linux-server ~]# chattr -i file2
5.高级权限
高级权限 suid普通文件 sgid,sticky 目录
这三个权限不能给到同一个文件
suid ==== 4 提权 (只对二进制命令文件生效,其他不管用)
sgid ==== 2 继承属组权限 (只能对目录设置)
sticky == 1 (t权限) 权限控制 (目录)
1.设置特殊权限
a、字符---语法:
suid:chmod u+s 命令 提权 (只针对二进制命令文件)
sgid:chmod g+s dir 只能对目录设置 (相当于root时此目录所持有的权限)
sticky:chmod o+t dir 即使有删除权限,也不能删除b、数字
chmod 4777 file
chmod 2770 dir
chmod 1770 dir
案例:set uid
suid 普通用户通过suid提权 <针对文件>
在进程文件(二进制,可执行的命令文件)上增加suid权限
[root@linux-server ~]# chmod u+s /usr/bin/cat
[root@linux-server ~]# chmod u+s /usr/bin/rm
[root@linux-server ~]# su - alice
Last login: Wed Nov 6 17:40:40 CST 2019 on pts/0
[alice@linux-server ~]$ cat /root/file1.txt
123
[alice@linux-server ~]$ rm -rf /root/file1.txt
2.set gid
把s放到文件的所属用户组的x位置上的话,就是SGID。那么SGID的功能是什么呢?和SUID一样,只是SGID是继承改程序的所属组。
SGID主要用在目录上-----如果用户在此目录下具有w权限的话,使用者在此目录下建立新文件,则创建的这个文件的群组与此目录的群组相同。
案例:
[root@linux-server ~]# mkdir /opt/dir1 #创建目录
[root@linux-server ~]# groupadd hr #创建一个组
[root@linux-server ~]# chmod 775 /opt/dir1/ #设置权限
[root@linux-server ~]# ll -d /opt/dir1/
drwxrwxr-x. 2 root root 6 Nov 6 21:26 /opt/dir1/
[root@linux-server ~]# chown .hr /opt/dir1/ #设置属组
[root@linux-server ~]# chmod g+s /opt/dir1/ #设置sgid
[root@linux-server ~]# ll -d /opt/dir1/
drwxrwsr-x. 2 root hr 6 Nov 6 21:26 /opt/dir1/
[root@linux-server ~]# touch /opt/dir1/a.txt
[root@linux-server ~]# ll /opt/dir1/a.txt
-rw-r--r--. 1 root hr 0 Nov 6 21:33 /opt/dir1/a.txt[root@linux-server ~]# chmod o+w /opt/dir1/ -R
[root@linux-server ~]# su - alice
Last login: Wed Nov 6 21:34:59 CST 2019 on pts/2
[alice@linux-server ~]$ touch /opt/dir1/b.txt
[alice@linux-server ~]$ ll /opt/dir1/b.txt
-rw-rw-r--. 1 alice hr 0 Nov 6 21:35 /opt/dir1/b.txt
3.Sticky Bit
这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。
SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。
案例:
[root@linux-server ~]# cd /home/
[root@linux-server home]# mkdir dir2
[root@linux-server home]# chmod 757 dir2/
[root@linux-server home]# chmod o+t dir2/
[root@linux-server home]# ll -d dir2/
drwxr-xrwt. 2 root root 52 Oct 31 16:49 dir2/
[root@linux-server home]# useradd jack #创建用户
[root@linux-server home]# su - alice
Last login: Wed Nov 6 21:48:12 CST 2019 on pts/2
[alice@linux-server ~]$ touch /home/dir2/alice.txt #用户alice创建文件
[alice@linux-server ~]$ exit
logout
[root@linux-server home]# su - jack
Last login: Wed Nov 6 21:48:36 CST 2019 on pts/2
[jack@linux-server ~]$ touch /home/dir2/jack.txt #用户jack创建文件
[jack@linux-server ~]$ rm -rf /home/dir2/alice.txt
rm: cannot remove ‘/home/dir2/alice.txt’: Operation not permitted
测试jack删除alice创建的文件,无法删除
6.sudo用户
赋予sudu权限(一)
1. 编辑/etc/sudoers配置文件
跳到第100行,将要赋予sudo权限的用户按照root ALL=(ALL) ALL 的格式写在下一行,然后强制保存退出
[root@server-1 ~]# vim /etc/sudoers
检查语法是否正确,一定要执行,否则出现问题很麻烦
[root@server-1 ~]# visudo -c
/etc/sudoers: parsed OK
第一次执行sudo时需要user01用户的密码,如果想要免除每次使用sudo命令输入密码,则修改为
user01 ALL =ALL NOPASSWD: ALL
赋予sudu权限(二)
若多个用户都需要sudo权限的话,一个一个添加不方便,这样的话需要实现批量添加 即可以将用户加到用户组wheel下
创建用户时指定在wheel组内,wheel组内的用户拥有sudo权限,并且wheel组在系统内部已存在的[root@server-1 ~]# useradd user01 -G wheel
添加已存在的用户到wheel组
[root@server-1 ~]# usermod -aG wheel user01
使用sudo命令时需要输入用户的密码,如不想输入用户密码则修改/etc/sudoers文件第110行,将行首的'#'删除
允许“wheel”用户组的成员运行所有命令
%wheel ALL=(ALL) ALL
允许“wheel”用户组的成员运行所有命令,且运行时不需要输入密码
%wheel ALL=(ALL) NOPASSWD: ALL
允许“user01”用户运行所有命令
user01 ALL=(ALL) ALL
允许“user01”用户运行所有命令,且运行时不需要输入密码
user01 ALL=(ALL) NOPASSWD: ALL
ACL
setfacl:针对个人设置权限,一个人查看一个文件
getfacl 查看权限
语法:setfacl -m u:alice:rw /home/test.txt
命令 设置 用户或组:用户名:权限 文件对象
当 - rw- r-- r-- . 1 当为 . 时表示无特殊权限为 + 时表示有特殊权限 此时用getfacl去查看
7.拓展
从本地拷贝文件到远程服务器
将当前目录下file1文件拷贝到服务器ip为'192.168.31.99'的root目录下
[root@server-1 ~]# scp file1 192.168.31.99:/root/
从远程服务器'192.168.31.99'拷贝/etc/passwd文件到当前目录
[root@server-1 ~]# scp 192.168.31.99:/etc/passwd ./
将/opt/dir1目录拷贝到服务器ip为'192.168.31.99'的/opt/目录下
[root@server-1 ~]# scp -r /opt/dir1 192.168.31.99:/opt/
将远程服务器'192.168.31.99'内的/opt/dir1目录拷贝到本服务器的/opt/目录下
[root@server-1 ~]# scp -r 192.168.31.99:/opt/dir1 /opt
8.修改网卡配置文件(设置静态ip)
新建虚拟机如若不能获取ip的话,输入dhclient -v 手动获取ip
[root@server-2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
#将dhcp修改为static
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="0ed8193f-42e1-4379-bb46-570ee8f89e7e"
DEVICE="ens33"
#系统启动时是否激活网卡
ONBOOT="yes"
#以下新增以实际为准
#设置静态IP地址
IPADDR=192.168.203.128 (以ip 192.168.203.128为例)
#设置子网掩码
FREFIX=24
或NETMASK=255.255.255.0
#设置网关
GATEWAY=192.168.203.2
#设置DNS
DNS1=114.114.114.114
DNS2=8.8.8.8
重启网络服务
[root@server-2 ~]# systemctl restart network
9.项目一:LNMP
yum源基础配置
配置yum源:先cd 进入/etc/yum/yum.repos.d中再创建一个 文件名.repo的文件 再vim编辑配置
从yum源中下载软件:yum -y install 软件名
[base]
name=base
baseurl=http://package.qf.com/base
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=http://package.qf.com/epel
enabled=1
gpgcheck=0
[updates]
name=updates
baseurl=http://package.qf.com/updates
enabled=1
gpgcheck=0
[extras]
name=extras
baseurl=http://package.qf.com/extras
enabled=1
gpgcheck=0
如若域名无法使yum进行下载则进入 /etc/hosts中添加域名的访问ip
1.下载配置nginx
1.1 安装编译Nginx依赖包
# yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gd-devel
1.2 官方下载Nginx安装包 (网址如下)或者从电脑中导入镜像
wget https://nginx.org/download/nginx-1.22.1.tar.gz
1.3创建Nginx运行用户
useradd -s /sbin/nologin -M nginx
-s 指定用户登录后使用的shell -M不自动建立用户的家目录
1.4解压配置Nginx编译
[root@qfedu.com ~ ]# tar zxvf nginx-1.22.1.tar.gz -C /usr/local/
root@localhost ~]# cd /usr/local/nginx-1.22.1/
[root@qfedu.com nginx-1.22.1]# ./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--sbin-path=/usr/sbin/nginx \
--error-log-path=/var/log/nginx/nginx_error.log \
--http-log-path=/var/log/nginx/nginx_access.log \
--pid-path=/usr/local/nginx/run/nginx.pid
1.5.Nginx编译安装
# make && make install
1.6.测试Nginx是否安装成功
[root@localhost nginx-1.22.1]# nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid
1.7.启动服务:
/usr/sbin/nginx
1.8.验证Nginx服务是否启动成功
# netstat -lntp | grep nginx (如若显示没有netstat命令则yum下载此命令包名称为net-tools)
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29740/nginx: master
# ps aux |grep nginx
1.9系统添加Nginx服务
以systemd形式添加
1.创建nginx.service文件
[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
2.以systemctl方式启动Nginx
[root@qfedu.com ~]# pkill nginx
[root@qfedu.com ~]# systemctl daemon-reload
[root@qfedu.com ~]# systemctl start nginx
3.查看Nginx服务状态
[root@qfedu.com ~]# ps -ef | grep ngin
root 31469 1 0 23:11 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 31470 31469 0 23:11 ? 00:00:00 nginx: worker process
root 31554 1182 0 23:11 pts/0 00:00:00 grep --color=auto ngin
4.验证Nginx服务是否成功启动
[root@qfedu.com ~]# netstat -ntlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 349/nginx: master p
5.配置Nginx服务自动启动
[root@qfedu.com ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
2.编译安装Mysql
2.1 清理安装环境
# yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
# userdel -r mysql
# rm -rf /etc/my*
# rm -rf /var/lib/mysql
2.2 创建mysql用户
root@mysql-server ~]# useradd -r mysql -M -s /bin/nologin
-M 不创建用户的家目录
2.3 从官网下载tar包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
2.4 安装编译工具
# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make glibc automake autoconf
cmake:
# yum -y install cmake
2.5 解压
[root@mysql-server ~]# tar xzvf mysql-boost-5.7.27.tar.gz -C /usr/local/
2.6 编译安装
[root@mysql-server ~]# cd /usr/local/mysql-5.7.27/
[root@mysql-server mysql-5.7.27]# cmake . \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
参数详解:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 安装目录
-DSYSCONFDIR=/etc \ 配置文件存放 (默认可以不安装配置文件)
-DMYSQL_DATADIR=/usr/local/mysql/data \ 数据目录 错误日志文件也会在这个目录
-DINSTALL_MANDIR=/usr/share/man \ 帮助文档
-DMYSQL_TCP_PORT=3306 \ 默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ sock文件位置,用来做网络通信的,客户端连接服务器的时候用
-DDEFAULT_CHARSET=utf8 \ 默认字符集。字符集的支持,可以调
-DEXTRA_CHARSETS=all \ 扩展的字符集支持所有的
-DDEFAULT_COLLATION=utf8_general_ci \ 支持的
-DWITH_READLINE=1 \ 上下翻历史命令
-DWITH_SSL=system \ 使用私钥和证书登陆(公钥) 可以加密。 适用与长连接。坏处:速度慢
-DWITH_EMBEDDED_SERVER=1 \ 嵌入式数据库
-DENABLED_LOCAL_INFILE=1 \ 从本地倒入数据,不是备份和恢复。
-DWITH_INNOBASE_STORAGE_ENGINE=1 默认的存储引擎,支持外键
[root@mysql-server mysql-5.7.27]# make && make install
2.7 初始化
[root@mysql-server mysql-5.7.27]# cd /usr/local/mysql
[root@mysql-server mysql]# chown -R mysql.mysql .
[root@mysql-server mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ---初始化完成之后,一定要记住提示最后的密码用于登陆或者修改密码
设置环境变量
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@mysql-server ~]# vim /etc/my.cnf --如果打开文件有内容将文件中所有内容注释掉,在添加如下内容
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
user = mysql
basedir = /usr/local/mysql #指定安装目录
datadir = /usr/local/mysql/data #指定数据存放目录
socket = /tmp/mysql.sock
character_set_server = utf8
[client]
# 默认连接端口
port = 3306
# 用于本地连接的socket套接字
socket = /tmp/mysql.sock
# 编码
default-character-set = utf8
[mysqld]
# 服务端口号,默认3306
port = 3306
# mysql启动用户
user = mysql
# mysql安装根目录
basedir = /usr/local/mysql
# mysql数据文件所在位置
datadir = /usr/local/mysql/data
# 为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
socket = /tmp/mysql.sock
# 数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character_set_server = utf8
2.8 启动mysql
[root@mysql-server ~]# cd /usr/local/mysql
[root@mysql-server mysql]# ./bin/mysqld_safe --user=mysql &
启动之后再按一下回车!即可后台运行
2.9 登录mysql
[root@mysql-server mysql]# /usr/local/mysql/bin/mysql -uroot -p'GP9TKGgY9i/8'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
2.10 systemctl启动方式
拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
[root@qfedu.com mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@qfedu.com mysql]# ls -l /etc/init.d/mysqld -rwxr-xr-x 1 root root 10588 Aug 1 18:33 /etc/init.d/mysqld
重新加载系统服务
[root@localhost mysql]# systemctl daemon-reload
启动MySQL数据库,并检查端口监听状态
[root@localhost mysql]# systemctl stop mysqld --停止mysqld
# 或者
[root@localhost mysql]# systemctl start mysqld --启动mysqld
Starting MySQL. SUCCESS!
[root@localhost mysql]# netstat -lntp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 16744/mysqld
2.11 数据库密码修改
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p'老密码' password '新密码'
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p'新密码'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database wordpress;
mysql> grant all on *.* to 'remote'@'%' identified by '123';
mysql> flush privileges;
3.编译安装Php
3.1 下载Php源码包
# wget https://www.php.net/distributions/php-7.3.6.tar.gz(或者从电脑上用rz导入镜像)
# yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel libxml2-devel curl-devel
3.2 配置Php编译
[root@localhost ~]# tar xzvf php-7.3.6.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/php-7.3.6/
[root@qfedu.com php-7.3.6 ]# ./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7 \
--with-config-file-scan-dir=/usr/local/php7/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache
3.3 Php编译参数说明
--prefix=/usr/local/php7 # 配置安装目录
--with-config-file-path=/usr/local/php7 # 配置文件 php.ini 的路径
--enable-sockets # 开启 socket
--enable-fpm # 启用 fpm 扩展
--enable-cli # 启用 命令行模式 (从 php 4.3.0 之后这个模块默认开启所以可以不用再加此命令)
--enable-mbstring # 启用 mbstring 库
--enable-pcntl # 启用 pcntl (仅 CLI / CGI)
--enable-soap # 启用 soap
--enable-opcache # 开启 opcache 缓存
--disable-fileinfo # 禁用 fileinfo (由于 5.3+ 之后已经不再持续维护了,但默认是开启的,所以还是禁止了吧)(1G以下内存服务器直接关了吧)
--disable-rpath # 禁用在搜索路径中传递其他运行库。
--with-mysqli # 启用 mysqli 扩展
--with-pdo-mysql # 启用 pdo 扩展
--with-iconv-dir # 启用 XMLRPC-EPI 字符编码转换 扩展
--with-openssl # 启用 openssl 扩展 (需要 openssl openssl-devel)
--with-fpm-user=nginx # 设定 fpm 所属的用户
--with-fpm-group=nginx # 设定 fpm 所属的组别
--with-curl # 启用 curl 扩展
--with-mhash # 开启 mhash 基于离散数学原理的不可逆向的php加密方式扩展库
# GD
--with-gd # 启用 GD 图片操作 扩展
--with-jpeg-dir # 开启对 jpeg 图片的支持 (需要 libjpeg)
--with-png-dir # 开启对 png 图片支持 (需要 libpng)
--with-freetype-dir # 开启 freetype
# xml
--enable-simplexml # 启用对 simplexml 支持
--with-libxml-dir # 启用对 libxml2 支持
--enable-debug # 开启 debug 模式
3.4 编译安装Php
[root@qfedu.com php-7.3.6]# make && make install (等待下载半小时)
3.5 创建php.ini配置文件
[root@localhost php-7.3.6]# cp php.ini-production /usr/local/php7/etc/php.ini
[root@localhost php-7.3.6]# vim /usr/local/php7/etc/php.ini
#1371行将注释打开 #php的Session存储目录
session.save_path = "/tmp"
3.6 设置php-fpm配置文件
[root@qfedu.com php-7.3.6]# cd /usr/local/php7/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
#17行将注释取消并修改
pid = /var/run/php-fpm.pid
# php-fpm连接文件
[root@localhost etc]# cd /usr/local/php7/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# vim www.conf
user = nginx
group = nginx
listen = 127.0.0.1:9000
3.7 启动php-fpm
[root@qfedu.com php-fpm.d]# /usr/local/php7/sbin/php-fpm
3.8 检查php-fpm 是否成功启动
[root@qfedu.com php-fpm.d]# ps aux | grep php-fpm
若看到相关进程,则证明启动成功。查询进程时,进程是以 nginx 用户身份执行的
ss -nplt | grep 9000
3.9 配置php-fpm 系统环境变量
[root@localhost php-fpm.d]# cd
[root@qfedu.com ~]# vim /etc/profile
export PHP_HOME=/usr/local/php7
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
3.10 重载环境变量
[root@qfedu.com ~]# source /etc/profile
使用 echo $PATH 命令查看环境变量中是否已经加入了相关的路径
3.11 配置php-fpm开机自启动
[root@qfedu.com ~]# vim /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php7/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.12 php-fpm.service 文件说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
3.13 重载systemctl配置
[root@qfedu.com ~]# systemctl daemon-reload
3.14 停止php-fpm
[root@qfedu.com ~]# pkill php-fpm
3.15 用systemctl 启动 php-fpm
[root@qfedu.com ~]# systemctl start php-fpm.service
3.16 设置 php-fpm 开机启动
[root@qfedu.com ~]# systemctl enable php-fpm.service
3.17 php-fpm管理命令
[root@qfedu.com ~]# systemctl stop php-fpm.service # 停止服务
[root@qfedu.com ~]# systemctl restart php-fpm.service # 重新启动服务
[root@qfedu.com ~]# systemctl status php-fpm.service # 查看服务当前状态
[root@qfedu.com ~]# systemctl disable php-fpm.service # 停止开机自启动
4. 基于LNMP环境上线wordpress
先使用 rz 将wordpress导入到linux中
再:tar xzvf wordpress-5.9.1-zh_CN.tar.gz
rm -rf /usr/local/nginx/html/*
cp -r wordpress/* /usr/local/nginx/html/ #将代码拷贝到网站发布目录中
systemctl restart nginx
创建数据库
[root@localhost html]# /usr/local/mysql/bin/mysql -uroot -p'Qf@123!'
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> \q
Bye
再打开浏览器输入ip操作
如果不能创建wp-config.php请手动创建
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim wp-config.php ---将提示框中的信息粘贴进去
三 . 进程管理
1.1什么是进程
进程是已启动的可执行程序的运行实例,是程序运行的过程,动态的,有生命周期及运行状态。
程序: 二进制文件,静态 /bin/date, /usr/sbin/httpd,/usr/sbin/sshd, /usr/sbin/nginx
1.2查看进程process
静态查看进程(一)
[root@linux-server ~]# ps aux | less
参数解释:
ps :process nsapashot
a 只能查看系统里面运行的所有终端进程
u 显示进程拥有者
x 显示系统内所有进程
f 显示进程之间的父子关系
[root@linux-server ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
============================================================================
USER: #运行进程的用户
PID: #进程ID
%CPU: #CPU占用率
%MEM: #内存占用率
VSZ 进程占用的虚拟内存大小
RSS 占用的物理内存大小
STAT: #进程状态 ---了解
? 表示没有占用终端
R 运行
S 可中断睡眠 Sleep
D 不可中断睡眠
T 停止的进程
Z 僵尸进程
X 死掉的进程
START: #进程的启动时间
TIME: #进程占用CPU的总时间
COMMAND: #进程文件,进程名
进程状态--了解
Sl 以线程的方式运行
Ss s进程的领导者,父进程
R+ +表示是前台的进程组
S< <优先级较高的进程
SN N优先级较低的进程
静态查看进程(二)
[root@linux-server ~]# ps -ef
参数解释:
-e 显示所有进程
-l 长格式显示
-f 完整格式
UID 用户ID
PID 进程ID
PPID 父进程ID
C CPU占用率
STIME 开始时间
TTY 开始此进程的TTY----终端设备
TIME 此进程运行的总时间
CMD 命令名
查看单个PID
[root@linux-server ~]# yum install -y httpd #安装apache软件
[root@linux-server ~]# systemctl start httpd #启动
[root@linux-server ~]# cat /var/run/httpd/httpd.pid
1043
查看指定PID
[root@linux-server ~]# ps aux | grep sshd
查看端口:lsof -i:端口号
查网络进程和正在监听的端口 :netstat -lntp
1.3 动态查看进程
top -d1(每一秒刷新) top -d1 -p PID (查看指定进程的动态信息)
top - 13:30:25 up 1 min, 2 users, load average: 1.89, 0.60, 0.21
程序名-系统时间,运行时间 登录用户数 CPU负载 1分钟 5分钟 15分钟
Tasks: 208 total, 2 running, 206 sleeping, 0 stopped, 0 zombie
总进程数208 运行数2 睡眠数206 停止数0 僵死数0
第三行:%Cpu(s): 3.4 us, 2.0 sy, 0.0 ni, 94.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
CPU使用占比
us:用户 sy:系统 ni:优先级 id:空闲 wa:等待 hi:硬件 si:软件 st:虚拟机
KiB Mem : 995676 total, 66648 free, 680676 used, 248352 buff/cache
物理内存K total 总共 10G free空闲6.6G used使用6.8G cache缓存硬盘内容2G
KiB Swap: 2097148 total, 2080244 free, 16904 used. 159104 avail Mem
交换内存K total20G free20G used使用1.6G 内存占用:16G
进程控制
kill
pkill(可以指定进程名字)
杀死单个进程的时候可以使用kill,杀死多个同名进程的时候可以使用pkill
语法:kill 信号 PID
kill -l 查看所有信号
-1 :重新加载进程或者重新加载配置文件,pid不变
-9:强制杀死
-15:正常杀死(可以不加数字)
-18:激活进程 -19 :挂起进程
作业控制
1、将终端运行的进程挂到后台运行
[root@localhost ~]# sleep 1000 ctrl + z 暂停 bg %<工作号> 挂入后台运行
2、将后台运行的进程调到终端运行
fg %<工作号> 调到前台运行
3、查看当前终端挂入后台的进程
jobs