Laravel is a popular PHP framework designed for building modern web applications. Docker helps streamline the setup process by providing a containerized environment. In this article, we'll show you how to install Laravel with Docker on Ubuntu 22.04. Additionally, we'll discuss how you can use rsync to mirror your Laravel setup across different servers. If you're looking for a reliable hosting solution, you might consider a Windows VPS UK for hosting your Laravel application.
Step 1: Install Docker and Docker Compose
First, ensure Docker is installed on your Ubuntu server. You can do this by running the following commands:
sudo apt update && sudo apt install docker.io
Next, install Docker Compose:
sudo apt install docker-compose
Docker and Docker Compose are essential for setting up Laravel in a containerized environment. This setup is suitable for local development or for hosting on a UK Windows VPS.
Step 2: Set Up Laravel Project
Create a new directory for your Laravel project and use Docker to pull a Laravel image. First, clone the Laravel GitHub repository or set up a new Laravel project using the following Docker command:
docker run --rm -v $(pwd):/app composer create-project --prefer-dist laravel/laravel my-laravel-app
This command will download Laravel into your project directory. For hosting on a Windows VPS hosting UK or other virtual private servers, you can easily replicate this process on the remote machine.
Step 3: Create a Docker Compose File
Next, create a docker-compose.yml
file in your project directory. This file defines the services needed to run your Laravel application, such as PHP, MySQL, and Nginx. Add the following content:
version: '3'
services:
app:
image: php:8.0-fpm
container_name: laravel_app
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- laravel
webserver:
image: nginx:alpine
container_name: laravel_web
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/nginx:/etc/nginx/conf.d
ports:
- "8000:80"
networks:
- laravel
db:
image: mysql:5.7
container_name: laravel_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel
networks:
laravel:
driver: bridge
volumes:
dbdata:
driver: local
This file configures the Laravel application to run in a Docker container with MySQL and Nginx. Whether you're running on a Windows Virtual Private Server hosting or another server, Docker Compose simplifies the process.
Step 4: Start the Containers
Now that your Docker Compose file is set up, start the containers by running:
docker-compose up -d
This command will pull the necessary Docker images, build the containers, and run your Laravel application. You can now access your Laravel application at http://localhost:8000
or the IP of your server. This setup works seamlessly, even if hosted on a UK VPS Windows.
Step 5: Mirror Your Laravel Setup with rsync
To ensure that your Laravel application is mirrored across different servers, you can use rsync. This helps to keep the same codebase on multiple servers, especially useful if you're managing multiple VPS Windows Servers or have a backup environment.
To mirror your Laravel project, run:
rsync -avz /path/to/laravel/ user@remote-server:/path/to/destination/
This command synchronizes your Laravel project from the local machine to a remote server. You can automate this process for regular backups or deployment to multiple servers.
Step 6: Additional Configuration for Production
For a production environment, you may want to configure environment variables, SSL certificates, and proper file permissions. Whether you're hosting on a Windows VPS hosting UK or another environment, it's crucial to follow security best practices.