EC2にLaravel6の実行環境を作ってという依頼があったので、その時の作業のメモ書きを残します。 節約のためか、RDSではなくEC2内にmysql立ててます。 抜けがあったり間違っていたりしたらすいません。 OSはcentosになります。
実行コマンド
## initialize
$ sudo yum update -y && sudo yum upgrade -y
$ sudo yum install -y git
$ sudo vi /etc/sysconfig/clock
ZONE="Japan"
UTC=true
$ sudo ln -sf /usr/share/zoneinfo/Japan /etc/localtime
## nginx
$ sudo amazon-linux-extras install nginx1.12
$ sudo vi /etc/nginx/nginx.conf
$ sudo service nginx start
$ sudo systemctl enable nginx
## php
$ sudo amazon-linux-extras install php7.3
$ sudo yum install --enablerepo=remi,remi-php73 php php-devel php-mbstring php-pdo php-gd php-dom php-xml php-fpm
$ php --ini
$ sudo vi /etc/php-fpm.d/www.conf
$ sudo touch /run/php-fpm/www.sock
$ sudo chown ec2-user:ec2-user /run/php-fpm/www.sock
$ sudo service php-fpm start
$ sudo systemctl enable php-fpm.service
## composer
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/bin/composer## mysql
$ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm -y
$ sudo yum-config-manager --disable mysql80-community
$ sudo yum-config-manager --enable mysql57-community
$ sudo yum install mysql-community-server -y
$ mysql_secure_installation
$ mysql -u root -p
$ sudo systemctl start mysqld.service
$ sudo systemctl enable mysqld.service
## project
$ sudo mkdir -p /var/www/${yourproject}/shared/
$ vi /var/www/${yourproject}/shared/.env
$ sudo chown -R ec2-user:ec2-user /var/www
## (デプロイしたあともしかしたら必要かも)
$ php artisan key:generate
$ composer dump-autoload && php artisan clear-compiled && php artisan cache:clear && php artisan route:clear && php artisan config:cache
$ cd release
$ chmod -R 777 storage/ && chmod -R 777 bootstrap/
nginx.conf
nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
# user nginx;
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/${your project root}/current/public;
index index.php;
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
php-fpm.conf
user = nginx
group = nginx
listen.owner = ec2-user
listen.group = ec2-user
listen.mode = 0660
.env
APP_ENV=production
deployer.php
deployer.php
namespace Deployer;
require 'recipe/laravel.php';
// Project name
set('application', '${your project name}');
// Project repository
set('repository', '${your git url}');
// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);
// Shared files/dirs between deploys
add('shared_files', ['.env']);
add('shared_dirs', [
'storage',
]);
// ssh type
set('ssh_type', 'native');
set('http_user', 'ec2-user');
set('keep_releases', 5);
// Writable dirs by web server
add('writable_dirs', ['bootstrap/cache', 'storage']);
set('allow_anonymous_stats', false);
// Hosts
host('${your deploy target ip address}')
->stage('production')
->user('ec2-user')
->port(22)
->identityFile('${aws ssh key path}')
->set('deploy_path', '/var/www/${your project directory name}')
->forwardAgent();
// Tasks
task('build', function () {
run('cd {{release_path}} && build');
});
// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
// Migrate database before symlink new release.
before('deploy:symlink', 'artisan:migrate');