How to Launch a PHP Script at Apache Startup

Is it possible to launch a php script at apache startup?

You need to modify you startup script for your apache.

Open your startup script, it should be in /etc/init.d/apache or apache2

Search for the start / restart section and add your cli call for your PHP script.

Example:

    restart)
[..]
php -q /tmp/myscript.php &
;;

Where /tmp/myscript.php is your php script that you want to launch.

The "&" at the end will start the script in the background so your startup will not wait until your php script has ended. If you want to wait until it has ended, remove the &.

You should not put such thing into your startup scripts, there might be better solutions. What are trying to achieve?

Launch php script at apache2 startup

You should not use apache to start your script, but follow your first idea of using an own init-script unless your php script depends on the existence of apache.

Just place a shell script callmyphp into /etc/init.d that calls the php interpreter and passes your php script as an argument like:

#!/bin/sh
/usr/bin/php -q /path/to/myphp.php

Don't forget to make your calling script executabel with chmod 755 /etc/init.d/callmyphp.

Then add your calling script via symbolic links to the desired run levels, i.e. by running update-rc.d callmyphp defaults

See also https://debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

How to run a script whenever Apache is started/restarted?

I have asked the same question at WAMP's forum and the answer was:

Hi,

Is it possible to launch a php script at apache startup?


No. Not with Wampserver.

The answerer was a moderator, therefore I have to believe that, unfortunately the answer is: no, that's not possible.

How to run a PHP script every time someone visits your site?

What you can do is to have php.ini autoprepend a script to each of your pages.

This should be a starting point for you:

# Prepend file to top of page
auto_prepend_file = '/yourpath/pre_header.php'

Note that php.ini is server related so this will affect all sites you host unless you do something like what is described here to have a php.ini for each site.

Regarding the content of the script and what to do with the information you are free to do whatever you want with that script. The pro is that you don't have to modify any file and this will be executed for any php page your server is serving

how to run php automatically on apache

A Cron Job is what you are looking for, mostly your hosting provide you with this service, if you are using a dedicated server, you can generate a cron here http://www.generateit.net/cron-job/ or here is a little tutorial for it: http://www.linuxweblog.com/crotab-tutorial



Related Topics



Leave a reply



Submit