Deploying Laravel backend and multiple vue frontend apps on the same server - laravel

I have a Laravel and several Vue apps. I'm trying to deploy them on Digital Ocean using docker and nginx.
I've never deployed apps before, so I poorly understand the process. I've already deployed Laravel backend and I am thinking of deploying my vue apps in separate docker containers, but I dont actually understand how should I configure my nginx server.
I've found out I could build my frontend app into the Laravel's public folder, but this approach works with a single Vue app, while I have several.
I'd use some help on how to manage this. Thanks in advance!
Here's my docker-compose.yml(without vue yet) :
version: '3'
services:
nginx:
image: nginx:alpine
container_name: shop_nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www/shop/backend
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/ssl/:/etc/nginx/ssl/
networks:
- shop
laravel:
container_name: shop_laravel
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/var/www/shop/backend'
networks:
- shop
depends_on:
- pgsql
- redis
pgsql:
container_name: shop_postgres
restart: unless-stopped
image: 'postgres:14'
ports:
- '5432:5432'
environment:
PGPASSWORD: 'shop'
POSTGRES_DB: 'shop'
POSTGRES_USER: 'shop'
POSTGRES_PASSWORD: 'shop'
volumes:
- 'shop-pgsql:/var/lib/postgresql/data'
networks:
- shop
redis:
container_name: shop_redis
restart: unless-stopped
image: 'redis:alpine'
ports:
- '6379:6379'
volumes:
- 'shop-redis:/data'
networks:
- shop
networks:
shop:
driver: bridge
volumes:
shop-pgsql:
driver: local
shop-redis:
driver: local
And nginx configuration:
server {
listen 80;
listen [::]:80;
server_name shop.laravel;
root /var/www/shop/backend/public;
index index.php;
charset utf-8;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass laravel: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;
}
}

Related

Laravel Docker On Window Volume mounting

I setup docker on window and setup Laravel project to run on docker, images are created but unfortunately volume is not mounting. Docker Project https://github.com/wariszargardev/Laravel-Nginx-Docker- project is setup on root. I have tried no of solution but unfortunately to setup on window. Same project with no changes working on Mac M2.
docker-compose.yml
version: '3.8'
networks:
laravel-vpn-network:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- php
# - mysql
networks:
- laravel-vpn-network
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- Laravel-vpn-network
**Docker File **
FROM php:8.0-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 9000
**
App.conf**
Root of project in nginx directory conf.d directory inside this app.conf file present
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 php: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;
}
}
This simple template if anyone available to guide me?

Problem docker-compose with laravel nginx not working on production

I wrote a docker-compose.yml with nginx, mysql, and an application laravel with a dockerfile.
My docker-compose :
version: "3.7"
services:
app_back:
build:
context: .
dockerfile: Dockerfile
image: dreamy_back
container_name: dreamy-back-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- .:/var/www
networks:
- dreamy
db:
image: mysql
container_name: dreamy-db
restart: unless-stopped
depends_on:
- app_back
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: 4ztgeU%
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- dreamy
nginx:
image: nginx:1.17-alpine
container_name: dreamy-back-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/vhosts:/etc/nginx/conf.d
networks:
- dreamy
networks:
dreamy:
driver: bridge
My dockerfile for an laravel application :
I execute composer install inside the container.
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
My nginx conf :
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_back: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;
}
}
On my laptop it's working but on my vps server I get an error 500.
My docker should be accessible port 8000
My nginx logs
UPDATE - I found the problem, in production the application does not display laravel errors. So it is the default ngynx page that is displayed. The error was the right for the storage file.
Thank you for your help.

hosting a react web app and a docker laravel app with nginx

