Hello guys, I am Nam and this is my personal blog about my work, my life. Outside of work, I do something about UI, UX, DevOps, mobile...
Hello guys, today i'll show you how to deploy Laravel project to Amazon EC2
ssh -i "<examplekey.pem>" <username>@<EC2 Address>
Clone from github repository
git clone <link github repository>
(Optional) Copy source from local using scp
Install zip, unzip from local machine
sudo apt install zip
Compress source files using zip command:
zip -r <zipfilename.zip> <folderpath>
Copy source from local to EC2 instance using scp command:
scp -i "<examplekey.pem>" <zipfilename.zip> <username>@<EC2 Address>:<pathtosave>
Extract source files using unzip command:
unzip <zipfilename.zip>
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.3
sudo apt install mysql-client-core-8.0
Connect to RDS instance from EC2 via mysql client by following command and type your password
mysql -h <RDS Address Instance> -P <port | 3306> -u <masteruser> -p
CREATE DATABASE ;
EXIT;
...
APP_URL=<URL of your EC2 instance>
APP_DEBUG=false
DB_CONNECTION=mysql
DB_HOST=<RDS Address link>
DB_PORT=3306
DB_DATABASE=<databasename>
DB_USERNAME=<master username>
DB_PASSWORD=<password>
...
sudo apt-get install -y composer
composer update
composer install
php artisan key:generate
cp <foldername> /var/www/
sudo chown -R www-data:www-data /var/www/<foldername>
sudo chmod -R 755 <foldername>
sudo vi /etc/nginx/sites-available/<foldername>
server {
listen 80;
server_name ;
root /var/www//public;
// Focus this log if you see error
access_log /var/log/nginx/laravel-access.log;
error_log /var/log/nginx/laravel-error.log;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
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:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/<foldername> /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo a2enmod proxy_fcgi setenvif
sudo service nginx restart
Check that package PHP-FPM is exist
sudo vi /etc/php/<version>/fpm/pool.d/www.conf
Then you find this line and check that path is correct (file .sock must exists in the folder /var/run/php/)
listen = /var/run/php/php<version>-fpm.sock
Restart the service
sudo service php<version>-fpm restart
We allow SSH, ICMP and TCP from anywhere and in Outbound we allow anything
Route the traffic to the internet gateway (igw)
Create database for our application
Choose your DB instance type
Check the option connection to the EC2 instance