ラベル cron の投稿を表示しています。 すべての投稿を表示
ラベル cron の投稿を表示しています。 すべての投稿を表示

2018年4月19日木曜日

今さらながら、Let's EncryptでSSL対応 + 自動更新

どうも、俺です。

今日は、随分前からWeb界隈でもてはやされているLet's Encryptの導入と自動更新設定についてメモ。

環境はCentOS6.9。たぶんCentOS7系でも一緒だと思う..。
Apache 2.4。

(1) certbotをダウンロード

$ wget "https://dl.eff.org/certbot-auto"

// certbot-autoを適当なPATHへ持っていく
# mv certbot-auto /usr/local/bin

// certbot-autoに実行権限を付与
# chmod +x /usr/local/bin/certbot-auto

(2) SSL証明書を作成

※注意 :この時点で対象サーバーにドメインが当たっていないと以下のコマンドは失敗します。

# certbot-auto certonly --webroot -w /www/path/to/public -d example.com --email info@example.com

うまくいくと
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2018-07-18. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le
が表示されます。

(3) 証明書の確認

$ ls /etc/letsencrypt/live/example.com

README  cert.pem  chain.pem  fullchain.pem  privkey.pem


  • cert.pem ... 証明書
  • chain.pem ... 中間証明書
  • fullchain.pem ... 証明書と中間証明書とを一緒にした証明書
  • privkey.pem ... 秘密鍵

(4) Apache(2.4系)の設定

対象の.confファイル内に以下を記述。
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

※2.2系の場合は以下も記述。
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

(5) Apache再起動

# /usr/local/apache2/bin/apachectl restart

(6) Let's Encrypt 自動更新設定

Let's Encryptの証明書は3ヶ月で有効期限が切れます。
そのためcronで自動更新させましょう。
# vim /etc/crontab

30  5  *  *  1  root /usr/bin/certbot-auto renew -q --no-self-upgrade --post-hook "/usr/local/apache2/bin/apachectl restart"

↑毎週月曜AM5:30に更新コマンドを叩いています。
更新ができるようになる30日前になったら、証明書が更新されます。
-qオプション ... 出力なし。
--no-self-upgrade ... プログラムの新バージョンへの自動更新をしない。




以上でぇぇぇえぇぇす。

2017年2月9日木曜日

Lumenでcron処理

どうも、俺です。

Lumen(多分5.3.1)でcronプログラムを作って登録する方法について。
おそらくLaravelはもうちょっと楽に出来そうですが、基本は同じはず。


Laravelの公式HP参照

【手順1】app/Console/Commands/以下にphpファイルを作成


$ vim app/Console/Commands/MyScript.php

内容は以下のような感じ。

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyScript extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'my_script_signature';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // ここに処理を書く
        echo "HELLO!\n";
    }
}


【手順2】app/Console/Kernel.phpに登録


$ vim app/console/Kernel.php

内容は以下。

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        // ここに作成したコマンドクラスを追記していく。
        \App\Console\Commands\MyScript::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // ここに実行させたいコマンドクラスのシグネチャを記す。
        $schedule->command('my_script_signature')->cron('* * * * * *');
    }
}

【手順3】動作を試してみる


$ php artisan my_script_signature
HELLO!

問題なし!

【手順4】cronに登録する


* * * * * php /path/to/artisan schedule:run 1>> /path/to/cron.log 2>&1

これで定期的に処理をさせることが出来ます。



以上でぇぇぇぇす!