Write the Code. Change the World.

分类目录
9月 12

这里的关键点:
1. redis 异步队列。
2. 多项目。

面对上边这种情况,怎么处理呢。如果不处理又会出现什么现象呢。

因为我们使用 supervisord 进行常驻队列侦听。如果不指定对应的 queue,多项目就会出现串的情况。这个是就可以用 queue 来处理该问题了。

处理

  1. 修改 supervisord 的配置文件。在 command 中指定一个 queue 。如
command=/alidata/service/php/bin/php /alidata/www/www.mlxiu.com/artisan queue:work redis --queue=test --sleep=3 --tries=3
  1. 在项目中,指定 queue。因为使用的是 laravel 框架。 队列继承 ShouldQueue。 可以在构造函数中指定 queue 即可。
public function __construct($id)
{
     $queueName = config('queue.name');
     if ($queueName) {
         $this->onQueue($queueName);
     }
}

queue.name 是自己额外在 queue.config 中添加的变量。

参考

https://www.cnblogs.com/zgxblog/p/10996112.html

8月 05

有时候,会去查看系统的磁盘空间。这个时候,一些命令就用的着了。

查看磁盘空间

# df 以磁盘分区为单位查看文件系统,可以获取硬盘被占用了多少空间,目前还剩下多少空间等信息。
# `-h` 选项为根据大小适当显示
df -h

显示内容参数说明:

  • Filesystem:文件系统
  • Size: 分区大小
  • Used: 已使用容量
  • Avail: 还可以使用的容量
  • Use%: 已用百分比
  • Mounted on: 挂载点 

查看某个文件夹或文件的大小

# 查看当前文件夹的总大小 `h` 以适当大小显示(比如 M,G 单位)
du -sh

# 查看对应深度的文件大小
du -h --max-depth=1

# 查看某个文件大小
du -h xxx.txt

排序功能就不说了,太多记不住。实用即可。

7月 20

既然是再来一次定时任务,那我们就要有明确的目标以及观察点。来列列。

列列观察点

  1. 如何开启定时任务,开启多大频率或定时的任务,几种方式开启。

  2. 定时任务日志功能。(这样才能更好的找到问题的所在)

  3. 定时任务的执行用户。比如 root 用户,非 root 用户。这之间有什么影响和不一样。

  4. 额外, no login 用户的 shell 执行。

开启定时任务

方法 1:

# 编辑,并添加定时任务
vim /etc/crontab 

方法 2:

# 给当前用户添加定时任务。如果是 root 用户登录的,就是给 root 用户添加定时任务(就是执行用户是 root)
crontab -e 

# 给 nginx 用户添加定时任务(编写命令即可)
crontab -e -u nginx

给 root 用户添加定时任务的缺点是 root 用户产生的文件,其他用户默认是没权限去操作的。比如写了日志,其他用户想继续打开该日志来编写,是没办法操作的。除非改权限设置等操作。

方法 3:

vim cron.txt

# 将定时任务命令写在 cron.txt 里边,然后再执行
crontab cron.txt -u nginx

方法 4:

以上的方法,只是命令在其他 sh 文件里。

查看,重启,关闭

查看关闭:

# 查看当前用户执行的定时任务
crontab -l

# 查看 nginx 用户执行的定时任务
crontab -l -u nginx

# 删除当前用户执行的定时任务
crontab -r 

# 删除 nginx 用户执行的定时任务

crontab -r -u nginx

重启:

//启动服务 
service crond start 

//关闭服务 
service crond stop 

//重启服务 
service crond restart 

日志

# 这里可以看到日志的
tail -f /var/log/cron

如果日志文件不存在(被删除了或啥的,请使用下面命令从新生起)

# 重启rsyslog服务:
service rsyslog restart 

# 重启 cron
service cron restart

报错

如果使用 nginx 这种 nologin 用户执行定时任务会报错的。如下:

(CRON) ERROR chdir failed (/home/nginx): No such file or directory

这个时候,只需要在 home 目录下,创建个 nginx 文件夹就 ok 了。

参考

https://www.jianshu.com/p/5d6a0d729ef7

7月 06

git 存在这样一个场景。曾经的代码用的 https 协议进行上传下载。这个时候,总是要输入用户名和密码。这样好烦好烦的。突然有一天,你想用 ssh 协议了(不用再输密码),这个时候就要切换协议了。下边就说说怎么操作。

