Laravel is a free, open-source PHP framework that is more popular, allowing you to easily develop scalable and flexible web applications. It's a simple, flexible, and lightweight PHP framework with a readable syntax for building modern and powerful applications from scratch.
Laravel is the best choice for you if you're looking to design a next-generation application.
In this tutorial, we show you how to set up a web application using the Laravel framework and NGINX as the web server on Ubuntu 22.04 server.
Prerequisites
- A server running Ubuntu 22.04 with a minimum of 2 GB of RAM and 10 GB of disk space.
- A root password set up on your Cloud server.
Getting Started
Before getting started, we recommend updating the packages to the latest version. You can do this by running the following command:
# apt-get update && apt-get upgrade -y
Then, you'll need to install some required packages on your system. You can install all of them by running the following command:
# apt-get install apt-transport-https ca-certificates curl software-properties-common -y
Once all the packages are installed, you can proceed to the next step.
Install Docker and Docker Compose
Next, you'll need to install Docker and Docker Compose on your server. By default, the latest version of Docker is not available in the default repository of Ubuntu 22.04, so you'll need to add the repository for this purpose.
First, download and add the GPG key using the following command:
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Then, add the Docker repository with the following command:
# echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
Once the repository is added, update your system and install Docker and Docker Compose by running the following command:
# apt-get update && apt install docker-ce docker-compose -y
Once the installation is complete, you can check the status of the Docker service with the following command:
# systemctl status docker --no-pager
Download Laravel and Install Dependencies
Next, you'll need to download the Laravel framework and install the required dependencies using the Docker PHP composer image.
First, download the latest version of Laravel with the following command:
# git clone https://github.com/laravel/laravel.git laravel
Then, install the required dependencies with the following command:
# cd laravel && docker run --rm -v $(pwd):/app composer install
You should see the following output when the installation is complete:
@php artisan package:discover --ansi
INFO Discovering packages.
laravel/sail .......................................................... DONE
laravel/tinker ........................................................ DONE
nesbot/carbon ......................................................... DONE
nunomaduro/collision .................................................. DONE
nunomaduro/termwind ................................................... DONE
spatie/laravel-ignition ............................................... DONE
85 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
@php artisan vendor:publish --tag=laravel-assets --ansi --force
INFO No publishable resources for tag [laravel-assets].
The command above will create a temporary container and copy the contents of your directory ~/laravel into the container.
Dockerizing Laravel
Next, you'll need to create a new file docker-compose.yml and a Dockerfile for your Laravel project.
First, create a file docker-compose inside your ~/laravel directory:
# nano ~/laravel/docker-compose.yml
Add the following lines:
version: '3'
services:
# PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:8.2-fpm-bookworm
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
# NGINX Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
# MySQL Service
db:
image: mysql:latest
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: oSFrJiZ1y67nbA41L0pdzOtA5R
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
# Docker Networks
networks:
app-network:
driver: bridge
# Volumes
volumes:
dbdata:
driver: local
Save and close the file when you are finished.
Next, create a Dockerfile to create custom images with all the software required by your application inside your ~/laravel directory:
# nano ~/laravel/Dockerfile
Add the following lines:
FROM php:7.2-fpm-buster
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
nano \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Save and close the file when you're done. Next, create a Dockerfile to build and customize images with all the software required by your application within the directory ~/laravel:
# nano ~/laravel/Dockerfile
Add the following lines:
FROM php:8.2-fpm-bookworm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
RUN apt-get update && apt-get install -y \
build-essential \
wget \
lsb-release \
gnupg \
curl
# Add repo MySQL
RUN curl -s https://dev.mysql.com/downloads/repo/apt/ | grep mysql-apt-config \
| grep href | awk -F '=' '{gsub(/&p/, "", $4); print "https://dev.mysql.com/get/"$4}' \
| xargs wget -O mysql-apt-config.deb \
&& dpkg -i mysql-apt-config.deb && apt update
# Install dependencies
RUN apt-get update && apt-get install -y \
mariadb-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
nano
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
# Install extensions
RUN install-php-extensions gd pdo_mysql mysqli mbstring zip exif pcntl
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 nginx
RUN useradd -u 101 -ms /bin/bash -g nginx nginx
# Copy existing application directory contents
# COPY . /var/www
# Copy existing application directory permissions
# COPY --chown=www:www . /var/www
# Change current user to www
USER nginx
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Save and close the file when you're done.
Configure PHP, MySQL, and NGINX for Laravel
Next, you'll need to create a PHP directory within your ~/laravel directory and create the local.ini file inside the ~/laravel/php directory:
# mkdir ~/laravel/php ; nano ~/laravel/php/local.ini
Add the following lines:
upload_max_filesize=40M
post_max_size=40M
Save and close the file when you're done. Then you'll need to create the directory nginx/conf.d inside your directory ~/laravel and create an app.conf file for Nginx configuration:
# mkdir -p ~/laravel/nginx/conf.d ; nano ~/laravel/nginx/conf.d/app.conf
Add the following lines:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Save and close the file when you're done. Then, create the MySQL directory within your directory ~/laravel and create the my.cnf file with the required settings:
# mkdir ~/laravel/mysql ; nano ~/laravel/mysql/my.cnf
Add the following lines:
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log
Deploy Laravel with Nginx and MySQL Services
At this point, we have defined all services in the docker-compose file and created a configuration file for each service. Next, make a copy of the default .env.example file with the following command:
# cp .env.example .env
Now run the following command to download all required images and build container services based on the 'docker-compose.yml' file with the following command:
# docker-compose up -d
Once the process is complete, you can check the active containers with the following command:
# docker ps
You should see the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07ac3fc0fbfa nginx:alpine "/docker-entrypoint.…" 2 minutes ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp webserver
a50984eff65e php:8.2-fpm-bookworm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 9000/tcp app
7857e21e6d26 mysql:latest "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp db
Configure the Laravel Container
Next, you'll need to modify the .env file with the Laravel container app.
You can easily edit any file within the container using the "docker-compose exec" command. First, open the .env file within the Laravel app container with the following command:
# docker-compose exec app nano .env
Change the following lines:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=oSFrJiZ1y67nbA41L0pdzOtA5R
Save and close the file. Now generate the Laravel application key and clear the cache with the following command:
# docker-compose exec app php artisan key:generate && docker-compose exec app php artisan config:cache
Then, migrate the database using the following command:
# docker-compose exec app php artisan migrate
Access the Laravel Web Interface
At this point, you've dockerized Laravel with NGINX and MySQL. Now, open your web browser and enter the URL http://YOUR-server-ip. You will be redirected to the default Laravel control panel:
Congratulations! You have Dockerized a web application using the Laravel framework and NGINX as the web server on a Cloud server with Ubuntu 22.04.
Remember, if you have any questions about this or any other issue related to your servers on Clouding, feel free to write to soporte@clouding.io. We're here for whatever you need, ask us!