PHP in Docker with NTLM-proxy: Cannot assign requested address - php

I have Windows-10 with Docker 18.06.0-ce-win72 (19098).
In our network uses NTLM-proxy, so docker goes to the Internet through Ntlmaps (I also try Cntlm, result is the same).
I run image based on php:7.2-fpm with docker-compose.
I enter the conteiner
docker exec -it {id} bash
and run command:
curl https://packagist.org/packages.json // works!
or
wget https://packagist.org/packages.json // works!
But
php -r "echo file_get_contents('https://packagist.org/packages.json');"
results "failed to open stream: Cannot assign requested address in Command line code on line 1"
curl https://packagist.org/packages.json ----> works
php -r "echo file_get_contents('https://packagist.org/packages.json');" ----------> fails to open stream
php -r "echo curl_exec(curl_init('https://packagist.org/packages.json'));" ---------> works
I see ntlmaps-console and view request from "curl", but it's no requests in console from "file_get_contents".
In php allow_url_open => on
I found similar problem: https://github.com/composer/composer/issues/2169
But I can't disable IPv6 in Docker for test the solution.
I try run "sysctl", but "bash: sysctl: command not found"
I try set
sysctls:
- net.ipv6.conf.all.disable_ipv6=1
- net.ipv6.conf.default.disable_ipv6=1
- net.ipv6.conf.lo.disable_ipv6=1
in docker-compose.yml for service, but it didn't solve the problem
I run this container on VPS and this work on VPS.
How can I solve this problem on Windows + NTLM?
My docker-compose.yml: https://pastebin.com/PfsgzrLs

Solution for file_get_contents():
$context = stream_context_create([
'http' => [
'proxy' => 'tcp://10.0.75.1:3112', // 3112 - proxy port on host-mashine
'request_fulluri' => true
]
]);
echo file_get_contents('https://ya.ru', false, $context);

Related

PHP fails to access the docker socket via curl

My Code fails to reach the Docker Socket
$client = new GuzzleHttp\Client();
$test = $client->request('GET','http://v1.40/containers/json',[
'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
]);
I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.
So just in case someone stumbles upon this in the future and has the same or a similar problem.
Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)
user = root
group = root
you will have to append the -R flag to the run command to allow running the workers as root.

how to make SSH connection in apache

I trying to build a web app to manage my severs using ansible
now I using Centos 7, PHP 7.4,Laravel 8 and apache to build this app and I'm already install https://packagist.org/packages/asm/php-ansible composer package for using ansible inside of my project.
here is my code:
$ansible = new Asm\Ansible\Ansible(
'/var/www/xxx/storage/ansible',
'',
''
);
$ansible->playbook()->play($myplaybookPath)->inventoryFile($myInventoryPath)->execute(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer ."<br/>";
} else {
echo 'OUT > '.$buffer."<br/>";
}
});
Here is the output :
OUT > PLAY [install nano] ************************************************************
OUT > TASK [Gathering Facts] *********************************************************
OUT > fatal: [xx.xx.14.139]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
OUT > PLAY RECAP *********************************************************************
OUT > xx.xx.14.139 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
OUT >
the username and password in the inventory file is correct and I test it many times also I try with new server with a very simple password.
I google this error host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)." and I found out its about the authorized_keys for the user that run the ssh
I change the permission and mod of the .ssh folder for my apache user in /var/user/share/httpd/.ssh
buy this error still remain and I don't know how to fix this.
thanks for help
update
it's not about my web server Configuration and everything about apache and PHP is ok.
I run my the playbook directly from terminal using ansible-playbook:
ansible-playbook /var/www/xxx/storage/ansible/playbooks/install_nano -i /var/www/xxx/storage/ansible/inventories/testInventory -vvv
I got the same error and I guess it's about the Auth information in InventoryFile
ansible-playbook 2.9.25
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /var/www/xxx/storage/ansible/inventories/testInventory as it did not pass its verify_file() method
auto declined parsing /var/www/xxx/storage/ansible/inventories/testInventory as it did not pass its verify_file() method
Parsed /var/www/xxx/storage/ansible/inventories/testInventory inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
PLAYBOOK: install nano ******************************************************************************************************************************************************************************
1 plays in /var/www/xxx/storage/ansible/playbooks/install nano
PLAY [install nano] *********************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************
task path: /var/www/xxx/storage/ansible/playbooks/install nano:2
<xx.xx.14.139> ESTABLISH SSH CONNECTION FOR USER: root
<xx.xx.14.139> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/a7f10d151a xx.xx.14.139 '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<xx.xx.14.139> (255, '', 'Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n')
fatal: [xx.xx.14.139]: UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
PLAY RECAP ******************************************************************************************************************************************************************************************
xx.xx.14.139 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
Here is the inventory file content
[linux]
xx.xx.14.139
[linux:vars]
ansible_user=root
ansible_password=testPassword
also, I try ansible_ssh_user / password but it's not working!
Here is the solution
I enabled verbose mode in ssh connection and I notice that the outcoming SSH request didn't support Password Authentication.
There are 2 SSH config files /etc/ssh/sshd.conf for the incoming requests and /etc/ssh/ssh.cong for outcoming so I fix the issue from out coming request config file and enable the password authentication.
after that, I set permissions for my apache ssh files like keys and known hosts files.
after that, I create a command in laravel so I can run my ansible playbooks using the root user and Crond.

