一个 PHPer 的 Linux 入门小册子

::Linux 从入门到精通

第一步:安装软件

Virtualbox(V5.1.8) 下载地址:

https://www.virtualbox.org/wiki/Download_O...

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 镜像使用(替换默认源)

http://mirrors.163.com/.help/centos.html


#备份/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件目录结构

    1. 根目录 /
    2. 家目录 /home
    3. 临时目录 /tmp
    4. 配置目录 /etc
    5. 用户程序 /usr
  • 文件基本操作命令

    1. mkdir -p imooc/test1(循环文件目录)
  • 编辑器 vim

    1. gg 首行
    2. G 尾行
    3. dd 删除
    4. u 恢复
    5. yy 复制
    6. p 粘贴
  • 文件权限 421

    1. r:4 w:2 x:0
  • 文件搜索,查

    1. tail 从文件尾部开始读
    2. head 从文件头部读
    3. more 分页读取
    4. grep -n '' filename 搜索
    5. wc cat imooc | wc -l 统计行数
    6. find /etc/ -name "*.conf" 该目录下后缀为 .conf 的文件
  • 文件压缩和解压

    1. tar -czvf imooc.tar.gz imooc.md 压缩
    2. tar -xzvf imooc.tar.gz 解压
    3. tar -tzvf imooc.tar.gz 查看

      系统用户操作命令

    4. useradd 添加用户
    5. adduser 添加用户
    6. userdel 删除用户
      a. userdel -r username 彻底删除用户
    7. passwd 设置密码

      防火墙相关设置

  • 只开放 80 22 43(https)

  • 安装

    1. 检查安装

      yum list |grep firewall

    2. 查看运行状态

      ps -ef |grep firewall

    3. 安装

      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

  • 文件下载

    1. 下载到文件

      curl -o baidu.html http://www.baidu.com

    2. wget http://www.baidu.com

    3. 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

  • 安装

    1. sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/...

    2. 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 源:

wget https://dev.mysql.com/get/mysql80-communit...

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

  • 安装源

    rpm -ivh https://repo.zabbix.com/zabbix/3.2/rhel/7/...

  • 安装服务

    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 协议》,转载必须注明作者和本文链接
相信时间和变化
本帖由系统于 5年前 自动加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 5

curl cht.sh/curl
curl cht.sh/php/array_values

5年前 评论

为什么安装完文件为空 cat /var/log/mysqld.log | grep "password" 更新密码也出错了?

mysql> update user set authentication_string=password('root') where user='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('root') where user='root'' at line 1
5年前 评论

@lovecn 你的 mysql 版本好像不支持这条命令,确认下你的版本再 Google 对应的命令。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!