オブジェクト指向とかデザインパターンとか開発プロセスとかツールとか

satoshi's ソフトウェア開発

js






当サイトはアフィリエイト広告を利用してます。

apache Laravel Ubuntu

Ubuntu+Apache2にLaravelのサイトをデプロイする方法

更新日:


Laravelで作成したWebアプリケーションを、Ubuntu+Apache2環境にデプロイする方法です。

ソースコードは git で管理されていることを前提にします。

まず、Ubuntuに一般ユーザーでログインして、Laravelプロジェクト用のフォルダに git から clone します。

$ pwd
/home/satoshis
$ mkdir laravel
$ cd laravel
$ git clone https://example.com/path/hoge.git

データベースを用意したり、composer update を実行したりなどして、Webアプリケーションが動くための環境を作ります。

apache2 のバーチャルホストを作ります。

$ sudo vi /etc/apache2/sites-available/hoge.conf

hoge.conf の内容はこんな感じでいいと思います。

<VirtualHost *:80>
	ServerName hoge.com
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/hoge/public/
	ErrorLog ${APACHE_LOG_DIR}/hoge-error.log
	CustomLog ${APACHE_LOG_DIR}/hoge-access.log combined
	RewriteEngine on
	RewriteCond %{SERVER_NAME} = hoge.com
	RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
	<Directory "/var/www/hoge/public/">
		Require all granted
		AllowOverride All
	</Directory>
</VirtualHost>

DocumentRoot を /var/www/hoge/public に設定してます。

一般ユーザーのフォルダにシンボリックリンクを作って、/var/www/hoge を参照すると Laravel のフォルダが見えるようにします。

$ sudo ln -s /home/satoshis/laravel/hoge /var/www/hoge

シンボリックリンクを作っただけでは、apache2 が動いているアカウントの www-data が読み込めないので、権限を与えます。

$ pwd
/home/satoshis/laravel/hoge
$ chmod -R go+r .
$ sudo chown www-data:www-data /var/www/hoge

確認しましょう。

$ ls -l /var/www/hoge
lrwxrwxrwx  1 www-data www-data   30 Jul  8 17:59 hoge -> /home/satoshis/laravel/hoge
$ ls -lH /var/www/hoge
total 328
drwxrwxrwx  6 satoshis satoshis   4096 Jun 20 17:04 app
-rwxrwxrwx  1 satoshis satoshis   1686 Jun 20 17:04 artisan
drwxrwxrwx  3 satoshis satoshis   4096 Jun 20 17:04 bootstrap
-rw-rw-rw-  1 satoshis satoshis   1546 Jun 20 18:31 composer.json
-rw-rw-rw-  1 satoshis satoshis 262734 Jun 20 18:31 composer.lock
drwxrwxrwx  2 satoshis satoshis   4096 Jun 20 17:30 config
drwxrwxrwx  5 satoshis satoshis   4096 Jun 20 17:04 database
-rw-rw-rw-  1 satoshis satoshis   1125 Jun 20 17:04 package.json
-rw-rw-rw-  1 satoshis satoshis   1138 Jun 20 17:04 phpunit.xml
drwxrwxrwx  6 satoshis satoshis   4096 Jun 20 17:04 public
-rw-rw-rw-  1 satoshis satoshis   4094 Jun 20 17:04 readme.md
drwxrwxrwx  6 satoshis satoshis   4096 Jun 20 17:04 resources
drwxrwxrwx  2 satoshis satoshis   4096 Jun 20 17:04 routes
-rw-rw-rw-  1 satoshis satoshis    563 Jun 20 17:04 server.php
drwxrwxrwx  5 satoshis satoshis   4096 Jun 20 17:04 storage
drwxrwxrwx  4 satoshis satoshis   4096 Jun 20 17:04 tests
-rw-rw-rw-  1 satoshis satoshis      0 Jun 20 17:04 text
drwxrwxrwx 43 satoshis satoshis   4096 Jun 20 17:34 vendor
-rw-rw-rw-  1 satoshis satoshis    537 Jun 20 17:04 webpack.mix.js

大丈夫っぽいですね。

バーチャルホストの設定を有効化して、apache の設定を読み込ませます。

$ sudo a2ensite hoge.conf
$ sudo systemctl reload apache2

これでアクセスできるようになります。

SSLを有効化します。

$ sudo certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: hoge.com
(略)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
(略)

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

(略)

 - 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

apache に SSL設定を読み込ませることで、https でアクセスできるようになります。

$ sudo systemctl reload apache2







-apache, Laravel, Ubuntu
-, , ,

Copyright© satoshi's ソフトウェア開発 , 2024 All Rights Reserved Powered by STINGER.