Deploying Laravel to local Virtual Machine

Many developers are frustrated when it comes to deploying their Laravel Applications, especially those who lack the budget and want an all-in-one self-managed server. In this article, I will show the steps to deploy laravel to your server on Ubuntu 20.04 using VMware workstation Pro 16.

Setting up ubuntu on a Virtual Machine (VMware workstation Pro 16)

Download Ubuntu Desktop 22.04.1 LTS from ubuntu.com

Once you have downloaded the OS, you can now follow the steps below to set up the virtual machine:

  1. Open Virtual Machine
  2. New Virtual Machine
  3. Custom
  4. Installing the operating system later
  5. Linux - Ubuntu 64 bit
  6. Name the virtual machine
  7. Processor configuration
  8. Memory for Virtual Machine
  9. Use bridge networking
  10. LSI Logic (I/O Controller types)
  11. SCSI (Disk types)
  12. Create new a virtual disk
  13. Specific disk capacity - store as a single file
  14. Modify the CD?DVD (SATA) - use iso image
  15. run the virtual machine
  16. language choose US

After successfully installing ubuntu, ping google.com to check the internet connectivity. Check you ipv4 address using ap addr

Configure your router to allow port forwarding

Once you have successfully installed ubuntu, you need to configure your router to allow port forwarding. This is important because you need to allow your server to be accessible from the internet.

Head over to your router's admin page and look for port forwarding. You should be able to find it under the advanced settings. Once you have found it, you can now configure it to allow port forwarding.

Edit the IP address with your server's IP address that you got from the previous step, then edit the TCP Port with the port you want to use. In this example, I will be using port 80.

Install git

sudo apt install git
git --version

For more information, pls refer to this tutorial

Clone the project

sudo mkdir -p /var/www/your-domain
sudo chown -R $USER:$USER /var/www/your-domain
sudo chmod -R 755 /var/www/your-domain
cd /var/www/your-domain
git clone your-git-repo

Permissions

cd cd /var/www/your-domain (root)
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
chmod -R a+x node_modules

For more information, pls refer to this tutorial

Setting up Nginx

sudo apt update
sudo apt install nginx
sudo ufw enable
sudo ufw status
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow 'Nginx Full'
sudo nano /etc/nginx/sites-available/your-domain (patse in the nginx config from https://laravel.com/docs/9.x/deployment#nginx)
ctrl x >> press y >> press enter
sudo ln -s /etc/nginx/sites-available/your-domain /etc/nginx/sites-enabled/
sudo nginx -t (make sure that there are no syntax errors in any of your Nginx files)
sudo systemctl restart nginx

Once you have successfully installed nginx, you can now edit the nginx config file to allow laravel to run on your server. You should be able to find the config file under /etc/nginx/sites-available/your-domain. You can now paste in the config detail from laravel.com

For more information, pls refer to this tutorial

Install php8.1

sudo apt install --no-install-recommends php8.1
php -v

For more information, pls refer to this tutorial

Install php-ext

sudo apt-get install -y php8.1-cli php8.1-fpm php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-msgpack php8.1-igbinary php8.1-redis  php8.1-memcached php8.1-pcov php8.1-xdebug php8.1-intl php8.1-readline php8.1-ldap php8.1-soap

For more information, pls refer to this tutorial

Install Composer

sudo apt install curl
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer

For more information, pls refer to this tutorial

Install Nodejs (Node Version Manager)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install v18.5.0

For more information, pls refer to this tutorial

Install mysql-server

sudo apt install mysql-server
sudo MySQL
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> exit
sudo mysql_secure_installation
mysql -u root -p
mysql> CREATE USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'your-domain'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
MySQL> CREATE DATABASE new_database;

For more information, pls refer to this tutorial

Install redis-server (optional)

sudo apt update
sudo apt -y install redis-server
sudo add-apt-repository ppa:redislabs/redis
sudo apt-get update
sudo apt-get install redis

Install meilisearch (optional)

sudo apt install ./meilisearch.deb
meilisearch (to start meilisearch)

Conclusion

You should now be able to run your laravel app on your server. If you have any questions, feel free to reach out to me on twitter. I hope this tutorial helps you to get started with your laravel app on your server.

Be sure to share this tutorial with your friends and colleagues. Thanks for reading.