ローカル設定
hostsファイル編集
sudo vim /etc/hostsで下記を追加する
127.0.0.1 example_domain.local
127.0.0.1 admin.example_domain.local
dockerでapacheを使ってる場合
<VirtualHost *:80>
ServerName example_domain.local
DocumentRoot /var/www/html/public
<Directory /var/www/html/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName admin.example_domain.local
DocumentRoot /var/www/html/public
<Directory /var/www/html/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
dockerでnginxを使ってる場合
server {
listen 80;
server_name example_domain.local;
root /path/to/your/laravel/project/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name admin.example_domain.local;
root /path/to/your/laravel/project/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
laravelのルート設定
use Illuminate\Support\Facades\Route;
Route::domain('admin.example_domain.local')->group(function () {
Route::get('/', [AdminController::class, 'index']);
Route::resource('users', UserController::class);
// 管理者用の他のルート
});
Route::domain('example_domain.local')->group(function () {
Route::get('/', [MainController::class, 'index']);
// ユーザー用の他のルート
});
サーバー再起動
Nginxの場合
sudo nginx -s reload
VPSでの設定
DNSでサブドメインのAレコードを作成する
admin.example_domain A ipアドレス
サーバーコンフィグの設定(本ドメインの設定とほぼ一緒)
Nginxでの設定、server_nameを変更するぐらい
server_name admin.example_domain.com
また、ドメインごとにファイルを作成してもいいし、本ドメインと同じ設定ファイルでも問題ない。ファイルを新しくした場合は、設定ファイルを有効にすることを忘れずに
ln -s /etc/nginx/sites-available/example_config /etc/nginx/sites-enabled/
laravelのルート設定
Route::domain('admin.example_domain.com')->group(function () {
Route::get('/', [AdminController::class, 'index']);
Route::resource('users', UserController::class);
// 管理者用の他のルート
});
コメント