CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
How to setup Laravel project in ubuntu server (with SSL)

How to setup Laravel project in ubuntu server (with SSL)

Posted by

kamlesh paul

on

Dec 8, 2024

| 3 min read

Last updated on : Dec 8, 2024

Server
78 views

#Table of contents

  • Initial Server Setup
  • Installing Necessary Packages
  • Configuring Nginx
  • Setting Up SSL with Certbot
  • Setting Up Cron for Laravel Tasks

#Initial Server Setup

  1. First, ensure your server’s package repositories are up to date:
apt update && apt upgrade && apt autoremove

#Installing Necessary Packages

  1. We’re going to install a range of packages including PHP, Node.js, npm, Yarn, MySQL, and Nginx. Run the following commands:
apt-get update \
&& apt-get install -y build-essential libssl-dev curl zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin fswatch \
&& curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \
&& echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
&& apt-get update \
&& apt-get install -y php8.2-cli php8.2-dev php8.2-fpm\
    php8.2-sqlite3 php8.2-gd php8.2-imagick \
    php8.2-curl \
    php8.2-imap php8.2-mysql php8.2-mbstring \
    php8.2-xml php8.2-zip php8.2-bcmath php8.2-soap \
    php8.2-intl php8.2-readline \
    php8.2-ldap \
    php8.2-msgpack php8.2-igbinary php8.2-redis php8.2-swoole \
    php8.2-memcached php8.2-pcov php8.2-xdebug \
&& curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \
&& apt-get update \
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash \
&& source ~/.nvm/nvm.sh \
&& nvm install --lts \
&& nvm use --lts \
&& npm install -g npm \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /etc/apt/keyrings/yarn.gpg >/dev/null \
&& echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get install -y yarn \
&& apt-get install -y mysql-server \
&& apt-get install -y nginx \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& sudo mysql_secure_installation

#Configuring Nginx

  1. Move to the Nginx configuration directory:
cd /etc/nginx/sites-enabled
  1. Create and edit a new configuration file for your domain:
sudo nano domain.conf
  1. Copy and paste the following configuration into the file.

Don’t forget to replace example.com with your domain name:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /srv/example.com/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;
    }
 
    # Add WebSocket configuration here
    #location /app/app-key {
    #   proxy_pass http://localhost:6001;
    #   proxy_http_version 1.1;
    #   proxy_set_header Upgrade $http_upgrade;
    #   proxy_set_header Connection "Upgrade";
    #   proxy_set_header Host $host;
    #}
 
    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/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
 
    client_max_body_size 75M;
}
  1. Test your Nginx configuration:
nginx -t

If the output indicates success

nginx: configuration file /etc/nginx/nginx.conf test is successful

proceed to the next step.

  1. Reload Nginx to apply the changes:
service nginx reload

#Setting Up SSL with Certbot

  1. To secure your site with HTTPS, we’ll use Certbot. Run the following commands to install and configure Certbot for Nginx:
sudo snap install --classic certbot \
&& sudo ln -s /snap/bin/certbot /usr/bin/certbot \
&& sudo certbot --nginx \
&& sudo certbot renew --dry-run 

#Setting Up Cron for Laravel Tasks

  1. Open the crontab editor to schedule tasks:
sudo crontab -e
  1. Add the following line to run Laravel’s scheduled tasks every minute. Replace /srv/example.com with your project’s path if different:
* * * * * cd /srv/example.com && php artisan schedule:run >> /dev/null 2>&1

Note: Ensure you’ve backed up any existing configurations or data before making changes. Always test configurations in a safe environment before applying them to a production server.

Related Posts

  • How to Customize FrankenPHPHow to Customize FrankenPHP
  • How to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP PackageHow to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP Package
  • How to setup Laravel Queues on cPanel: A Step-by-Step GuideHow to setup Laravel Queues on cPanel: A Step-by-Step Guide
  • How to Protect Your Linux Server from Brute Force SSH AttacksHow to Protect Your Linux Server from Brute Force SSH Attacks
  • How to Manage Multiple GitHub Accounts on the Same ComputerHow to Manage Multiple GitHub Accounts on the Same Computer

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

78 views