PHP5 - Fails to resolve hostnames when not in interactive mode - php

I'm working with an OS X 10.6 Server running Apache2 / PHP5 and having a problem with PHP not resolving hostnames when 'fopen()' tries to retrieve a file from a remote server. When run in interactive mode on the command line 'fopen()' works perfectly. However, when run through the web it will always fail with the error:
failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
I'm at a loss in finding the source of this problem: 'fopen()' works on the web when given an IP address instead of a hostname; 'gethostbyname()' also fails when run on the web (it doesn't error, it just returns whatever hostname it was given to resolve) but also works fine when run in interactive mode. The only exception seems to be 'dns_get_record()' which works fine when run on the web or in interactive mode.
I've been trying to find DNS problems on the server but dig, nslookup, and ping all work and "scutil -r" says the remote server is reachable with the current DNS settings. Any ideas on where the problem might be?

Try to recompile PHP with '--disable-ipv6' option. Then, apachectl stop & start (not 'apachectl restart'). I've got the same error message on Mac OS 10.6.4 + manually compiled PHP5.3.3 + Apache2.2.16.

You must set properly the allow_url_fopen value of your php.ini.
http://ar.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Consider the CLI uses a different php.ini that the webserver (I assume you are using Apache).
Another option may be you are not including libnss_dns
Try adding to your httpd configuration (correct path as needed for you system):
LoadFile /lib/libnss_dns.so

If you are are running PHP-FPM with chroot settings (as you should be) - then neither DNS nor mail will work.
Update Feb 2016
strace using gethostbyname only from the test-script # https://knzl.de/setting-up-a-chroot-for-php/ solved dns for me in an Alpine Linux / nginx chroot. I also use the sh from this link with mini-sendmail to solve the mail problem.
An approximate list of files needed for DNS & Mail in a chroot
The following commands got DNS working for me with PHP5.5 on Debian (tested with the Joomla builtin update component):
#!/bin/sh
chroot=/var/www
domain=mydomain
webuser=www-data
webgroup=www-data
cd $chroot/$domain
mkdir etc
cp /etc/resolv.conf etc/
cp /etc/nsswitch.conf etc/
echo $(cat /etc/group|grep $webgroup) > etc/group
echo $(cat /etc/passwd|grep $webuser) > etc/passwd
cp /etc/services etc/
cp /etc/protocols etc/
cp /etc/host.conf etc/
cp /etc/hosts etc/
cp /etc/networks etc/
mkdir -p usr/bin
mkdir usr/sbin
cp /usr/sbin/ssmtp usr/sbin/
cp /usr/sbin/sendmail usr/sbin/
mkdir usr/share
cp -rf /usr/share/zoneinfo usr/share
mkdir lib
cp /lib/i386-linux-gnu/libresolv.so.2 lib/
cp /lib/i386-linux-gnu/libnss_compat.so.2 lib/
cp /lib/i386-linux-gnu/libc.so.6 lib/
cp /lib/i386-linux-gnu/libnsl.so.1 lib/
cp /lib/i386-linux-gnu/libnss_files.so.2 lib/
cp /lib/i386-linux-gnu/libz.so.1 lib/
cp /lib/i386-linux-gnu/libdl.so.2 lib/
cp /lib/i386-linux-gnu/libcidn.so.1 lib/
cp /lib/i386-linux-gnu/ld-linux.so.2 lib/
cp /lib/i386-linux-gnu/libcrypt.so.1 lib/
cp /lib/i386-linux-gnu/libnss_nis.so.2 lib/
cp /lib/i386-linux-gnu/libnss_dns.so.2 lib/
chown -R $webuser:$webgroup lib usr etc
service php5-fpm restart
I did not need /bin/sh or /usr/lib/locale

Same problem, different solution then the original poster. I was calling mysqli(...) to connect, but the URL I had for the server was wrong. So it doesn't always mean you are unable to do any DNS lookup, just that the server that it is not able to do that specific lookup (check your host!)

