Many developers assume PHP requires Apache due to historical LAMP stack conventions, but PHP can run independently as a CLI (Command Line Interface) tool or via other web servers like Nginx. The key is installing the core PHP package without Apache modules.
First, update your package lists:
sudo apt-get update
Then install the minimal PHP5 CLI package:
sudo apt-get install php5-cli
For development purposes, you might want common extensions:
sudo apt-get install php5-common php5-mysql php5-curl php5-gd
Create a test file (test.php) with:
<?php
phpinfo();
?>
Run it via CLI to confirm:
php test.php
If using Nginx, install PHP-FPM:
sudo apt-get install php5-fpm
Configure Nginx to process PHP files by adding to server block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Avoid these mistakes:
- Installing
libapache2-mod-php5
(this pulls in Apache) - Assuming CGI mode requires Apache
- Not configuring alternative web servers properly
Standalone PHP5 typically uses less memory than Apache-module installations. Benchmark with:
ab -n 1000 -c 100 http://yourserver/test.php
Many developers assume PHP requires Apache or other web servers, but PHP can run as a standalone CLI (Command Line Interface) interpreter. This separation is particularly useful for:
- Writing standalone PHP scripts
- Running background jobs/cron tasks
- Testing code without web server overhead
- Building command-line tools
For Ubuntu systems, you have two primary approaches:
Method 1: Using apt-get (Recommended)
sudo apt-get update
sudo apt-get install php5-cli php5-common
Method 2: Compiling from Source
wget http://php.net/distributions/php-5.6.40.tar.gz
tar -xvzf php-5.6.40.tar.gz
cd php-5.6.40/
./configure --prefix=/usr/local/php5 --without-apache
make
sudo make install
Check your PHP CLI version:
php -v
Create a test script:
echo '' > test.php
php test.php
Edit php.ini for CLI-specific settings:
sudo nano /etc/php5/cli/php.ini
Key parameters to consider:
memory_limit = 256M
max_execution_time = 300
error_reporting = E_ALL
display_errors = On
Running Scheduled Tasks
# Add to crontab -e
* * * * * /usr/bin/php /path/to/your/script.php
Creating CLI Tools
#!/usr/bin/php
\n";
exit(1);
}
echo "Hello, {$argv[1]}!\n";
?>
Make it executable:
chmod +x hello.php
./hello.php "Developer"
- Missing Extensions: Install with
sudo apt-get install php5-{extension-name}
- Path Issues: Verify PHP location with
which php
- Permission Problems: Run scripts with proper user privileges