爬虫网页

#nohup scrapy runspider spider.py > urls.log &
import scrapy

class MySpider(scrapy.Spider):
    name = 'domain'
    allowed_domains = ['domain']
    start_urls = [
        'https://domain/'
    ]

    #avoid repeat
    tempList = []

    def parse(self, response):
        # for h3 in response.xpath('//h3').getall():
        #     yield {"title": h3}

        for link in response.xpath('//a/@href').getall():
            if link not in self.tempList:
                print(link)
                self.tempList.append(link)
                yield scrapy.Request(response.urljoin(link), self.parse)

 


php-fpm性能

1. 查看是否是硬件问题

方式:top  命令

主要查看:load average(平均负载),这是一个4核8G内存的服务器

1分钟平均负载:2.32;

5分钟平均负载:2.18;

15分钟平均负载:3.95;

load average  中3个数的含义,如果是1核cpu,那么不能超过1,4核那么就不能超过4,15分钟可以代表长期,5分钟代表中期,1分钟代表短期,所以先看15分钟

大神详解load average

可以说它现在的平均负载接近了它的cpu总核数:4;需要考虑服务器配置升级!


2.查看是否是PHP-fpm的进程数过多

首先使用 free -m 指令查看当前服务器执行状况:

可以看到我的内存消耗不多,也能看到我是2G内存

然后再用  top  命令 m 参数  查看内存情况

再使用:ps auxw|head -1;ps auxw|sort -rn -k4|head -40      查看消耗内存最多的前40个进程

  查看通过命令查看服务器上一共开了多少的 php-cgi 进程:ps -fe |grep "php-fpm"|grep "pool"|wc -l

  查看已经有多少个php-cgi进程用来处理tcp请求:netstat -anp|grep "php-fpm"|grep "tcp"|grep "pool"|wc -l

  设置PHP-FPM的进程数:vi /etc/php-fpm.d/www.conf(根据实际情况变化)找到 pm.max_children 字段,设置一个合理的值,比之前的小

  pm.max_spare_servers : 该值表示保证空闲进程数最大值,如果空闲进程大于此值,此进行清理 pm.min_spare_servers : 保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程;

这两个值均不能不能大于 pm.max_children 值,通常设置 pm.max_spare_servers 值为 pm.max_children 值的60%-80%。

正常情况下,一个php–fpm占用内存20~30M

3.进程跟踪

  1.使用  top   找出CPU最高的进程pid

2.strace -p PID(进程数)   来跟踪进程

3. ll /proc/PID/fd   来查看该进程在处理哪些文件

1 Comment more...

ssl证书到期时间查询

  1. 在服务端使用 Openssl 工具进行查看

    由于我服务端是搭建在 Centos 上,所以用 xshell 或者 putty 工具登录后,进入证书目录,使用 openssl 命令进行查看:

    # cd /usr/ssl/cert

    # openssl x509 -in signed.crt -noout -dates

    上面改成你自己证书的所在目录,证书名称也改成你自己服务端上证书的名称。

  2. 使用 php 代码方法进行查看

    如果你有多个可访问的域名,那么使用代码的方法进行查看就会容易很多,省得一个一个手动查看。下面贴上代码:

    /**
     * 获取证书有效期
     */
    public function getValidity(){
      $domain = "sslforfree.com";
      $context = stream_context_create(array("ssl" => array("capture_peer_cert_chain" => true)));
      $socket = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
      $context = stream_context_get_params($socket);
      foreach ($context["options"]["ssl"]["peer_certificate_chain"] as $value) {
        //使用openssl扩展解析证书,这里使用x509证书验证函数
        $cerInfo = openssl_x509_parse($value);
        if(strpos($cerInfo['name'],$domain)) {
          echo  "start:".date("Y-m-d",$cerInfo['validFrom_time_t'])."<br/>";
          echo "end:".date("Y-m-d",$cerInfo['validTo_time_t']);
        }
      }
    }

     


git还原代码

如果你的仓库是自己在用(不影响别人),那么你可以使用

git reset --hard <target_commit_id>

 

来恢复到指定的提交,再用

git push -f

 

