I'm using virtphp to have separate environments (different PHP versions, extensions, etc).
When I use symfony's command to run a local development server:
php app/console server:run
It seems that it ignores the php.ini file of my virtual environment (~/.virtenv/envs/myenv/etc/php.ini), e.g.: does not load the extensions definided in that file.
But when I use the php built-in server directly, it works perfectly:
php -S 127.0.0.1:8000 --docroot=web/
What's the difference of those two commands or what does symfony do differently?
This is the output of the php --ini command:
Configuration File (php.ini) Path: /usr/local/etc/php/5.5
Loaded Configuration File: /Users/mjuarez/.virtphp/envs/wowfi/etc/php.ini
Scan for additional .ini files in: /Users/mjuarez/.virtphp/envs/wowfi/etc/php
Additional .ini files parsed: (none)
This is the output of the function phpinfo() in a Symfony controller when using the command php app/console server:run:
Configuration File (php.ini) Path /usr/local/etc/php/5.5
Loaded Configuration File /usr/local/etc/php/5.5/php.ini
Scan this dir for additional .ini files /Users/mjuarez/.virtphp/envs/wowfi/etc/php
Additional .ini files parsed (none)
Note the difference in "Loaded Configuration File"... when I use the php --ini command it replaces the "Loaded Configuration file" with the one in my php virtual environment and when I use the command php app/console server:run it uses the "global" configuration file.
For the record (and for all lost souls who arrive here by Google search like me):
Symfony console is not a program per se. It is just a PHP script. Therefore, when you run Symfony console, it doesn't have its own php.ini or extension directory or other configuration - It uses the same settings as all your other PHP scripts. (Unless overwritten at runtime - which actually might be the case of the original question.)
A workaround for this issue is to move the php.ini file of my virtual environment to a sub-directory called php inside the etc directory. e.g.:
Move the file ~/.virtphp/envs/my-env/etc/php.ini
To ~/.virtphp/envs/my-env/etc/php/php.ini
Yet another piece for the "lost souls" who arrive here - Symfony does overwrite at least the max_execution_time, when Symfony commands are called through the console. Check the console.php file, there's a "set_time_limit(0)" in line #12 or so.
Drop a file in your web directory with inside. Open it with your browser and search for php.ini that will give you the correct file. There could be several files yf you look directy in your configuration directory (cli, fpm, apache and so on)
Related
I have installed yii2 and I have tried to run using the PHP built in server using ./yii serve. When I open http://localhost:8080 I got the following permission error:
PHP Warning – yii\base\ErrorException session_start():
open(/var/lib/php/sessions/sess_t1462ivbhhq79igqjp6b4fekth, O_RDWR)
failed: Permission denied (13)
On other application on the same machine that it run on Apache, I fired up phpinfo() and I found that path /var/lib/php/sessions is set to session.save_path.
To work around the permission issue, I have tried to run sudo ./yii serve and it worked fine.
Now, I care about the security of my development system, where, occasionally, I expose it to the Internet to serve my clients visits to the currently developed apps. I don't know weather I keep running the app using sudo or is there a better way to change /var/lib/php/sessions permissions to allow php from the cli, php -S, to access it?
I have done the following to change the save session path to another accessible path by PHP cli:
From the terminal php --ini to get a list of configurations files and I meant by Loaded Configuration File entry to specify the path of the loaded php.ini file. In my case it is /etc/php/7.2/cli/php.ini.
Using any text editor that could be run as sudo, in my case I used kate, I opened the specified php.ini file and I search for: session.save_path entry of the php.ini and I uncommented it then change the path to a path in my home. In my case I set it to be /home/myuser/tmp/sessions. Save the file.
Now ./yii serve works fine without need to sudo and without need to change any settings of the php.ini for Apache.
I have made a Laravel command that I try to run with a cronjob every minute.
In the command there is some logic that does a soap call. When I run the command through a route that I created everything works fine.
like this:
Route::get('test-command', function() {
Artisan::call('updateRegisterLogs');
});
Now when I try to do the same thing in my command on the server like this:
php artisan updateRegisterLogs
I get the following error:
Class 'SoapClient' not found
Now I searched on the internet and in my php.ini I removed the ; before extension=php_soap.dll and before extension=php_openssl.dll and I've restarted my apache and unfortunately I get the same error.
I am now sure what is going wrong here since my code works but not when executed with artian.
Usually php has various php.ini configuration files, typically located on paths like, /etc/php/7.2/cli/php.ini or /etc/php/7.2/fpm/php.ini so from the problem you are having my guess is that you have uncommented the extension on one of those config files but not the cli path one.
If you have for example the application being served using nginx and phpfpm the php.ini file that will determine what modules are enabled when executing php for those requests will be the one located at /etc/php/7.2/fpm/php.ini. When you execute artisan commands the "selected" config file is the one at /etc/php/7.2/cli/php.init
I am running this Docker instance of a linux debian:jessie with php 5.6.
This is part of my phpinfo :
As we can see the php.ini should be located at
/usr/local/etc/php
And this is what I have inside /usr/local/etc/
But there is no php.ini inside it.
I the other hand, I have the php.ini inside
So, from where exactly is my php.ini being loaded?
We dont even have a php process running but the php seems to be ok - being displayed phpinfo in the screen.
A little late to the party but since question is still relevant today, let me add a short answer:
Official php:7 images get their settings from /usr/local/etc/php folder.
# First log into the running container
$ docker exec -it «container_name» /bin/bash
# List folder content
$ ls /usr/local/etc/php
# Which outputs following line
conf.d php.ini-development php.ini-production
If needed, modifying settings via conf.d folder seems better alternative, since xdebug uses it. For example, you can change upload size by adding uploads.ini to conf.d folder with the following content:
file_uploads = On
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600
Complete list of ini directives can be found at https://www.php.net/manual/en/ini.core.php
Let try it as an answer:
It does not exist at all, which means php will run the default options.
Look at your docker file, it starts from a "clean" OS, installs Apache and PHP in it. But it never copies the php.ini file from the PHP installation into /usr/local/etc/php. Actually in lines 31 and 32 it creates the conf.d directory but that is it.
So I would suggest, at the end of your docker file, add code to copy php.ini-production to /usr/local/etc/php.ini, and edits as required. Or use default options.
The default php.ini file that the docker php images look for is:
/usr/local/etc/php/php.ini
You can see this in the output from the phpinfo function (just run "php -a" in the container and then "phpinfo();" at the prompt):
Configuration File (php.ini) Path => /usr/local/etc/php
Loaded Configuration File => /usr/local/etc/php/php.ini
You can always link this file in as a volume to get a custom one when running the container with a -v option like:
docker run -v /local/path/to/php.ini:/usr/local/etc/php/php.ini [OPTIONS] IMAGE [COMMAND] [ARG...]
I typically prefer to use the default ini file that comes with it, with just a few modified options as I need them. If you want your container to do this during build, you can do something like the following in the Dockerfile:
RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini && \
sed -i -e "s/^ *memory_limit.*/memory_limit = 4G/g" /usr/local/etc/php/php.ini
The RUN commands above will copy the default production ini file, and then will modify the memory_limit and set it to 4G in the ini file.
I prefer this method because it allows custom configurations to be used so the container always works with defaults when it's pulled, but you still have the option to override the ini file in the container by passing a volume in.
Short answer is you don't need one. If you're missing or want to add extensions, you can do so in your Dockerfile by doing docker-php-ext-install or docker-php-ext-enable.
Most of the common ones, you can simply do enable, such as mbstring for example, but for some less common ones, you might have to run pecl first or something to get the package. Take a look at this Docker documentation page for more information on php extensions
If you are using something like wodby (docker4php or docker4drupal) or lando or trying to find an answer "why php.ini doesn't work" (like me), these tools are using their own way to pass configuration into php
https://github.com/wodby/php#php-and-php-fpm-configuration
How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?
I have a phpinfo.php file which is accessed from the web shows that German is installed. However, if I run the phpinfo.php from the command line using - php phpinfo.php and grep for German, I don't find it. So both PHP files are different. I need to run a script which the php on which German is installed.
How can I do this?
You should check your server configuration files. Look for lines that start with LoadModule php...
There probably are configuration files/directories named mods or something like that. Start from there.
You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.
To run php interactively:
(So you can paste/write code in the console.)
php -a
To make it parse a file and output to the console:
php -f file.php
Parse a file and output to another file:
php -f file.php > results.html
Do you need something else?
To run only a small part, one line or like, you can use:
php -r '$x = "Hello World"; echo "$x\n";'
If you are running Linux then do man php at the console.
If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:
SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"
Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.
On SUSE Linux, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (PHP configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.
If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the German module, while the the CLI php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.
You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.
Or, you could find the line that loads the German module in the Apache file and copy/paste just it to the CLI file.
I was looking for a resolution to this issue in Windows, and it seems to be that if you don't have the environment variables ok, you need to put the complete directory. For example, with a file in the same directory as PHP:
F:\myfolder\php\php.exe -f F:\myfolder\php\script.php
Brief introduction to the problem:
I need to load pdo_mysql to run command php app/console doctrine:database:create and other commands for Symfony 2.
I found a way to do this by running php -c "path/to/my/php.ini" app/console doctrine:database:create
Problem:
Since I don't want to add the path to my php.ini every time I run commands in PHP CLI, where/how can I set up Windows, so that every time I type php somecommand in console it will load my desired php.ini file?
Create a .CMD file which automatically runs PHP with the required options:
path/to/php.exe -c "path/to/php.ini" %1 %2 %3 % %5 %6 %7 %8 %9
and call it something like phpcli.cmd. Make sure it's on your search path and off you go. The only change you need to make is to run phpcli rather than php.
There are several ways to do it, but if you don't want to mess with an alias or making multiple copies of file php.ini, you can also set the PHPRC environment variable. I would think this is the recommended method to set it more "permanently".
More information in the PHP documentation:
The configuration file
In Windows, an easy way to do this is to go to the "System Properties" dialog; either right-click on "My Computer" and click "Properties", or use the "System" item in Control Panel, then go to "Advanced" settings, click "Environment Variables", and click "Add" for either the system or your user, call it "PHPRC" and copy the path to your .ini file in there ... for example, mine was in C:\MAMP\conf\php5.6.28.
(This was on Windows 7, and they changed some of the UI in different versions, but it's basically the same.)
You can verify it's working by doing php --ini from the command line. The output should be something like:
Configuration File (php.ini) Path: C:\Windows
Loaded Configuration File: C:\MAMP\conf\php5.6.28\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
You can also do echo %PHPRC% from the Windows command prompt, or echo $PHPRC from Cygwin/Bash/MinGW, etc. You will have to restart any existing terminal sessions for this to take effect, but in my experience it works for all three, since the bash environments also inherit the Windows environment variables.
What if you add path/to/your/php.ini to the path environment variable and then just run php -c "php.ini" app/console doctrine:database:create
Could you try that?