没有公私秘钥,要生

# 生,一路回车就好
ssh-keygen -t rsa -C "abcd@mlxiu.com"

cat ~/.ssh/id_rsa.pub

生了后,先要将公钥内容复制出来,设置到你所托管的 ssh 设置相应的地方。比如托管的 gitlab 或 github,都有对应的地方设置。设置好了后,就可以切协议了。

查看切换协议

# 查看
git remote -v

# 切换
git remote set-url origin git@gitlab.xxxxxxx

然后你


git pull origin master git push origin master

就不用再输入账号密码了。

7月 02

linux 下 tree 很好用的。方便查看文件夹以及子文件夹N个子子文件夹里的文件,并很好的展示出来。如下图:

下边来说说怎么使用和操作。

先安装

## centos 
yum install tree

## ubuntu
apt-get install tree

没权限,请加 sudo

再使用

## 帮助
tree --help

## 显示某个文件夹下的(默认当前文件夹)
tree /usr/local 
tree app

## -d 参数,只显示文件夹,不显示文件夹里的内容。如上图

tree -d

## -L n (n表示级数,是 int 类型,且必须)
tree -L 2 # 显示当前目录以及目录下的文件

不仅能展示,还能将展示的结构写到文本里,如:

sudo tree app -d -L 3 > /usr/local/src/tree.txt

# 查看保存的结果
cat /usr/local/src/tree.txt

就这样了。用到这几个就可以了。更多的功能待去揣摩。

7月 01

通常情况下,使用构建的命令就可以关闭对应的服务。可有时候 pid 被删了后,程序逻辑就关闭不了服务了。这个时候,就得用命令来关。

通常方式

laravel 中 swoole 为例

# 停止
php artisan swoole:action stop

# 启动
php artisan swoole:action start

当 pid 文件删除或被改后,就 stop 不了了。

命令方式

# 先查看端口对应的进程 id
netstat -tunlp|grep 9400

### 结果 tcp        0      0 0.0.0.0:9400            0.0.0.0:*               LISTEN      2750/php

# 再 kill 进程 id 就完事了
kill -9 2750

# 查看进程信息 (当然在 kill 之前查看)
ps -ef|grep 2750

参考

https://www.cnblogs.com/moy25/p/8668432.html

6月 11

vim 很多很好的命令,本该记住的。曾经用的很好的命令也本该记住的。比如删除整个文件的内容,比如跳到某一行,跳到行尾,跳到文档尾等等。记忆力不好呀,还是先放在这里暂存下吧。

基础

:wq  # 保存并退出

:q! # 直接退出不保存

:e! # 放弃所有修改,并重新载入该文件

gg # 跳到文档第一行

G # 跳到文档最后一行

28G # 跳转到 28 行

ggdG # 删除整个文档内容

ggyG # 复制整个文档内容

dd # 删除当前一行

:set number # 显示行号(临时的)

: set nonumber # 不显示行号

:set ruler  # 会在屏幕右下角显示当前光标所处位置,并随光移动而改变,占用屏幕空间较小,使用也比较方便,推荐使用。

/字符串 # 搜索字符串

操作-选择文本,删除,复制

v   # 从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束。

V   # 从光标当前行开始,光标经过的行都会被选中,再按一下V结束。

ggVG # 选中整个文档

d # 删除

y # 复制
5yy # 复制 5 行
9yy # 复制 9 行

p # 粘贴

x # 删除选中光标的字符

J # 删除换行符(就是将两行合并)

u # 撤销操作

k # 光标向上移动一行
9k # 光标向上移动 9 行
3k # 光标向上移动 3 行

操作复制

:num1, num2, copy num3 # 将第 num1 行到 num2 行的内容复制到 num3 行后边。

:num1, num2, move num3 # 将第 num1 行到 num2 行的内容移动到 num3 行后边。

ndd # 删除从光标开始的 n 行。 比如 n 如果是 5 ,就是删除从光标开始的 5 行。

操作搜索

/string # 搜索一个字符串。

# ? 命令与 / 的工作相同,只是搜索方向相反
# 如果查找内容忽略大小写,则用命令"set ignorecase", 返回精确匹配用命令"set noignorecase"