What is the hostname you tried to resolve? I had the same problem with localhost.
This didn't worked and I got your Error. I changed the hostname to 127.0.0.1 than it worked. Not the best solution, but good for a workaround.

Disable IPv6 in your TCP/IP configuration in net preference.

Related

ORA-28547 with php-fpm in docker container

I got a working php-fpm docker container acting as the php backend to a nginx frontend. What I mean by working, is that it renders phpinfo output in the browser as expected.
My php-fpm container was produced by php-fpm-7.4 prod of the devilbox docker repo. It has OCI8 enable.
The issue: I keep getting ORA-28547 when trying oci_connect
What I have done:
1--add /usr/lib/oracle/client64/lib to a file inside ld.so.conf.d and run ldconfig -v
2--restart docker container.
3-- Now phpinfo shows ORACLE_HOME=/usr/lib/oracle/client64/lib
4--Add tnsnames.ora to /usr/lib/oracle/client6/lib/network/admin (there is a README.md file inside that folder that even tells you to do that)
5--Restart docker container again.
6-oci_connect still fails with the same error.
What I am missing?
Thank you very much for any pointers, I think I have browsed to the end of the internet and back without finding a solution yet.
----SOLUTION: reinstall instantclient, relink libraries (ldconfig) to use new instantclient libraries. Create modified dockerfile to do it when container is created.
I modified the Dockerfile file of the php-fpm to add new instant client files and not the one that were provided by the original file. I was not able to make it work with them. I have tried a few times rebuilding the image (docker-compose up --build) and this is the file that does the trick:
FROM devilbox/php-fpm:7.4-work
#instantclient.conf content: /opt/instantclient
RUN echo "/opt/instantclient" >/etc/ld.so.conf.d/instantclient.conf
WORKDIR /opt
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-sqlplus-linux.x64-19.8.0.0.0dbru.zip
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-sqlplus-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
RUN mv instantclient_19_8 instantclient
ADD tnsnames.ora /opt/instantclient/network/admin
RUN ldconfig -v
CMD ["php-fpm"]
expose 9000
# Insert following to .bash_profile or .profile of the User starting the php-fpm
export ORACLE_HOME=/usr/lib/oracle/client64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
export PATH=$PATH:$ORACLE_HOME/bin
export TNS_ADMIN=$ORACLE_HOME/network/admin
# Test to Ping Remote Db to be connected by PHP
tnsping <tns-name of remote DB - i.e. db12c.world>
# restart here the php Engine
Can you please check
https://github.com/caffeinalab/php-fpm-oci8/blob/master/Dockerfile
which seems to create a p-fpm-oci8 docker image
the "wget" for
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-basic-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local &&
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-sdk-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local &&
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local && \
can be dropped when you place downloaded instant client files into local host dir
/usr/local
and extract them - resulting in
/usr/local/instantcient_12_2
or 18, 19c equivalents
the 4 "ln" commands have to be adjusted to reflect the local host instantclient dir
the tnsnames.ora for instantclient is available from host by VOLUME command
-------------FINAL SOLUTION------------(it was not network related, I had done a couple of changes to the files, and also tried a different database, all at the same time, so it made me think that it was the different database what fixed the issue)
After many trial and errors, I came up with a Dockerfile that creates the correct configuration of files and connects without any issues to the database:
--Dockerfile: (to build php-fpm 7.4 using devilbox image)
Final solution:
I modified the Dockerfile file of the php-fpm to add new instant client files and not the one that were provided by the original file. I was not able to make it work with them. I have tried a few times rebuilding the image (docker-compose up --build) and this is the file that does the trick:
FROM devilbox/php-fpm:7.4-work
ADD instantclient.conf /etc/ld.so.conf.d/
WORKDIR /opt
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-sqlplus-linux.x64-19.8.0.0.0dbru.zip
RUN wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-sqlplus-linux.x64-19.8.0.0.0dbru.zip
RUN unzip instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
RUN mv instantclient_19_8 instantclient
ADD tnsnames.ora /opt/instantclient/network/admin
RUN ldconfig -v
CMD ["php-fpm"]
expose 9000
That's why I have suggested to use tnsping - unfortunaly it is not included in any of the instant client files which is a pity - so you have to pick it up from regular client with matching OS, bitsize and Oracle release. As workaround you could place SQL*Plus package files into container and try to connect with a foo user like
sqlplus foo/foo#\<ip>:\<port>/\<dbname>
which should generate an error - if
user/password not matching - ORA-1017 i.e. DB & listener running
listener running - ORA-1034 i.e. DB down
listener down (no return, or TNS-Errors)
I got it!. It was a firewall issue. I launched a tcpdump capture
session and there was nothing wrong with php-fpm, oci8 and
instantclient libraries. The traffic was initiated but there was no
response from the database. I made it work against a different
database where this box has no firewall issues.
I now will try rebuilding the docker image so I can see what I have to
manually add if any.
That was incorrect (the firewall as the origin of the problem). Rebuilding the docker file showed me where I had it wrong. See original question for solution.

