Run Cron job with local php.ini file - Laravel 5.8 - php

I have created a cron job with Laravel. The problem is I am using file_get_contents in it and when I run the job, there is an error:
file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0
I know that in the command I need to specify and the local php.ini file, where allow_url_open is enabled.
I am using shared hosting, where in global php.ini file this property is disabled, but in my local is enabled. When I run the job it uses global php.ini.
I am running the job in SSH: - php artisan my_command.
I need to run it and manually in the code, too, like:
Artisan::call('my_command');
I have created the command with:
php artisan make:command MyCommand
Then in handle method, I am using file_get_contents.
How to speecify the php.ini file when I run a Task with Laravel 5.8 ?
EDIT:
On the hosting, this option is enabled and I can use it in my code.
If I run phpinfo(), I see that is enabled, too, because, it is used local php.ini.

The solution wasto make an alias to my php.ini file.
This file is located in:
/home/<USERNAME>/php73-fcgi.ini
Global php.ini file location:
/opt/cpanel/ea-php73/root/usr/bin/php
The command that I have executed in SSH:
alias php='/opt/cpanel/ea-php73/root/usr/bin/php -c /opt/cpanel/ea-php73/root/usr/bin/php -c /home/<USERNAME>/php73-fcgi.ini'
73 is the version of PHP.

Related

How to safely change PHP session.save_path permissions to allow built in server work

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.

PHP can not perform soap call from laravel command

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

What php.ini file uses symfony server:run command?

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)

artisan commands in laravel 4 not able to load php_mcrypt

I have been able to setup my laravel but everytime i try executing an artisan command on the command line i get this error
"unable to load dynamic library. " C:/wamp/bin/php/php5.4.12/ext/php_mcrypt.dll" - the specified module could not be found
From phpinfo() it is said that mcrypt is enabled
I have tried
adding "extension=php_mcrypt.dll" to my php.ini file as suggested on similar questions but it did not work
what can i do to solve this problem ?
Its because your PHP CLI is runing a different config to your PHP which your web server is running.
You need to update both php.ini files to load php_mcrypt.
/etc/php5/apache2/php.ini
apache2/php.ini
and maybe
cli/php.ini

Running PHP script from the command line

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

Categories