来强制更新远程的分支指针。为了保证万一需要找回历史提交,我们可以先打一个 tag 来备份。

 

如果你的仓库是多人在协作,那么你这么操作会使用别人本地的代码库混乱,所以只能建一个新的提交,这个新的提交中把想取消的提交都 revert 掉,那么具体应该如何做呢?方法如下:

首先,和刚刚一样,用

git reset --hard 23801b2

将代码切换到目标提交的 id。接下来,用

git reset --soft origin/source 命令(source 分支)

,将当前代码切换回最新的提交。

执行完上面两步后,你的仓库还是最新的提交,但是工作区变成了历史的提交内容,这个时候用 git add 和 git commit 即可。

该方法需要保证 reset 的时候没有别人做新的提交,如果有的话,会一并把别人的提交也撤销了。所以还是挺危险的,慎用。


Magento deploy

1. php bin/magento deploy:mode:set production
2. rm -rf var/di/* && rm -rf var/generation/* && rm -rf var/cache/* && rm -rf var/page_cache/* && rm -rf var/view_preprocessed/* && rm -rf pub/static/* && rm -rf generated/* && php bin/magento setup:upgrade && php bin/magento setup:di:compile
3. php bin/magento setup:static-content:deploy fi_FI en_US sv_SE -f && php bin/magento indexer:reindex && php bin/magento maintenance:disable && php bin/magento cache:clean && php bin/magento cache:flush

 


Composer Memory No Limit

$ COMPOSER_MEMORY_LIMIT=-1 php composer.phar [command]

1 Comment more...

Iframe适应高度

<script type="text/javascript"> 
        function reinitIframe() {
          var iframe = document.getElementById("form-iframe");
          try {
            var bHeight = iframe.contentWindow.document.body.scrollHeight;
            var dHeight = 900;
            var height = Math.max(bHeight, dHeight);
            iframe.height = height;
          } catch (ex) {}
        }
        window.setInterval("reinitIframe()", 200);
</script>

 


overwrite render list

You have to override two core checkout xml file for override default.phtml file in magento 2,

app/code/XYZ/Checkout/view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock class="Magento\Checkout\Block\Cart" name="checkout.cart.form">
            <block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers.override" as="renderer.list.custom"/>
            <arguments>
                <argument name="renderer_list_name" xsi:type="string">checkout.cart.item.renderers.override</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

app/code/XYZ/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.item.renderers.override">

            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="XYZ_Checkout::cart/item/default.phtml" />              

            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="XYZ_Checkout::cart/item/default.phtml" />   

        </referenceBlock>
    </body>
</page>

 


magento翻译

1. In .ptml files

  • Use the function  __()

__(‘<your_string>’)

  • For example:
  • If your string contains a variable,you can use::
  • In this example, the ‘Hello %s’ string is added to the dictionary when the i18n tool is run.

2. In email template

If your theme contains custom email template, use the {{trans}} directive.

3. Strings added in UI components’ templates

  • A string is added in the scope of an HTML element:
  • A string is added with no binding to an HTML element:

4.Strings added in UI components configuration files

  • Use the translate attribute in .xml files:

5.Strings added in .js files

  • Define
  • Use the $.mage.__(”) function when adding a string:
  • If your string contains a variable:

The  5 steps I mention above is the shortest process for you to Translate a String by Code in Magento 2. With this guide, you can manage the String by Code in Magento 2 easily. Every store has a String by Code in Magento 2 with many attributes.


Percona Xtrabackup快速备份MySQL

1.1 了解备份方式

  • 热备份:读写不受影响(mysqldump–>innodb)
  • 温备份:仅可以执行读操作(mysqldump–>myisam)
  • 冷备份:离线备份,读写都不可用
  • 逻辑备份:将数据导出文本文件中(mysqldump)
  • 物理备份:将数据文件拷贝(xtrabackup、mysqlhotcopy)
  • 完整备份:备份所有数据
  • 增量备份:仅备份上次完整备份或增量备份以来变化的数据
  • 差异备份:仅备份上次完整备份以来变化的数据

(continue reading…)


Copyright © 1996-2010 Add Lives. All rights reserved.
iDream theme by Templates Next | Powered by WordPress