Procedure:
Elastic Beanstalk
I assume you already have a laravel project ready to deploy and an account in AWS. I registered for free tier. It's a free account but you need to input your cc details.
After logging in, search for Elastic Beanstalk in AWS Management Console.
Click Create Application.
Input your application name. Then for platform, choose PHP and click create application after.
In uploading laravel project source code, I tried two ways.
First method:
** Upload and Deploy**
Zip your laravel project. Make sure you zip it inside your laravel project just like what I did in the screenshot above. Upload the zip file then click Deploy.
Click Configuration in the left pane, then on the software category section, click edit action.
Set document root to /public then click apply.
Click the link for your site and you will be able to see your landing page in the browser. If not, please let me know.
Second Method:
Using Code Pipeline
Search for Code Pipeline in AWS Management Console .
Click create pipeline
Input your details
I'm using Github as my source provider. Choose the appropriate repository and branch. The next steps are all self-explanatory.
You will be able to see a screenshot like mine above if you are able to connect with code pipeline with github.
EC2
Search for EC2 in AWS Management Console
Click Running instances.
Right click on your laravel project name. Click connect. Please take note of the commands stated.
Now, lets create a key pairs. Still in EC2 Dashboard, in network and security click key pairs. Create key pair. A .pem file will be downloaded.
I tried using putty, winscp and git bash in connecting. I'll show how I did it with these three. You can choose whatever is more suited for you. They just have the same result.
Putty
We need to convert the .pem file to .ppk. To do that download first putty. Download the putty and putty gen installers here: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Puttygen is used to generate a .ppk by using the .pem file
Once done, open puttygen.
Click load then locate your .pem file.
Click save private key.
Open putty then in Auth section browse .ppk file.
The host name is ec2-user@your public DNS in EC2 instances section. Click open after.
If you see the same screen with my screenshot above, you are able to connect successfully using Putty.
Winscp
Download and install winscp here: https://winscp.net/eng/docs/guide_install.
Open winscp and click Advanced. In SSH authentication section, uploadyour .ppk file. If you will upload the .pem file, it's fine. This will be converted to .ppk.
Same with putty, hostname is your public DNS and user name is ec2-user. Click login.
If you have the same screenshot as mine above, you have successfully connected to winscp. Go ahead and transfer/copy/update files from your local machine to the server.
Note: If you're using winscp, you need to use GIT or putty to execute commands.
GIT Bash
Download git here : https://git-scm.com/downloads.
chmod 400 laravel-newkey.pem
This is my instance in EC2. Change this with your instance. You must execute it in the folder where you save your .pem file. In my case, my .pem file is in downloads folder.
Execute the ssh command found in your ec2 instance. Make sure to use ec2-user not root.
I'll proceed on using GIT bash.
sudo chown -R ec2-user /var/app/current/
First give the current user permission to write.
cd /var/app/current/
To go to your laravel project folder, execute command above.
sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composerp
sudo ln -s /usr/local/bin/composer /usr/bin/composer
sudo composer install
We need to install composer, so please execute commands above. This will create a vendor file in your laravel project folder.
sudo chmod -R 0777 storage
sudo chmod 777 storage/logs/laravel.log
sudo chmod 777 storage/framework/cache
Change your storage file permission so that the server can access it.
cp .env.example .env
php artisan key:generate
Don't forget to have .env file, then execute php artisan key:generate.
Before we'll proceed to database, there has been a change in Elastic beanstalk as of this writing. It is now configured to nginx instead of apache so the .htaccess file no longer works. All laravel routes are getting 404 Not Found Nginx error except for the landing page. If you don't encounter this issue, just ignore the work around below.
cd /etc/nginx/conf.d/elasticbeanstalk
Navigate to the php.conf file using the command above.
sudo nano php.conf
The above command will let us open the file
location / { try_files $uri $uri/ /index.php?$query_string; gzip_static on; }
Insert the following into this after index and before location.
To save, press ctrl+o. Then ctrl+x to exit.
sudo nginx -s reload
Run command above to restart nginx.
Database
.env file
Environment variables in Elastic Beanstalk software section
My database setup in Elastic Beanstalk database section
The info for database config is in RDS. Go ahead and search for RDS in AWS management section.
DB_name is ebdb
DB_HOST is the endpoint
DBUSERNAME and DBPASSWORD are the one you setup in environment variables in Elasticbeanstalk sofware section. It should be the same in the .env file.
I already migrated my db a while ago and forgot to screenshot ehhe
After setting up your database config, go ahead and migrate and seed. Then after, access your site in the browser.
Sharing my knowledge about deployment and I hope I did not miss a thing. Please let me know if you encounter issues. I’ll be happy to help.