LoginSignup
2
2

More than 1 year has passed since last update.

Ubuntu + lighttpd + Let's Encrypt 構成で laravel を動かす

Last updated at Posted at 2022-01-09

以下の構成での構築記事がなかったので作成しました。

構成

  • Ubuntu 20.04
  • Lighttpd 1.4.55
  • php 8.0
  • laravel 8.x

Ubuntu OS のインストールは済んでいるとします。

OS 最新化

$ sudo apt update
$ sudo apt upgrade

タイムゾーン設定

$ date
$ sudo timedatectl set-timezone Asia/Tokyo
$ date

lighttpd のインストール

$ sudo apt install lighttpd
$ lighttpd -v
$ sudo chown -R www-data:www-data /var/www/html/
$ sudo chown -R 755 /var/www/html/
$ sudo vi /var/www/html/index.html
---
example.com
---

ブラウザでアクセスする
 ⇒ http://example.com/

起動・停止

$ sudo systemctl stop lighttpd
$ sudo systemctl start lighttpd
$ sudo systemctl restart lighttpd

自動起動設定

$ sudo systemctl enable lighttpd

php 8.0 のインストール

$ sudo apt show php8.0 -a
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt show php8.0 -a
$ sudo apt-get update
$ sudo apt install php8.0 php8.0-fpm php8.0-sqlite3 php8.0-cli php8.0-curl php8.0-xml
$ php -v

php-fpm 設定

$ sudo vi /etc/php/8.0/fpm/pool.d/www.conf
---
;listen = /run/php/php8.0-fpm.sock
listen = 127.0.0.1:9000
---
$ sudo vi /etc/lighttpd/conf-available/15-fastcgi-php.conf
---
# -*- depends: fastcgi -*-
# /usr/share/doc/lighttpd/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi

## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server += ( ".php" =>
        ((
                "host" => "127.0.0.1",
                "port" => "9000",
                "max-procs" => 1,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "4",
                        "PHP_FCGI_MAX_REQUESTS" => "10000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)
---

モジュールの有効化

$ sudo lighty-enable-mod fastcgi
$ sudo lighty-enable-mod fastcgi-php

再起動

$ sudo systemctl restart lighttpd php8.0-fpm

phpファイルの動作確認

$ sudo vim /var/www/html/test.php
---
<?php
phpinfo();
---

ブラウザでアクセスする
 ⇒http://example.com/test.php

composer のインストール

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
$ sudo mv composer.phar /usr/local/bin/composer
$ composer -V
Composer version 2.2.3 2021-12-31 12:18:53

SSL設定 (Let's Encrypt)

lighttpdは停止しておく(でないとエラーになる)

$ sudo systemctl stop lighttpd
$ systemctl status lighttpd

apache2をインストールすることでssl_modをインストールする

$ sudo apt install apache2

SSL有効化

$ sudo a2enmod ssl

certbot コマンドのインストール

$ sudo apt install certbot

コマンド実行

$ sudo certbot certonly --standalone -d example.com

ここで管理者メールアドレスを設定する必要あり

$ sudo ls -la /etc/letsencrypt/live/example.com

 ⇒ cert.pem や fullchain.pem などが生成されている

pemファイルをrootユーザに一度なって結合する

$ sudo su -
# cat /etc/letsencrypt/live/example.com/{privkey.pem,fullchain.pem} > /etc/letsencrypt/live/example.com/combined.pem
# cat /etc/letsencrypt/live/example.com/combined.pem
# exit

設定ファイルの下部に追記

$ sudo vim /etc/lighttpd/lighttpd.conf
---
# http => https Redirect Setting
$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "example.com" {
        url.redirect = ( "^/(.*)" => "https://example.com/$1" )
        server.name = "example.com"
    }
}

# SSL Setting
$SERVER["socket"] == ":443" {
    ssl.engine = "enable" 
    ssl.pemfile = "/etc/letsencrypt/live/example.com/combined.pem"
    ssl.ca-file = "/etc/letsencrypt/live/example.com/fullchain.pem"
}
---

起動

$ sudo systemctl start lighttpd

ブラウザでアクセスする
 ⇒ https://example.com/

laravelの導入

$ sudo apt install unzip
$ cd ~/
$ composer create-project laravel/laravel example-app
$ sudo mv example-app /var/www
$ chmod -R 777 /var/www/example-app/storage

webrootを割り当てる

$ sudo vim /etc/lighttpd/lighttpd.conf
---
-server.document-root        = "/var/www/html"
+server.document-root        = "/var/www/example-app/public"
---

再起動

$ sudo systemctl restart lighttpd

ブラウザでアクセスする
 ⇒ https://example.com/

以上です。

なお、mod_rewrite の設定が必要っぼい。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2