Why sail didn't build my laravel container?

I followed the guide in laravel document.Run the command
composer require laravel/sail --dev and command php artisan sail:install in my terimal.After that I tried the command ./vendor/bin/sail up, but I got this : No such file or directory.I turn to termial and open an Ubuntu tab to run this command.But I got this
Building laravel.test
[+] Building 27.0s (4/4) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 38B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> ERROR [internal] load metadata for docker.io/library/ubuntu:21.04 26.9s
=> [auth] library/ubuntu:pull token for registry-1.docker.io 0.0s
------
> [internal] load metadata for docker.io/library/ubuntu:21.04:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to authorize: rpc error: code = Unknown desc = failed to fetch oauth token: Get "https://auth.docker.io/token?scope=repository%3Alibrary%2Fubuntu%3Apull&service=registry.docker.io": net/http: TLS handshake timeout
ERROR: Service 'laravel.test' failed to build : Build failed
I tried docker login ...failed.
I tried delete the vendor directory and then run command composer update to update my sail file... failed.
Please help me.Great thanks!
I encountered similar problems when I was setting up laravel sail project and here is my solution.
If you get No such file or directory, please confirm that you have Laravel Sail installed successfully and the files are there under vendor/bin/sail
Then, you should check that the DNS is configured correctly. Try ping google.com and see if there's any response. If not, it is probably why you're encountering this problem and there's some work for you. However, we just need to configure the DNS and it is pretty simple.
Create a file sudo vim /etc/wsl.conf
Add the following line
[network]
generateResolvConf=false
Save and exit :wq
sudo vim /etc/resolv.conf
Your file may probably look like this
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.28.32.1
Just replace the nameserver to 8.8.8.8
nameserver 8.8.8.8
Finally go to your project directory and try ./vendor/bin/sail up again

PHP can't reach url

Today I set up some new PCs with Linux Mint and wanted to get my Magento-SOAP API run locally.
If I run the script:
$client = new SoapClient('http://magento.dev/mageapi/v2_soap/?wsdl', array('login' => 'apiuser', 'password' => '12345678', 'cache_wsdl' => WSDL_CACHE_NONE, 'trace'=>1))
I get an error that it can't reach http://schemas.xmlsoap.org/soap/encoding/
So I tried it with:
php -r "echo file_get_contents('http://schemas.xmlsoap.org/soap/encoding/');" -d allow_url_fopen=1
If I tun it trough the local network I get the warning:
PHP Warning: file_get_contents(http://schemas.xmlsoap.org/soap/encoding/):
failed to open stream: HTTP request failed! in Command line code on line 1
But If I run it through my mobile phone (via tethering) it works just fine.
It does not look like a network, but more like a local DNS-issue. On my MacBook and on my Windows device in the same network it's running fine.

installing php symfony installer

I am using this cmd
php -r "file_put_contents('symfony', file_get_contents('https://symfony.com/installer'));"
output i am getting
Warning: file_get_contents(https://symfony.com/installer): failed to open stream
: No connection could be made because the target machine actively refused it.
in Command line code on line 1
i on proxy so that above cmd will not work directly .
I found some solution on stack overflow
Install Symfony on Windows behind proxy server with NTLM authentication
but its not working for me
I tried setting http_proxy and https_proxy env variable but it dint worked for me
Are there any chances of problem because of ssl ?
Can you access https://symfony.com/installer in your browser? If you can, it'll download a PHAR file for you. This file is ready to be used. You can put this in your "projects" directory and use it as:
> php symfony.phar new blog
Or you can move it to a location that's in your PATH and create a bat executable file:
#echo off
php "C:\path\to\symfony.phar" %*
Now you can use it like this:
> symfony new blog

Categories