Setting up Laravel 5.1 on Digital Ocean Ubuntu

I'm following this guide on setting up Laravel 5.1 on Ubuntu (LAMP stack) for Digital Ocean. When I try to access my Laravel app on the one-click droplet, I get:
I went through the steps of installing Composer then Laravel, and then placed ~/.composer/vendor/bin directory in my PATH "so the laravel executable can be located by your system."
root#phpmyadmin-512mb-nyc3-01:/# cat ~/.bashrc:
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
export PATH="$PATH:$HOME/.composer/vendor/bin"
Then follow this guide on changing my webroot so I can serve from /public like Laravel expects:
nano /etc/apache2/sites-enabled/000-default.conf
I change DocumentRoot /var/www/html/ to DocumentRoot /var/www/html/public
Then restarted sudo systemctl restart apache2
I cannot access my Laravel app. It gives a 500 error. Why is this?
PHP Fatal error: Uncaught UnexpectedValueException: The stream or
file "/var/www/html/storage/logs/laravel-2017-05-17.log" could not be
opened: failed to open stream: Permission denied in
/var/www/html/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:107\nStack
trace:\n#0
/var/www/html/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(106):
Monolog\Handler\StreamHandler->write(Array)\n#1
/var/www/html/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37):
Monolog\Handler\RotatingFileHandler->write(Array)\n#2
/var/www/html/vendor/monolog/monolog/src/Monolog/Logger.php(336):
Monolog\Handler\AbstractProcessingHandler->handle(Array)\n#3
/var/www/html/vendor/monolog/monolog/src/Monolog/Logger.php(615):
Monolog\Logger->addRecord(400, Object(UnexpectedValueException),
Array)\n#4
/var/www/html/vendor/laravel/framework/src/Illuminate/Log/Writer.php(202):
Monolog\Logger->error(Object(UnexpectedValueException), Array)\n#5
/var/www/html/vendor/laravel/framework/src/Illuminate/Log/Writer.php(11
in
/var/www/html/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php
on line 107
Do you have access to the error log of apache? (or, in case you've defined a custom log file, then check in there). A 500 error will typically leave an entry in the apache log file (/var/log/apache2/error.log) or your custom error log.
In case the error is a permissions issue in writing to "laravel.log", there can be multiple reasons:
You haven't given write permissions to your storage or bootstrap/cache directories. Try that.
cd /var/www/html/<projectname>
chmod +777 -R storage
chmod +777 -R bootstrap/cache
You will need to be root to use chmod
SELinux does not allow your http user to write to these files (for example, on CentOS). use the following commands from within your application's base directory (the directory that contains your 'app' folder, as well as storage and bootstrap folders:
chcon -R -t httpd_sys_rw_content_t storage
chcon -R -t httpd_sys_rw_content_t bootstrap/cache
In case its SELinux, and your application plans on connecting to MySQL as well, you will also need to run :
setsebool -P httpd_can_network_connect_db 1
This is to allow httpd to connect to database. Some locations may try to dissuade the usage of SELinux and tell you to turn it off as a whole, but that is not recommended.

Symfony2 NGINX Unable to create the cache directory

I know this may seem as a repeated question but so far I have 4 hours digging around unable to find a straight answer to this. I have setup a brand new CentOS 7 and I installed NGINX (https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7) + PHP 5.6 (https://webtatic.com/packages/php56/) and MariaDB.
Following the guides I was able to make the NGINX serve PHP files successfully.
I have a project in Symfony2 which I am trying to get in this server, my problem is when I try to open up http://server/web/app.php. I get the following error:
Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory (/usr/share/nginx/html/app/cache/prod) ' in ................
I read in the documentation (http://symfony.com/doc/current/book/installation.html) that I should work around my permissions so I was able to apply the following:
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
Since I have CentOS 7 from scratch that was the option that I was able to apply. However, I still have the same issue, after this approach these are the other options I've tried:
changed the owner for app/cache and app/logs to nginx:nginx
changed the permissions to 777 to the above folders
verified and re-verified that PHP is running under the nginx user, I had to change this in the www.conf
if I do ls -Al it shows that app/cache and app/logs are owned by nginx
restarted services after each single change
restarted the server completely
cleared the cache and performed warmup and still
Tried the umask option provided by symfony and still.
So after all these options I still get the same issue, to other people out there with this problem it's simply following the Symfony2 guidelines for applying the ACL and puff, done. But this is not the case.
What am I missing?
Ps: I double checked the permissions of the folder..and I am in the server as root.
Update 1
I re-installed CentOS 7 from scratch and followed https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7 and in combination with Symfony's suggestion for the configuration file and still I get the " Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory" error
You might be running into SELinux issues, since CentOS 7 ships with SELinux running by default.
You can test by executing setenforce 0 to see if that solves the problem. If that works, you need to configure SELinux to allow the nginx user to write to the cache directory, and then re-enable SELinux. SELinux does provide some good security, so it would be best to figure out how to configure it properly, as opposed to turning it off completely.
You need to add a local custom rule (policy module), suppose you have your application under the /usr/share/nginx/www directory:
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/www/.+/app/(cache|logs)(/.*)?'
You need to restore the contexts for all the files in that dir:
restorecon -Rv /usr/share/nginx/www/your-app-dir
Change the group of the cache and logs directories and add permissions for the group:
chgrp -R apache /usr/share/nginx/www/your-app-dir/app/{cache,logs}
chmod -R 775 /usr/share/nginx/www/your-app-dir/app/{cache,logs}

phpMyAdmin "Cannot load or save configuration"

I have been trying to setup phpMyAdmin on a macbook pro running yosemite 10.10.2. I have created a config folder in phpmyadmin and have given it the permissions required:
chmod o+wr ~/Sites/phpmyadmin/config
However, when I then go onto "localhost/phpmyadmin/setup" I get an error:
Cannot load or save configuration
Please create web server writable folder config in phpMyAdmin top level
directory as described in documentation. Otherwise you will be only able to
download or display it.
(I have tried attaching an image, but can't due to my reputation points)
I have tried resetting the permissions, tried deleting and recreating the folder. Tried redownloading the phpmyadmin zip but nothing seems to work.
Could anyone kindly advise me what I am doing wrong and how I am best placed to solve this issue?
I have had similar issue on my Ubuntu 16.04. I made a research and in the end I found a resolution of the issue. Maybe my case solution will help somebody else.
Background: For security reasons I have non privileged user and group apache:apache (sudo groupadd apache | useradd -g apache apache). They are preset by directives (User apache; Group apache) in /etc/apache2/apache2.conf. This user apache:apache owns Apache2 main directory (sudo chown -R apache:apache /etc/apache2) and some other files, for example: sudo chown -R apache:apache/etc/phpmyadmin/htpasswd.setup
In this manual: http://docs.phpmyadmin.net/en/latest/setup.html - I found that...
Debian and Ubuntu have changed way how setup is enabled and disabled,
in a way that single command has to be executed for either of these.
To allow editing configuration invoke:
/usr/sbin/pma-configure
To block editing configuration invoke:
/usr/sbin/pma-secure
Note! In the content of the two files listed above we talk about /var/lib/phpmyadmin/config.inc.php instead of /etc/phpmyadmin/config/config.inc.php. It was the key.
In my case I was modified the content of these scripts (see below) and now I can use localhost/phpmyadmin/setup properly.
/usr/sbin/pma-configure:
#!/bin/sh
echo "Unsecuring phpMyAdmin installation..."
echo "Setup script can now write to the configuration file."
echo
echo "Do not forget to run /usr/sbin/pma-secure after configuring,"
echo "otherwise your installation might be at risk of attack."
sudo sudo chown -R apache:apache /var/lib/phpmyadmin/config.inc.php
chmod 0660 /var/lib/phpmyadmin/config.inc.php
/usr/sbin/pma-secure:
#!/bin/sh
echo "Securing phpMyAdmin installation..."
echo "Setup script won't be able to write configuration."
sudo sudo chown -R root:root /var/lib/phpmyadmin/config.inc.php
chmod 0640 /var/lib/phpmyadmin/config.inc.php
I was able to use phpMyAdmin in my ~/Sites directory and remove the warning by giving the config folder writable access as such:
chmod 756 ~/Sites/phpmyadmin/config
Does it work if you try setting up PHPMyAdmin in system root versus user root? On OSX that server web root should be under /Library/WebServer/Documents?
I used this guide when I set mine up, and it works fine, although I did not use Sites as my root.
http://www.dingendoen.com/osx-installs-configuration-examples/install-apache-mysql-php-on-osx-yosemite/
For local development, changing permissions worked for an OSX Sierra install:
sudo chown -R _www:_www ~/Sites/phpmyadmin

Symfony2 Installation problems

I'm new to Symfony2 and I'm trying to create a new project in my local environment following the steps listed in the Symfony book:
Download
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
Create Project (path relative to /var/www/)
$ php composer.phar create-project symfony/framework-standard-edition grupo76/final/ Symfony 2.5.*
1st Problem: I have to add /web/ to paths
So, now I have to test the configuration by hitting
http://localhost/grupo_76/final/config.php
I get a 404, and have to change the address to
http://localhost/grupo_76/final/web/config.php
It complains about permissions on /app/logs and /app/cache but that's ok. I ran
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
2nd Problem: app_dev.php is completely broken
Then, the book suggest I should hit
http://localhost/grupo_76/final/app_dev.php
but, again, I need to add /web/
http://localhost/grupo_76/final/web/app_dev.php
The page renders, but full of errors like this No route found for "GET /"
Please, see attachment.
I'm running:
Apache2
PHP 5.5.9-1ubuntu4
Ubuntu 14.04
1st Problem
You have to configure a web server. There is an official documentation for configuring web server for both Apache and Nginx. What you are doing is accessing app_dev.php or config.php from your localhost host relatively. See: http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
2nd Problem
You have to define routing for your / path (homepage, in other words). See: http://symfony.com/doc/current/book/routing.html
A virtual-host won't help you, I already tried that, my working solution is:
1- Delete the old Symfony installation
2- Install a fresh one as root (sudo su)
3- Grant permissions to /var/www/html with chmod 777 -R /var/www/html"
Just take in count that you may want to readjust the folder's permissions when you get it working, to your specific scenario.

Categories