Skip to content

Instantly share code, notes, and snippets.

@zawyelwin
Last active August 10, 2023 08:25
Show Gist options
  • Save zawyelwin/1b4233cb2c5b8a9d300baf0cd06384d7 to your computer and use it in GitHub Desktop.
Save zawyelwin/1b4233cb2c5b8a9d300baf0cd06384d7 to your computer and use it in GitHub Desktop.
Deploy a Laravel app properly.

Add a user to the server and append the user to the sudo group. You can verify the user is in the sudo group using id <username> command.

adduser <username>
usrmod -aG sudo <username>

Install Ngxin MySql PHP and necessary extensions for Laravel.

sudo apt update
sudo apt install php7.4-common php7.4-bcmath openssl php7.4-json php7.4-mbstring php7.4-fpm php7.4-xml php7.4-mysql nginx curl zip unzip vim mysql-server

Install composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Start the Nginx PHP-FPM and MySql services.

systemctl start nginx && systemctl enable nginx
systemctl start mysql && systemctl enable mysql
systemctl start php7.4-fpm && systemctl enable php7.4-fpm

Configure MySql.

su - root
mysql_secure_installation

Add a non-root user and grant all permissions for a specific database.

CREATE USER dbuser@localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON <dbname>. * TO 'dbuser'@'localhost';
FLUSH PRIVILEGES;

Deploying the application and set proper permissions.

git clone <git-project-url>
cd <project>
sudo chown -R $USER:www-data .
sudo find . -type f -exec chmod 664 {} \;   
sudo find . -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Copy the .env file and update the necessary files like database and mail config. In your production environment, APP_DEBUG value should always be false. If the APP_DEBUG variable is set to true in production, you risk exposing sensitive configuration values to your application's end users.

cp .env.example .env
composer install --optimize-autoloader --no-dev
php artisan key:generate

Configure Nginx for pointing to Laravel app directory. For simplicity we will update the default config file.

sudo vim /etc/nginx/sites-enabled/default

Nginx Config

server {
    listen 80;
    server_name example.com;
    root /path/to/project/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index 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_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Verify configuration and syntax for nginx.

nginx -t

Restart nginx.

systemctl restart nginx

Optimizing Configuration Route and View Loading.

php artisan config:cache
php artisan route:cache
php artisan view:cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment