Laravel application $_ENV array is empty after running composer update - php

After updating my laravel application with composer update my .env file is not loaded into scripts anymore. When I var_dump($_ENV) I get an empty array.
I use docker - and precisely laradock - to host my application. I only boot docker-compose up nginx mariadb so I only have the following containers running: nginx, php-fpm, workspace, mariadb and docker-in-docker.
When I ssh into the container docker-composer exec workspace /bin/bash and cat .env in the /var/www directory, I see that the .env file with all variables is available inside the container. But still, the $_ENV array is empty.
Solutions provided this question did not work form me. As laradock is not part of my composer definition nothing changed about the server setup and there is no .htaccess file that might mess things up as nginx is doing the job.
Any ideas where to start debugging?

Related

Laravel/Homestead vagrant box disapeared from system

I got a php project which is already done and for starters I have to put it up on a server (planning to use Heroku). I am new to PHP virtual boxes, Laravel, etc. The person who made the project recommended to use Laravel so I started to get things going that way. I am running on Windows 10 and using cmder as my terminal.
I downloaded VirtualBox & Vagrant.
Then using commander I added the Laravel/Homestead box
by doing:
vagrant box add laravel/homestead
in the folder c:\Users\Jacky\vagrant\ubuntu (At least I think that is where I added it)
Afterwards I believe I did
vagrant init
or vagrant init laravel/homestead
whatever I did afterwards I was able to run
vagrant ssh
and get into the system. There I was able to check and I had php and composer installed.
I then followed laravels instructions and created a homestead folder at c:\Jack\Homestead
ran git checkout for v7.19.2
ran init.bat
and configured the Homestead.yaml file
I made the following folder map
- map: C:\Jack\myapp\public
to: /home/vagrant/myapp
and the following site map
- map: myapp.test
to: /home/vagrant/myapp/public
I am a bit confused with the directory structure of this whole thing and not sure if I was supposed to put the myapp directory inside c:\Jack\Homestead\myapp or if just doing it like i did in c:\Jack\myapp is fine.
Anyways that still worked and I could see it in my web browser. So far all was good.
Then the project instructions told me to do the following
run composer install
There was an env.example file I had to change to .env and change some setting
run php artisan key:generate
run php artisan migrate --seed
run php artisan passport:install
run php artisan storage:link
At this point I got an error that the Symlink could not be created, so I googled and found in Stack Overflow to restart as admin. I ran cmder as admin and have been having a heart attack for the last 30 minutes since it told me I had to vagrant up and when I did so it couldnt find my box and even doing vagrant box list
I would get no results and it wanted to redownload which takes over 3 hours.
I just restarted without admin and I do find that my Laravel/Homestead box is listed so I assume it was installed for my user and not admin so I guess I cant run cmder as admin.
So now I am trying to connect again, I am not sure in which folder I should be running the vagrant commands but I only seem to have a VagrantFile in my C:\Jack\Homestead folder so if I run a vagrant command anywhere else it gives me an error that a vagrant environment is required, etc.
So I tried the following inside my homestead folder:
vagrant ssh - I get:
VM must be running to open SSH connection. Run `vagrant up to start the virtual machine.
(In my VirtualBox Manager i see my homestead-7 VM running though)
vagrant up - I get:
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...
==> homestead-7: Matching MAC address for NAT networking...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
A VirtualBox machine with the name 'homestead-7' already exists.
Please use another name or delete the machine with the existing
name, and try again.
vagrant ssh 'homestead-7' I also get
VM must be running to open SSH connection. Run vagrant up
to start the virtual machine.
And the problem is once I do connect I assume I will still have the same problem running:
php artisan storage:link
So my questions are as follows:
Where should I have my projects myapp folder, should it be where it is at C:\Jack\myapp or am I supposed to put it in C:\Jack\Homestead\myapp
Is the VagrantFile supposed to be only in my Homestead folder and is that where I should be running all my Vagrant commands from? Like vagrant ssh and vagrant up
How do I connect again now that I am getting those errors running vagrant ssh and vagrant up
Once I connect I assume I will get the same error running php artisan storage:link since to run as administrator seems to not work what do I do?
I Assume that I should run git init and heroku create inside the myapp folder is this correct?
1) I always keep my apps folders outside of the Homestead folder. Your Homestead folder contains a git project, so if you put your apps inside you should include the folder in the .gitignore file. I think it's easier if you just put your apps elsewhere.
2) Vagrantfile is suposed to only be inside of your Homestead folder ( c:\Jack\Homestead). You should run all your vagrant commands inside the Homestead folder.
3) I used to get this error when I tried to run 'vagrant up' to a project that used the same box name 'homestead-7'. If you have version control, check if the file c:\Jack\Homestead\.vagrant\machines\homestead-7\virtualbox\id has been changed. If so, try restoring the old value and running 'vagrant up' again.
4) You are running in a windows environment, right? Windows can be a little temperamental with symlinks inside vagrant. You'll get the same problem if you try to npm install something. What I do to solve this is to run npm (and php artisan storage:link) outside of the virtual machine, in the host pc, inside the root of the app folder. Just a reminder: to do that you will need php installed in your host machine.
5) All commands related to the app (and not the virtual machine) should be run inside the virtual machine, in the folder app (ex: /home/vagrant/code/myapp) or inside the app folder of your host machine. Those two folders are in sync, thanks to vagrant.

Debug Symfony CLI application with PhpStorm inside Docker container

My current setup involve PhpStorm IDE in which I have imported Symfony 3 projects which is basically CLI tool. On the host machine I don't have PHP installed so I'm running the application from Docker container which has PHP and Xdebug installed.
I don't have issues to debug web applications from Docker containers but with Symfony and this CLI tool it seems a little bit more tricky.
My question is how to properly set this up and debug it from PhpStorm? I tried to create a new debug configuration (PHP Remote Debug) but breakpoints are not trigged.
Suppossing you have followed into the instructions mentioned into the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
Or similar questions
Then you need to follow theese steps:
Step1:
Get shell access to your container via running:
docker exec -ti ^container_id^ /bin/sh
Or if running a debian/ubuntu based one (or you installed manually bash):
docker exec -ti ^container_id^ /bin/bash
The ^container_id^ can be found via docker ps command it is the first column of the table. If running on a small window just pipe int into less -S resulting the command:
docker ps | less -S
Then export the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
Please keep in mind to setup the correct value specified into Servers section as you see in the image:
It is important in order not to run into the problem specified in this question.
Then just enable debugger listentin into the phpstorm and spawn the cli as you do when you run a symfony application.

Call phpunit from console in my symfony project

this is my current status:
I have a running symfony environment, based on a docker image. Everything works fine and from PHPStorm i can execute phpunit tests. But i want to execute them manually from console.
After i started all services by using docker-compose up --build, i login into the phpfpm service by: docker-compose exec -it phpfpm bash
then i move into my symfony project folder that contain all folders and files like "app/, bin/, vendor/, ... composer.json... etc"
I could go by calling vendor/phpunit/phpunit/phpunit but i want to get a shorter way. Is there any chance, maybe calling bin/phpunit or something like this?
Thanks in advance,
Max
PHPUnit has that script in the own composer.json, so bin (or vendor/bin) directory should contains a relevant symlink after running composer install. Also check your composer.json for bin-dir settings.
At least you always can create a symlink:
$ ln -s vendor/phpunit/phpunit/phpunit phpunit
According to the symfony docs the preferable way of using phpunit is by .phar file. In this way you can download phpunit as a .phar file and it will works both outside and inside docker container.
wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar --version
Type now php phpunit-6.0.phar to run tests.

Docker + PHP - ERROR: Couldn't connect to Docker daemon?

I'm following this guide to set up a PHP development environment with Docker.
I have created a folder on my desktop docker-php and added a docker-compose.yml file into it, with this content:
nginx:
image: nginx:latest
ports:
- 80:80
On my terminal:
$ cd /home/my-username/Desktop/docker-php/
$ docker-compose up -d
I get this error:
ERROR: Couldn't connect to Docker daemon at
http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the
DOCKER_HOST environment variable.
I'm on Xubuntu 16.04.
Or perhaps I should put the folder in the specific location that is required by Docker? If so, which is it?
The most common reason for this error is that you ran 'docker-compose up' without sudo. As long as there is docker installed and is up and running, you are likely missing sudo in the docker command.
You could use native Docker
One option is to abandon docker-machine and use a native Docker setup on your system. Since you are on Linux (Xubuntu), this is an option for you. docker-machine is most often used by people who can't run Docker natively (Mac or Windows), and use it to install a Docker-capable VM and some local commands on their OS to talk to it.
You can find install docs for Docker on Linux here.
However, you already have docker-machine installed, so this may be the most disruptive option for you.
Your docker-machine may not be running
The error you are getting is saying the Docker client cannot talk to the server. One potential reason for this is that your docker-machine VM isn't running. You should verify it is running, and if not, start it.
To get a list of your docker-machines (may be one or more):
docker-machine ls
You will probably have one machine named default, but you may have more, depending on how you did your setup.
You can get the current status with:
docker-machine status <machine-name>
And you can use stop, start, restart to manage the docker-machine.
(More in the Docker Machine CLI reference.)
You need the proper environment set
docker-machine relies on environment variables to work properly. Because you may have multiple docker-machine setups, you have to tell the client which one to use.
To set the environment, you can get it from the docker-machine command.
docker-machine env <machine-name>
And you can automatically inject it into the environment (this may be a useful thing to put into your shell startup file).
eval "$(docker-machine env <machine-name>)"
You should end up with env vars similar to these:
DOCKER_HOST=tcp://192.168.99.101:2376
DOCKER_CERT_PATH=/Users/nathanleclaire/.docker/machines/.client
DOCKER_TLS_VERIFY=1
DOCKER_MACHINE_NAME=dev
Keep in mind you should use the eval form here, not just run the env command and paste the output into your shell setup; it may change on a docker-machine restart, etc, so you can't rely on an old setup to still work later.
If your docker-machine is running, and these env vars are set, your docker and docker-compose commands should work.
Solution:
sudo usermod -a -G docker USERNAME

How use my docker-compose configuration with gitlab CI

I have a project on gitlab, and I would use gitlab CI for unit testing.
Actually, I have a other repository name "docker" with docker-compose.yml and Dockerfile for two project (because i reproduce the production configuration, the two project are interdependant)
Actually in my dev configuration
in Projects directory:
docker
project_1
project_2
in docker directory:
docker-compose.yml
Dockerfile-project1
Dockerfile-project2
[some config file ask in dockerFile]
docker-compose.yml have relative path as ../project_1 and ../project_2
For set up my configuration, I make :
cd docker
docker-compose up -d project1 (name in docker-compose.yml)
docker exec -ti project1 bash
Question ?
I want know how I can pull the git repository "docker" and launch docker-compose up for the project1 since gitlab CI start ?
Thanks
We've built a gitlab runner with docker-compose support. See its README for setup and configuration.
Basically you just use the same commands like in development, see here for an example with Makefiles or this one with native docker-compose commands.

Categories