I have 2 apps I'm trying to host on one machine (Ubuntu 22). The first app is a client side react app that makes api calls to my backend which is a docker compose laravel/nginx/sql. I went into sudo vim /etc/nginx/sites-available/MY.DOMAIN.COM and added the following:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm; #index.nginx-debian.html;
server_name MY.IP.ADDRESS.HERE;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 81;
listen [::]:81;
root /var/www/html/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name MY_DOMAIN_NAME.COM www.MY_DOMAIN_NAME.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
On port 80 is where my react app lives. When someone visits MY.DOMAIN.COM I'm able to serve it without any issues. I'm not sure why putting server_name MY.IP.ADDRESS.HERE; works there vs the domain name like I did for port 81 server_name MY_DOMAIN_NAME.COM www.MY_DOMAIN_NAME.com;. Anyway on port 81 is where my laravel app should live. It's using a docker-compose.yml with this configuration:
version: "3.7"
services:
app:
build:
args:
user: admin
uid: 1000
context: ./
dockerfile: Dockerfile
image: my-laravel
container_name: my-laravel-app
restart: unless-stopped
working_dir: /var/www/
# command: bash -c "php artisan migrate:fresh --seed"
# depends_on:
# - db
# links:
# - db
volumes:
- ./:/var/www
networks:
- my-laravel
db:
image: mysql:8.0.30
container_name: my-laravel-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- my-laravel
nginx:
image: nginx:alpine
container_name: my-laravel-nginx
restart: unless-stopped
ports:
- '8081:81'
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- my-laravel
networks:
my-laravel:
driver: bridge
The react app is trying to make api calls to const domain = "http://MY.IP.ADDRESS.HERE:8081"; and fails with the message net::ERR_CONNECTION_REFUSED This is where I'm not sure what I did wrong.
I have nginx installed, but I'm also using it in the docker image. Is there any conflict there? Do I even need to configure my installed nginx instance to serve the docker image?
Anyway, the funny thing is I had this all setup and working last week, but I decided to add an ssl cert with cert bot which messed everything up and now I'm starting from scratch. The problem is I don't remember how I got it working to being with lol.
Any help here would be greatly appreciated, thank you in advance.

Problems with connect rabbitmq [Laravel][RabbitMQ][Docker]

I'm trying to connect my application (in Laravel) with rabbitMQ, but when trying, this error happens:
[PhpAmqpLib\Exception\AMQPIOException
Error Connecting to server(99): Cannot assign requested address]: https://i.stack.imgur.com/wmLPo.png
Here's my docker-compose
services:
nginx:
image: nginx:1.13
container_name: nginx
volumes:
- "./projects/publisher:/var/www/publisher"
- "./projects/consumer:/var/www/consumer"
- "./volumes/nginx/nginx.conf:/etc/nginx/nginx.conf"
ports:
- "8080:80"
- "8081:81"
depends_on:
- php
php:
build:
context: ./volumes/php
container_name: php
volumes:
- "./projects/publisher:/var/www/publisher"
- "./projects/consumer:/var/www/consumer"
ports:
- "9000:9000"
depends_on:
- mysql
environment:
- MYSQL_USER=root
- MYSQL_PASS=123.456
mysql:
image: mysql:5.7.20
container_name: mysql
volumes:
- "db_data:/var/lib/mysql"
- "./volumes/mysql/initial-data:/docker-entrypoint-initdb.d"
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123.456
rabbitmq:
build:
context: ./volumes/rabbitmq
ports:
- "15672:15672"
- "5672:5672"
volumes:
- ./volumes/rabbitmq/rabbitmq-data:/var/lib/rabbitmq
volumes:
db_data:
ps.: Use laradock is not an option. I have to create this docker...
Can you help, me? =)
Put them in the same network and show me the config file of nginx.
example of network in docker compose:
nginx_docker:
networks:
- my-network
rabbit_docker:
networks:
my-network:
aliases:
- rabbit-net
app_docker:
networks:
my-network:
aliases:
- app-net
networks:
- my-network:
use this config for nginx config:
server{
listen 80;
server_name example.com;
location / {
proxy_pass http://example.com:port/
proxy_set_header Host $http_host
}
location /myUri {
proxy_pass http://example.com:port/
proxy_set_header Host $proxy_host
}
}

Ngnix laravel docker project return error 502 bad gateway

I tried run laravel inside docker container with ngnix.
Directory structure:
File docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
File Dockerfile:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
Result of command docker ps:
When I tried open in browser address http://localhost:8004 then get error:
502 Bad Gateway nginx/1.18.0
Ngnix config file:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Env vars:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
As you're using nginx as your web-server, you don't need to install PHP Apache (FROM php:7.4-apache) in your Dockerfile.
Instead try php:7.4-fpm and make sure nginx accesses that php-fpm correctly (line fastcgi_pass php:9000; in your default.conf file. In your case everything seems configured correctly).
One more thing to note that it's not necessary to expose php-fpm port 9000 to the host machine. As both containers (in your docker-compose.yml) will use the same network, nginx container can directly access php container. You can remove the lines
ports:
- "9000:9000"

Resources