Deploying Laravel applications in AWS Lightsail with a NGINX instance
Hello everybody, I am relatively new to AWS and recently I was trying to deploy a Laravel application into AWS Lightsail with a nginx instance. At this article I am going to share my solutions! Let’s get started.
Cloning your project
According to the bitnami documentation the recommended directory for your Laravel project files is:
/opt/bitnami/projects/[your-project-name]
Changing the storage directory permission
After that you cloned your project, you need to give the web server writing permission. Trust me this step could be a nightmare if you don’t do it properly.
sudo chown -R daemon:daemon /opt/bitnami/projects/[your-project-name]/storage
Do not forget to include -R flag as you might face some errors like:
The stream or file /opt/bitnami/projects/[your-project-name]/storage/logs/laravel.log could not be opened in append mode: failed to open stream: Permission denied.
Creating the NGINX configuration for your application
Now you need to create a .conf file containing the application’s HTTP server block configuration :
cp /opt/bitnami/nginx/conf/server_blocks/sample-server-block.conf.disabled /opt/bitnami/nginx/conf/server_blocks/[your-project-name]-server-block.conf
The configuration block inside the “[your-project-name]-server-block.conf” file should be something like:
server {
# Port to listen on, can also be set in IP:PORT format
listen 80 default_server;
root /opt/bitnami/projects/[your-project-name]/public;
# Catch-all server block
# See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
server_name _;
include "/opt/bitnami/nginx/conf/bitnami/*.conf";
}
Similarly, you also need to create another .conf file containing the application HTTPS server block configuration :
cp /opt/bitnami/nginx/conf/server_blocks/sample-https-server-block.conf.disabled /opt/bitnami/nginx/conf/server_blocks/[your-project-name]-https-server-block.conf
The configuration block inside the [your-project-name]-https-server-block.conf file should be something like:
server {
# Port to listen on, can also be set in IP:PORT format
listen 443 ssl default_server;
root /opt/bitnami/projects/[your-project-name]/public;
# Catch-all server block
# See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
server_name _;
ssl_certificate bitnami/certs/server.crt;
ssl_certificate_key bitnami/certs/server.key;
include "/opt/bitnami/nginx/conf/bitnami/*.conf";
}
Finally, you need to restart the NGINX server with the following command:
sudo /opt/bitnami/ctlscript.sh restart nginx
Congratulation!, your NGINX server is configured successfully.
Note: you still need to enter the database connection details in the .env file and also you need to run the following commands as well :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=user
DB_PASSWORD=pass
php artisan key:generate
php artisan migrate
Hopefully this article would help you to configure you web server easier.
Please feel free to leave a message if you see any mistake in this article.
Cheers!
Resources: