Laravel Deployment on Elastic Beanstalk
Tags: aws, laravel, beginners, php
Published: 2020-04-20 21:44:22

so i was trying to deploy Laravel web application on AWS elastic beanstalk, although it appeared to be easy it wasn't. some obstacles were hidden or unanswered whether i should upload all project files ,how to connect to this instance, where to begin and many more questions. I've found answers for most of these questions,before i get started special thanks to the youtube channel [ProCoder](https://www.youtube.com/channel/UCWD8eDGfiYDBjw44v2Yrggg) it helped me a lot.
<blockquote>Note: this tutorial is for Linux users especially Ubuntu</blockquote>
## Requirements:
<ul>
<li>stable project on your localhost</li>
<li>AWS account , you can use the free tier</li>
</ul>
<h2>Create a new empty project</h2>
#### on your AWS console choose elastic beanstalk and create new project without adding anything from your project
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fyzkgwr111fe5y5rf9nw.png)
<p>select web server environment, then you will be in Application page</p>

#### write your application info
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pnsumivo9fol8balknjg.png)

#### write your environment name , domin and descrption
<blockquote>Note: by default AWS will choose them for you</blockquote>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h6muukf7m1ypaj6dsdui.png)

#### from platform drop down menu choose PHP and the rest will be chosen automatically
<blockquote>Note: feel free to change both the platform branch and platform version</blockquote>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dszcgf5hkq6dx8em1h8h.png)
#### choose sample application and click on configure , scroll down till you find Database click on edit
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5o2s2puiihuxrdvz5qhc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i9gpfxdewhgm38f4wr4q.png)
#### in this section the most important part is the username and password, save your settings and create your instance
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eq3evuazwdtcqlljakcs.png)
<blockquote>Note: if your AWS account is free stick to db.t2.micro instance</blockquote>
<h2>In your laravel project</h2>
<h3>Edit database configuration</h3>
<p>open your database config file /config/database.php now edit and make three global variables</p>
<pre>define('RDS_HOSTNAME', $_SERVER['RDS_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RDS_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RDS_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RDS_DB_NAME']);</pre>
<p>now scroll down to connections and edit it to be something like that</p>
<pre>'mysql' => [
            'driver' => 'mysql',
            'host' => RDS_HOSTNAME,
            'port' => env('DB_PORT', '3306'),
            'database' => RDS_DB_NAME,
            'username' => RDS_USERNAME,
            'password' => RDS_PASSWORD,
        ]</pre>
<h3>Zip your files</h3>
<p>this part is a little bit tricky cause if you didn't notice the files you're zipping it might affect your
    server,also make sure you're not adding any hidden folders. your files should be something like that:</p>
<ul>
<li>app</li>
<li>bootstrap</li>
<li>config</li>
<li>database</li>
<li>hooks</li>
<li>public</li>
<li>resources</li>
<li>routes</li>
<li>storage</li>
<li>tests</li>
<li>.editorconfig</li>
<li>.env</li>
<li>.env.example</li>
<li>.gitattributes</li>
<li>.styleci.yml</li>
<li>artisan</li>
<li>composer.json</li>
<li>composer.lock</li>
<li>package.json</li>
<li>package-lock.json</li>
<li>phpunit.xml</li>
<li>server.php</li>
<li>webpack.mix.js</li>
</ul>
<blockquote>Note: i've added the .env file to the file tree to make it easier but feel free to remove it</blockquote>
<h3>Upload zipped file</h3>
<p>now open your elastic beanstalk tab ,choose the environment that you'd like to deploy to , then click on the upload and deploy button,choose your file give it a label and press deploy.</p>
<h3>Web app route</h3>
<p>on the left-side of your environment page click on configuration,then edit your software section,now change document root to be /public and apply the changes</p>
<blockquote>Note: if your application need to migrate data and/or other installation don't worry continue to the SSH section</blockquote>
<h2>Add key pair to your elastic beanstalk</h2>
<p>this step is very important because it will help you connect with your EC2 instance from anywhere of course by using your key pair file(.pem)</p>
<blockquote>If you haven't made a key pair yet, make one by clicking Key Pairs below Security Group in the EC2 tab</blockquote>
<ul>
<li>In the AWS console, open the Elastic Beanstalk tab</li>
<li>Select Environment</li>
<li>Select Configurations in left panel</li>
<li>Select Security</li>
<li>Under "EC2 key pair:", select the name of your key pair in the Existing Key Pair field</li>
</ul>
<h2>open your env using SSH</h2>
<p>open EC2 tab then choose running instance and click on connect ,follow the instruction page about accessing your instance and you'd be online</p>
<h3>Install composer in your instance</h3>
<pre>sudo curl -sS https://getcomposer.org/installer | sudo php</pre>
<pre>sudo mv composer.phar /user/local/bin/composer</pre>
<pre>sudo ln /user/local/bin/composer /user/bin/composer</pre>
<h3>Open current version</h3>
first give the current user permission to write
<pre>sudo chown -R ec2-user /var/app/current/</pre>
<blockquote>Note: ec2-user is the name of your ec2 by default</blockquote>
<pre>cd /var/app/current/</pre>
<pre>composer dump-autoload</pre>
now all your artisan commands will work perfectly fine
<pre>php artisan migrate</pre>
change your storage file permission so that the server can access it
<pre>sudo chmod -R 0777 storage</pre>
<pre>sudo chmod 777 storage/logs/laravel.log</pre>
<pre>sudo chmod 777 storage/framework/cache</pre>
now everything is working perfectly fine you can run your own artisan commands