:set hlsearch # 高亮显示搜索结果

:set nohlsearch # 去掉高亮显示,可简写 :set noh

参考

https://www.cnblogs.com/yangjig/p/6014198.html

5月 20

centos7安装supervisor

方式一: yum 安装

yum install epel-release

yum install supervisor

systemctl enable supervisord # 开机自启动

systemctl start supervisord # 启动supervisord服务

systemctl status supervisord # 查看supervisord服务状态

ps -ef|grep supervisord # 查看是否存在supervisord进程

方式二: apt-get 安装

apt-get install supervisor

操作

systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord

使用之前

cat /etc/supervisord.conf

在最底部,会看到:

files = supervisord.d/*.ini

这个会调用 supervisord.d 目录下的配置文件。所以我们的配置文件都建在 supervisord.d 下。

使用

vim mlxiu-queue.ini

[program:mlxiu]
process_name=%(program_name)s_%(process_num)02d
command=/alidata/service/php/bin/php /alidata/www/www.mlxiu.com/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=nginx
numprocs=8
redirect_stderr=true
stdout_logfile=/alidata/logs/supervisord/www.yueqiubao.net.txt

stdout_logfile 文件一定要先创建好哈。

参考

https://learnku.com/articles/28919

https://blog.csdn.net/donggege214/article/details/80264811

https://blog.csdn.net/kkevinyang/article/details/80539940?utm_source=blogxgwz3

5月 08

官方文档很美好,执行 * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 就可以启动每分钟调用一次的调度了。

但是,你会发现该调度只执行一次。

然后你使用 crontab -l 发现空空如也。

这个时候怎么办呢。

操作一

小伙伴也有方法。说 php 路径,文件路径没写成绝对路径,需要有这样的操作。

# 找到 php 的位置
whereis php

# pwd 找到项目的位置

# 然后使用绝对路径执行
* * * * * /usr/bin/php7.3 /home/vagrant/code/work/zeipan.com/artisan schedule:run >> /dev/null 2>&1

但是,依然不行。当然也有人行了。

https://learnku.com/laravel/t/5032/ubuntu1604crontab-timed-tasks-are-not-held

操作二

环境变量等问题

echo $PATH;

# 或
env > /tmp/env.output
cat /tmp/env.output 
# 找到其中的 PATH 的值。

这样操作,感觉不相符。小伙伴也说了,使用了绝对路径可以不用管环境变量的。
https://learnku.com/articles/18697

个人测试有效的方法

依然按照操作一操作。只是,需要先生成一个 cron.txt 文件。在这个文件中,放入之前的命令。然后执行 crontab cron.txt 即可。

vim cron.txt

# 添加内容
* * * * * /usr/bin/php7.3 /home/vagrant/code/work/zeipan.com/artisan schedule:run >> /dev/null 2>&1

# :wq 保存退出

crontab cron.txt

# 查看任务
crontab -l

# 停止所有定时任务
crontab -r 

这样就美美解决了。

https://www.cnblogs.com/lamp01/p/6864258.html

4月 21

php官网:https://php.net/downloads.php

Nginx官网提供了三个类型的版本。

  1. Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
  2. Stable version:最新稳定版,生产环境上建议使用的版本
  3. Legacy versions:遗留的老版本的稳定版

打开下载页面,选择适合的版本(当前最新版本是7.3.4),进入镜像页面。找到对应的镜像地址。进入存储目录,开始下载。

cd /usr/local/src

http://cn2.php.net/distributions/php-7.3.4.tar.gz

tar -xzvf php-7.3.4.tar.gz

先下着。

准备编译环境

yum install curl-devel

yum install  libxml2-devel

yum install  libjpeg-devel

yum install  libpng-devel

yum install freetype-devel

yum install libxslt-devel

libzip 一定要编译安装,否则版本低,安装 php7.3.4 过不了。不过之前安装 nginx 的时候,已经安装过了。没啥问题了。

安装

cd /usr/local/src/php-7.3.4

./configure --prefix=/alidata/service/php \
--with-config-file-path=/alidata/service/php/etc \
--with-config-file-scan-dir=/alidata/service/php/etc/php.d \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--enable-fpm

如果提示:configure: error: Cannot find OpenSSL's libraries, 那是因为没有找到 libssl.so, 我们需要处理下。

find / -name libssl.so

# 输出
# /usr/local/openssl/lib/libssl.so
# 我们建立一个软链接,再配置 php
ln -s /usr/local/openssl/lib/libssl.so /usr/lib

如果还报这个错,估计少了 openssl-devel,用 yum 安装下。

yum install openssl-devel

如果是其他错误,缺少模块,可以用 yum 安装。

直到提示说 Thank you for using PHP,然后。

make && make install

安装完成之后,可以通过命令 make test 检查一下。

检查发现有好几个 bug。

Number of tests : 16250             12978
Tests skipped   : 3272 ( 20.1%) --------
Tests warned    :    0 (  0.0%) (  0.0%)
Tests failed    :    4 (  0.0%) (  0.0%)
Expected fail   :   36 (  0.2%) (  0.3%)
Tests passed    : 12938 ( 79.6%) ( 99.7%)
---------------------------------------------------------------------
Time taken      :  777 seconds
=====================================================================

=====================================================================
EXPECTED FAILED TEST SUMMARY
---------------------------------------------------------------------
Test open_basedir configuration [tests/security/open_basedir_linkinfo.phpt]  XFAIL REASON: BUG: open_basedir cannot delete symlink to prohibited file. See also
bugs 48111 and 52176.
Inconsistencies when accessing protected members [Zend/tests/access_modifiers_008.phpt]  XFAIL REASON: Discussion: http://marc.info/?l=php-internals&m=120221184420957&w=2
Inconsistencies when accessing protected members - 2 [Zend/tests/access_modifiers_009.phpt]  XFAIL REASON: Discussion: http://marc.info/?l=php-internals&m=120221184420957&w=2
Bug #48770 (call_user_func_array() fails to call parent from inheriting class) [Zend/tests/bug48770.phpt]  XFAIL REASON: See Bug #48770
Bug #48770 (call_user_func_array() fails to call parent from inheriting class) [Zend/tests/bug48770_2.phpt]  XFAIL REASON: See Bug #48770
Bug #48770 (call_user_func_array() fails to call parent from inheriting class) [Zend/tests/bug48770_3.phpt]  XFAIL REASON: See Bug #48770
Initial value of static var in method depends on the include time of the class definition [Zend/tests/method_static_var.phpt]  XFAIL REASON: Maybe not a bug
DateTime::add() -- fall type2 type3 [ext/date/tests/DateTime_add-fall-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::add() -- fall type3 type2 [ext/date/tests/DateTime_add-fall-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::add() -- fall type3 type3 [ext/date/tests/DateTime_add-fall-type3-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::add() -- spring type2 type3 [ext/date/tests/DateTime_add-spring-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::add() -- spring type3 type2 [ext/date/tests/DateTime_add-spring-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::add() -- spring type3 type3 [ext/date/tests/DateTime_add-spring-type3-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- fall type2 type3 [ext/date/tests/DateTime_diff-fall-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- fall type3 type2 [ext/date/tests/DateTime_diff-fall-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- fall type3 type3 [ext/date/tests/DateTime_diff-fall-type3-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- spring type2 type3 [ext/date/tests/DateTime_diff-spring-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- spring type3 type2 [ext/date/tests/DateTime_diff-spring-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::diff() -- spring type3 type3 [ext/date/tests/DateTime_diff-spring-type3-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- fall type2 type3 [ext/date/tests/DateTime_sub-fall-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- fall type3 type2 [ext/date/tests/DateTime_sub-fall-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- fall type3 type3 [ext/date/tests/DateTime_sub-fall-type3-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- spring type2 type3 [ext/date/tests/DateTime_sub-spring-type2-type3.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- spring type3 type2 [ext/date/tests/DateTime_sub-spring-type3-type2.phpt]  XFAIL REASON: Various bugs exist
DateTime::sub() -- spring type3 type3 [ext/date/tests/DateTime_sub-spring-type3-type3.phpt]  XFAIL REASON: Various bugs exist
Bug #52480 (Incorrect difference using DateInterval) [ext/date/tests/bug52480.phpt]  XFAIL REASON: See https://bugs.php.net/bug.php?id=52480
RFC: DateTime and Daylight Saving Time Transitions (zone type 3, bd2) [ext/date/tests/rfc-datetime_and_daylight_saving_time-type3-bd2.phpt]  XFAIL REASON: Still not quite right
RFC: DateTime and Daylight Saving Time Transitions (zone type 3, fs) [ext/date/tests/rfc-datetime_and_daylight_saving_time-type3-fs.phpt]  XFAIL REASON: Still not quite right
Bug #42718 (unsafe_raw filter not applied when configured as default filter) [ext/filter/tests/bug42718.phpt]  XFAIL REASON: FILTER_UNSAFE_RAW not applied when configured as default filter, even with flags
Bug #67296 (filter_input doesn't validate variables) [ext/filter/tests/bug49184.phpt]  XFAIL REASON: See Bug #49184
Bug #67167: filter_var(null,FILTER_VALIDATE_BOOLEAN,FILTER_NULL_ON_FAILURE) returns null [ext/filter/tests/bug67167.02.phpt]  XFAIL REASON: Requires php_zval_filter to not use convert_to_string for all filters.
via [ext/pdo_sqlite/tests/common.phpt]
    SQLite PDO Common: PDOStatement::getColumnMeta [ext/pdo_sqlite/tests/pdo_022.phpt]  XFAIL REASON: This feature is not yet finalized, no test makes sense
Phar: bug #69958: Segfault in Phar::convertToData on invalid file [ext/phar/tests/bug69958.phpt]  XFAIL REASON: Still has memory leaks, see https://bugs.php.net/bug.php?id=70005
updateTimestamp never called when session data is empty [ext/session/tests/bug71162.phpt]  XFAIL REASON: Current session module is designed to write empty session always. In addition, current session module only supports SessionHandlerInterface only from PHP 7.0.
Bug #73529 session_decode() silently fails on wrong input [ext/session/tests/bug73529.phpt]  XFAIL REASON: session_decode() does not return proper status.
Bug #70219 Use after free vulnerability in session deserializer [ext/standard/tests/serialize/bug70219.phpt]  XFAIL REASON: Unfinished merge, needs fix.
=====================================================================

=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
Bug #64267 (CURLOPT_INFILE doesn't allow reset) [ext/curl/tests/bug64267.phpt]
Bug #71523 (Copied handle with new option CURLOPT_HTTPHEADER crashes while curl_multi_exec) [ext/curl/tests/bug71523.phpt]
stream context tcp_nodelay fopen [ext/standard/tests/streams/stream_context_tcp_nodelay_fopen.phpt]
ZipArchive::setEncryption*() functions [ext/zip/tests/oo_encryption.phpt]
=====================================================================

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it.  You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: 

还得修复。

配置环境变量。

在终端中,任意位置都能使用的命令,需要配置环境变量。就如在win下一样。liunx下,只要编辑 /etc/profile 文件并 source 就可以。

vi /etc/profile

在末尾追加

PATH=$PATH:/alidata/service/php/bin
export PATH

保存,然后 source

source /etc/profile
echo $PATH  #看到配置的环境变量了
php -v #查看php的版本信息

刚安装完成,你并不会发现 php-fpm.conf,www.conf 以及 php.ini 文件。这几个重要文件,其实都有,前边两个文件只是将名字后边加了 .default 。第三个文件在源码包的根目录里,而且有两个,一个是 php.ini-development ,另一个是php.ini-production。既然找到了对应文件的位置,只需要复制一份过来就可以了。注意复制过来的路径(-with-config-file-path对应)和名字。

填充文件。

cp /alidata/service/php/etc/php-fpm.conf.default  /alidata/service/php/etc/php-fpm.conf
cp /alidata/service/php/etc/php-fpm.d/www.conf.default /alidata/service/php/etc/php-fpm.d/www.conf

cp /usr/local/src/php-7.3.4/php.ini-production  /alidata/service/php/etc/php.ini

cp /usr/local/src/php-7.3.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

chkconfig --add php-fpm  #开机自启动

开启可以这样

#启动服务
service php-fpm start 
#停止服务
service php-fpm stop  
#重启服务
service php-fpm reload

到此,我们熟悉的 /etc/init.d/php-fpm start回来了。

/etc/init.d/php-fpm start   #开启
/etc/init.d/php-fpm stop  #关闭
/etc/init.d/php-fpm restart  #重启
php -i|grep php.ini