一个 PHPer 的 Linux 入门小册子
::Linux 从入门到精通
第一步:安装软件
Virtualbox(V5.1.8) 下载地址:
CentOS下载:
http://mirrors.163.com/centos/7/isos/x86_6...-
x86_64-Minimal-1611.iso
官方下载地址: https://www.centos.org/download/
第二步:安装基础服务
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
if
重启服务
service network restart
路由网址
traceroute car.blessjing.cn
安装 ifconfig
yum install net-tools
安装 wget
yum install wget
安装 vim
yum install vim
查看进程
ps -ef |grep ssh
第三步: 查看虚拟机版本
cat /etc/redhat-release
第四步:CentOS 镜像使用(替换默认源)
#备份/etc/yum.repos.d/CentOS-Base.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
cd /etc/yum.repos.d
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
#运行以下命令生成缓存
yum clean all
yum makecache
免密登录
生成key
ssh-keygen
将客户机公钥复制一份到目标机的
~/.ssh/authorized_keys
端口安全
修改 ssh 服务端口 vim /etc/ssh/sshd_config
service sshd restart
常用命令
软件包管理器:yum
- 安装软件:yum install xxx
- 卸载软件:yum remove xxx
- 搜索软件:yum serach xxx
- 清理缓存:yum clean packages
- 列出已安装:yum list
- 软件包信息 : yum info xxx
服务器硬件资源和磁盘操作
- 内存:free -m
- 硬盘:df -h
- 负载:w/top(阀值低于1)
- cpu : 个数和核数
cat /proc/cpuinfo
- fdisk:格式化磁盘
文件和文件夹操作命令
-
文a件目录结构
- 根目录 /
- 家目录 /home
- 临时目录 /tmp
- 配置目录 /etc
- 用户程序 /usr
-
文件基本操作命令
- mkdir -p imooc/test1(循环文件目录)
-
编辑器 vim
- gg 首行
- G 尾行
- dd 删除
- u 恢复
- yy 复制
- p 粘贴
-
文件权限 421
- r:4 w:2 x:0
-
文件搜索,查
- tail 从文件尾部开始读
- head 从文件头部读
- more 分页读取
- grep -n '' filename 搜索
- wc cat imooc | wc -l 统计行数
- find /etc/ -name "*.conf" 该目录下后缀为 .conf 的文件
-
文件压缩和解压
- tar -czvf imooc.tar.gz imooc.md 压缩
- tar -xzvf imooc.tar.gz 解压
- tar -tzvf imooc.tar.gz 查看
系统用户操作命令
- useradd 添加用户
- adduser 添加用户
- userdel 删除用户
a. userdel -r username 彻底删除用户 - passwd 设置密码
防火墙相关设置
-
只开放 80 22 43(https)
-
安装
-
检查安装
yum list |grep firewall
-
查看运行状态
ps -ef |grep firewall
-
安装
yum install firewalld
-
-
启动
service firewalld start
-
检查状态
service firewalld status
-
关闭或禁用防火墙
service firewalld stop/disable
-
相关操作
firewall-cmd --version
firewall-cmd --query-port=22/tcp
firewall-cmd --list-services
firewall-cmd --remove-service=ssh
firewall-cmd --add-service=ssh
提权操作 sudo 和文件传输操作
-
提权
$ visudo
%imooc ALL=(ALL) ALL
-
文件下载
-
下载到文件
curl -o baidu.html http://www.baidu.com
-
wget http://www.baidu.com
-
scp imooc@192.168.0.101:/tmp/imooc.text ./
-
-
文件上传
scp imooc.txt imooc@192.168.0.101:/tmp/
WebServer
Apache
-
安装
yum install httpd
-
启动
service httpd start
查询进程
ps -ef | grep httpd
-
停止
service nginx stop
Nginx
-
安装
-
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/...
-
yum install -y nginx
查询进程
ps -ef | grep nign
-
-
启动
service nginx start
-
停止
service nginx stop
-
重载 (用户无感)
service nginx reload
-
伪静态
-
日志格式化
百度一下:
nginx log_format
access_log /var/log/nginx/access_imooc.log imooc;
-
反向代理
-
负载均衡
-
调试技巧
8: MySQL
a. CentOS 默认安装了想先卸载 mariable 数据库
yum search mysql
yum remove mariadb-libs.x86_64
b. 下载 MySQL 源:
c. 安装源
yum localinstall mysql80-community-release-el7-1.noarch.rpm
d. 安装 MySQL
yum install mysql-community-server
e. 默认密码:
cat /var/log/mysqld.log | grep "password"
f.登录
mysql -h192.168.0.104 -uroot -password
g. 开启 Genelog 记录日志方便调试
# 设置general log保存路径
# 注意 在Linux中只能设置到 /tmp 或 /var 文件夹下,设置其他路径出错
# 需要root用户才有访问此文件的权限
mysql> set global general_log_file='/tmp/general.log';
# 开启general log模式
mysql> set global general_log=on;
# 关闭general log模式
mysql>set global general_log=off;
在general log模式开启过程中,所有对数据库的操作都将被记录 general.log 文件
h. 新建用户
CREATE USER 'imooc'@'%' IDENTIFIED BY '123456';
i. 权限操作
赋予权限
GRANT ALL PRIVILEGES ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
GRANT select,insert,update,delete ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
收回权限
REVOKE ALL PRIVILEGES ON *.* FROM imooc;
FLUSH PRIVILEGES;
i. 忘记 root 密码怎么办
在 /etc/my.cnf 加入 skip-grant-tables
use mysql;
update user set authentication_string=password('456789') where user='root';
j. 远程链接
1. 链接数据库
mysql -h192.168.0.104 -uroot -password
2. use mysql;
3.select Host,User from user \G;
4.update user set Host = '%' where Host = "localhost" and User = "root";
5. flush privileges;
6. 重启数据库
7.关闭防火墙
大文件导入用命令行比客户端效率高
source D:\hudong.sql
9: 缓存 Memcache 和 Redis
telnet
telnet 127.0.0.1 11211
-bash: telnet: command not found
yum install telnet.*
telnet 127.0.0.1 11211
set imooc 0 60 5
hello
get imooc
delete imooc
Memcache
安装:
yum install memcached
启动:
memcached -d -l -m -p
停止:
kill pid
Redis
安装:
1:获取源码
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
2:解压
tar -xzvf redis-4.0.2.tar.gz
3:安装
yum install gcc
make MALLOC=libc
sudo make install
启动: | 命令 | 简介 |
---|---|---|
redis-server | Redis服务器端启动程序 | |
redis-cli | Redis客户端操作工具。也可以用telnet根据其纯文本协议来操作 | |
redis-benchmark | Redis性能测试工具 | |
redis-check-aof | 数据修复工具 | |
redis-check-dump | 检查导出工具 |
cd src && ls
./ redis-server
停止:
redis-server stop
客户端
redis-client
10:Git
1 : 安装
sudo yum install git
2:生成SSH KEY
ssh-keygen
3:访问码云(https://gitee.com),配置SSH Key
4:基本命令
推荐学习Git基本命令网址:http://www.runoob.com/git/git-tutorial.html
5:命令自动补全
5.1. 下载源码 使用下载源码中的 git-completion.bash 自动补全命令的文件(如果无法下载请到慕课课程群中下载)
git clone git@github.com:git/git.git
5.2 复制 git-completion.bash 文件
cp contrib/completion/git-completion.bash /etc/bash_completion.d/
5.3 加载 bash 脚本
source /etc/bash_completion.d/git-completion.bash
5.4 自动加载脚本,编辑~/.bash_profile
# Git bash autoload
if [ -f /etc/bash_completion.d/git-completion.bash ]; then
source /etc/bash_completion.d/git-completion.bash
fi
PHP 环境搭建
基础运行环境搭建
1:默认版本太低(5.4) 升级php 到5.6
1.1.检查当前安装的PHP包
yum list installed | grep php
1.2如果有安装的PHP包,先删除他们
yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64
2 : 配置源
sudo rpm -Uvh http://mirror.webtatic.com/yum/el7/epel-release.rpm
sudo rpm -Uvh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
如果想删除上面安装的包,重新安装
rpm -qa | grep webstatic
rpm -e 上面搜索到的包即可
3:fpm 安装 和 基本操作
sudo yum install php56w-fpm( 也可以php55w-fpm php70w-fpm )
service php56w-fpm start/restart/stop
4:安装PHP扩展
sudo yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
Laravel 运行环境搭建
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name laravel.imooc.test;
root /home/www/mooc/vagrant/phpmvc/laravel5/public;
index index.php;
location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
try_files $uri =404;
}
}
PHPMyAdmin 配置安装
- 官网
www.phpmyadmin.net
- 下载
https://files.phpmyadmin.net/phpM yAdmin/4.8.3/phpMyAdmin-4.8.3-all-languages.zip - 解压
unzip phpMyAdmin-4.8.3-all-languages.zip - 简化文件名
mv phpMyAdmin-4.8.3-all-languages phpmyadmin
- 下载
PHPRedisAdmin 配置安装
14. 服务管理
设置软链接
sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Ntpdate 日期同步
Logrotate 日志切割
supervisor 进程管理
15. Zabbix 服务器监控
-
学习资料
https://www.cnblogs.com/clsn/p/7885990.html
-
安装源
-
安装服务
sudo yum install zabbix-server-mysql zabbix-web-mysql
-
create database zabbix;
-
安装客户端
sudo yum install zabbix-agent
-
导入表
cd /usr/share/doc/zabbix-server-mysql-3.2.11/
zcat create.sql.gz | mysql -uroot -p zabbix
sercer {
listen 80;
server_name www.blessjing.cn;
root /usr/share/zabbix;
index index.php index.html;
access_log /var/log/nginx/access_zabbix.log imooc;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: