How to Run Laravel Spark with Stripe in a Subdirectory (domain.com/subscribe) on a Live Server
How to Run Laravel Spark with Stripe in a Subdirectory
How to Run Laravel Spark with Stripe in a Subdirectory (domain.com/subscribe
) on a Live Server
Running Laravel Spark with Stripe in a subdirectory (domain.com/subscribe
) requires proper configuration in Laravel, Apache/Nginx, Stripe Webhooks, and Server Setup.
Step 1: Install Laravel Spark in the Subdirectory
If Laravel Spark is not installed, install it in your subdirectory (subscribe
):
cd /var/www/domain.com/
composer create-project --prefer-dist laravel/laravel subscribe
cd subscribe
composer require laravel/spark-stripe
Then, publish Spark's assets:
php artisan vendor:publish --tag=spark-config
php artisan vendor:publish --tag=spark-migrations
php artisan vendor:publish --tag=spark-assets
Set correct permissions:
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data /var/www/domain.com/subscribe
Step 2: Configure .env
for the Subdirectory
Edit your .env
file and update these values:
APP_NAME="Laravel Spark"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://domain.com/subscribe
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_spark
DB_USERNAME=root
DB_PASSWORD=yourpassword
STRIPE_KEY=pk_live_xxxxxxxxxxxxxxxxxxxxxx
STRIPE_SECRET=sk_live_xxxxxxxxxxxxxxxxxxxxxx
STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxxxxxxxx
Clear cache:
php artisan config:clear
php artisan cache:clear
Step 3: Update server.php
for Running in a Subdirectory
Modify Laravelβs server.php
in the subscribe directory:
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Step 4: Update Laravel's public/.htaccess
for the Subdirectory
Edit /var/www/domain.com/subscribe/public/.htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subscribe/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subscribe/index.php [L]
</IfModule>
This ensures Laravel processes requests correctly in domain.com/subscribe
.
Step 5: Configure Web Server (Apache or Nginx)
Apache Configuration
Edit Apache virtual host config:
nano /etc/apache2/sites-available/domain.com.conf
Add:
<VirtualHost *:80>
ServerName domain.com
DocumentRoot /var/www/domain.com
<Directory /var/www/domain.com/subscribe/public>
AllowOverride All
Require all granted
</Directory>
Alias /subscribe /var/www/domain.com/subscribe/public
</VirtualHost>
Then restart Apache:
sudo a2ensite domain.com.conf
sudo systemctl restart apache2
Nginx Configuration
Edit Nginx site config:
nano /etc/nginx/sites-available/domain.com
Add:
server {
listen 80;
server_name domain.com;
root /var/www/domain.com;
index index.php index.html;
location /subscribe {
root /var/www/domain.com/subscribe/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Restart Nginx:
sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Step 6: Set Up Database & Run Migrations
Create a database:
mysql -u root -p
CREATE DATABASE laravel_spark;
exit;
Run migrations:
php artisan migrate --seed
Step 7: Configure Stripe Webhooks
Run:
php artisan cashier:webhook --url="https://domain.com/subscribe/spark/webhook"
Verify webhook routes:
php artisan route:list | grep webhook
Ensure this route exists:
POST spark/webhook Spark\Http βΊ WebhookController@handleWebhook
Step 8: Exclude Webhooks from CSRF Protection
Since Stripe webhooks come from an external service, Laravel's CSRF protection might block them.
Edit app/Http/Middleware/VerifyCsrfToken.php
:
protected $except = [
'subscribe/spark/webhook',
];
Then, clear Laravel cache:
php artisan config:clear
php artisan cache:clear
Step 9: Run Laravel Queues & Scheduler
Stripe payments require queued jobs to process invoices.
Run queue worker:
php artisan queue:work --daemon
(Optional: Set up Supervisor for queue handling)
Set up cron job:
crontab -e
Add:
* * * * * cd /var/www/domain.com/subscribe && php artisan schedule:run >> /dev/null 2>&1
Step 10: Secure with SSL (Optional but Recommended)
If you have an SSL certificate, force HTTPS:
sudo certbot --apache -d domain.com
For Nginx:
sudo certbot --nginx -d domain.com
Step 11: Final Testing
Check Application Routes
php artisan route:list
Ensure webhook routes exist.
Check Laravel Logs for Errors
tail -f storage/logs/laravel.log
Test Webhook
curl -X POST -H "Content-Type: application/json" -d '{}' https://domain.com/subscribe/spark/webhook
Test Webhooks with Stripe
stripe trigger invoice.payment_succeeded
Check logs:
tail -f storage/logs/laravel.log
Summary
Laravel Spark is installed in a subdirectory (
domain.com/subscribe
).
Apache/Nginx is configured correctly to serve Laravel from the subdirectory.
Stripe Webhooks are configured to
https://domain.com/subscribe/spark/webhook
.
CSRF protection is disabled for Stripe webhooks.
Queue workers & cron jobs are set up for handling payments.
Laravel Spark is Now Running in Your Subdirectory!
Live site:
https://domain.com/subscribe
Admin Dashboard:
https://domain.com/subscribe/dashboard
Let me know if you need further troubleshooting!