Apache+mod_php so slow - php

I have a problem setting up my Apache server with mod_php. I have a website whose index.php page is generated in ~200ms. But Apache serves it in about 1.5sec. Why is this slowdown taking place?
If needed, my /etc/apache2/ports.conf file is:
Listen 80
<VirtualHost *:80>
ServerName mysite.local
ServerAlias www.mysite.local
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:9999/
ProxyPassReverse / http://localhost:9999/
</VirtualHost>
My file /etc/apache2/sites-available/mysite.local.conf is:
Listen 9999
User max
Group max
<VirtualHost *:9999>
ServerName 127.0.0.1:9999
DocumentRoot /home/max/www/mysite.local/www/html/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/max/www/mysite.local/www>
Options FollowSymLinks
AllowOverride All
#Order allow,deny
#Allow from all
Require all granted
</Directory>
ErrorLog /home/max/www/mysite.local/logs/error.log
LogLevel warn
CustomLog /home/max/www/mysite.local/logs/access.log combined
</VirtualHost>
My main question is why is this so slow and how can make it faster? I have tested putting the index.php content into a plain html file, and it is served in ~10ms. so the problem is probably with mod_php? Thanks in advance.

A reason could be that apache checks for .htaccess files recursively within each directory of the root for each request. apache is used very widely but not very fast or even optimized.
I definitely would suggest an nginx server combined with php-fpm. The configuration is very straight forward and if you're familiar with apache, you should not have problems using nginx. How To: https://tecadmin.net/setup-nginx-php-fpm-on-ubuntu-20-04/

Related

How can i configure apache server to Proxy to http://localhost:3000/ and exclude some URL?

I want my Laravel to use Client side Nuxt JS and use Laravel as backend admin-panel and api(s).
This is my code to proxy Laravel project to Nuxt JS but it's not properly working.
<VirtualHost *:80>
ServerName nuxt.local
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/nuxt/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPassMatch /^(admin-panel)(.*)$ !
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyPreserveHost on
LogLevel debug
</VirtualHost>
I want to exclude "admin-panel" and "api" routes from hitting the proxy.
You might want to locate your httpd.conf inside your apache directory. Then, make an original copy and keep it.
Then, search for VirtualHost, you might find something similar to:
<VirtualHost *:3000>
ServerAdmin localhost
DocumentRoot "/var/www/html/nuxt/public"
ServerName localhost
ProxyRequests On
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
<Directory "/var/www/html/nuxt/public">
Options Indexes FollowSymLinks MultiViews
MultiviewsMatch Any
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Make the changes, however you wish. You might want to make sure, this is correct before you add:
ProxyPassMatch /^(admin-panel)(.*)$ !
Maybe, you want something like this:
ProxyPassMatch ^(admin-panel)(.*)$ http://localhost:3000/
After each changes in httpd.conf, you might want to restart apache and test to see if it would work.
I have solved the problem
I am posting my apache2 config code. Might be this can be helpful for any.
<VirtualHost *:80>
ServerName blog.net
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/blog/public
<LocationMatch "/">
allow from all
Satisfy any
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</LocationMatch>
<LocationMatch "/admin-panel/*">
allow from all
Satisfy any
ProxyPass http://localhost/blog/public
ProxyPassReverse http://localhost/blog/public
</LocationMatch>
<LocationMatch "/admin/*">
allow from all
Satisfy any
ProxyPass http://localhost/blog/public
ProxyPassReverse http://localhost/blog/public
</LocationMatch>
<LocationMatch "/api/*">
allow from all
Satisfy any
ProxyPass http://localhost/blog/public
ProxyPassReverse http://localhost/blog/public
</LocationMatch>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and in laravel routes/web.php file write the
\URL::forceRootUrl(env('APP_URL'));
to use project URL not proxy URL
Instead of using the ProxyPassMatch directive try just excluding each url from your proxy pass:
ProxyPass / http://localhost:3000/
ProxyPass /admin-panel !
ProxyPass /api !
There is probably a way to combine them into one line with regular expression as well, something like: ProxyPass ^/(admin-panel|api)(.*)$ !

Apache Gives me 403 Error when trying to access "/" on server

When I try to change the web root path to any other folder it give me a 403 error (Forbidden), The default path is /Library/WebServer/Documents, but since this rather annoying for me, I changed it too /Users/Xero/WebServer
Permissions I got the permissions from /Library/WebServer/Documents by right clicking and selecting Get info (http://i.imgur.com/f21D7z0.png What it looks like)
So I changed it to be the exact same for /Users/Xero/WebServer. But it give me a 403 error still.
Attempt to fix
So, I looked it up on Google, and I tried these solutions to fix my problem:
http://www.cyberciti.biz/faq/apache-403-forbidden-error-and-solution/
Error message "Forbidden You don't have permission to access / on this server" (Stack Overflow)
But they didn't help.
Other Notes
Remember I'm using the preinstalled Apache on OS X Snow leopard. And, my virtual host isn't local, so it's not part or the hosts file issue (It's hosted off my DNS, and my IP as a backup, neither work)
My Virtual Hosts:
<VirtualHost *:80> ( Edited 2 )
DocumentRoot "/Users/Xero/WebServer"
</VirtualHost>
<VirtualHost *:80>
Options Indexes FollowSymLinks Includes ExecCGI
DocumentRoot "/Users/Xero/WebServer"
ServerName wrks.tk
ErrorLog "/Logs/Workarea/wrks-err"
CustomLog "/Logs/Workarea/wrks-acc" common
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
<Directory "/Users/Xero/WebServer">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Xero/WebServer"
ServerName 209.169.203.53
ErrorLog "/Logs/Workarea/ip"
CustomLog "/Logs/Workarea/ip-acc" common
<Directory "/Users/Xero/WebServer">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Also, I have my httpd.conf User and Group setup like this:
User _www
Group _www
Responses to Answers
Edits
So, I fixed the path's to the document's, but it still giving me the same error, 403.
I checked my FireWall settings and it's off. I also checked my `Sharing Preferences`, `File Sharing` and `Web Sharing` are on.
I edited the duplicate of `DirectoryIndex Home.php Home.html` in the `wrks.tk` VirtualHost, but still, I'm getting the same error. (See Other Notes)
Added more too Other Notes.
Fixed directory, but still didn't fix problem.
Added another to Answer Responses.
Tried a computer restart, did not work.
I put a `index.html` in the folder, still got the same 403 error, I tried accessing `/index.html`, still, 403.
What file exactly are you getting all of this from?
<VirtualHost *:80> ( Edited 2 )
DocumentRoot "/Users/Xero/WebServer"
</VirtualHost>
<VirtualHost *:80>
Options Indexes FollowSymLinks Includes ExecCGI
DocumentRoot "/Users/Xero/WebServer"
ServerName wrks.tk
ErrorLog "/Logs/Workarea/wrks-err"
CustomLog "/Logs/Workarea/wrks-acc" common
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
<Directory "/Users/Xero/WebServer">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Xero/WebServer"
ServerName 209.169.203.53
ErrorLog "/Logs/Workarea/ip"
CustomLog "/Logs/Workarea/ip-acc" common
<Directory "/Users/Xero/WebServer">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
There are more than one file for those settings. If you have apache, then take a look in the sites-enabled and sites-available folders.
Change the bottom part of your httpd.conf file to:
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
Make a new file index.html and put the following in it:
<!DOCTYPE html>
<html>
<body>
<h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body>
</html>
Then place the index.html file into the empty directory.
create a file in /etc/apache2/users/ called yourusername.conf (yourusername being the account short name, e.g. hulu – it's usually the name of your home folder in /Users) with the following contents:
<Directory "/Users/yourusername/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Django/mod_wsgi WAMP with PHP

I followed this guide and managed to make Python with a Django installation work perfectly, but it seems to have rendered all the locally hosted PHP sites inaccessible returning a 404 error.
httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
#This is placed right after the rule for <Directory "f:/WAMP/www/">
<Directory "f:/WAMP/www/python">
Options ExecCGI
AddHandler wsgi-script .py
Order allow,deny
Allow from all
</Directory>
#This is placed at the end of the file
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
Include "f:/WAMP/alias/*"
Include "F:/WAMP/www/python/sandbox/apache/apache_django_wsgi.conf"
apache_django_wsgi.conf
Alias /python/images/ "F:/WAMP/www/python/sandbox/images"
<Directory "F:/WAMP/www/python/sandbox/images">
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias /python "F:/WAMP/www/python/sandbox/apache/django.wsgi"
<Directory "F:/WAMP/www/python/sandbox/apache">
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot f:/WAMP/www/python/sandbox/
ServerName 127.0.0.1
</VirtualHost>
django.wsgi
import os, sys
sys.path.append('F:/WAMP/www/python/sandbox')
os.environ['DJANGO_SETTINGS_MODULE'] = 'sandbox.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
PHP only renders when I comment out the lastline from httpd.conf.
You need to have another virtual host if you're going to set the DocumentRoot:
# This is for PHP
<VirtualHost *:80>
DocumentRoot f:/WAMP/www/
ServerName local.php.dev
</VirtualHost>
# This is for your Django stuff
<VirtualHost *:80>
DocumentRoot f:/WAMP/www/python/sandbox/
ServerName local.py.dev
</VirtualHost>
Otherwise, as it is now, all the local requests are being sent to the python sandbox.
Note that you'll need to have several hosts pointing locally.

Wamp Server: Multiple Virtual Hosts are not working on Windows

I have two virtual hosts on windows(for example: test1.dev and test2.dev). But it always load content of test1.dev for both virtual hosts.
Following are my files:
hosts:
127.0.0.1 localhost
127.0.0.1 test1.dev
127.0.0.1 test2.dev
httpd.conf:
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
Include "c:/wamp/alias/*"
<VirtualHost 127.0.0.1>
ServerName test1.dev
DocumentRoot "C:\wamp\www\test1\public"
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName test2.dev
DocumentRoot "C:\wamp\www\test2\public"
</VirtualHost>
Can someone recognize the problem ?
I'm guessing you're missing the NameVirtualHost 127.0.0.1:80 line somewhere :)
I did some thing like this
1- for the local host its :
NameVirtualHost localhost:80
<VirtualHost localhost:80>
ServerName localhost
ServerAlias localhost
DocumentRoot D:/wamp/www
ErrorLog "D:/wamp/www/error.log"
CustomLog D:/wamp/www/access.log common
<Directory "D:/wamp/www">
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
<IfModule mod_access.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
</VirtualHost>
2 - and for any other local domain
NameVirtualHost zf.local:80
<VirtualHost zf.local:80>
ServerName zf.local
ServerAlias zf.local
DocumentRoot D:/Workspace/Zend/documentation
ErrorLog "D:/Workspace/Zend/documentation/error.log"
CustomLog D:/Workspace/Zend/documentation/access.log common
<Directory "D:/Workspace/Zend/documentation">
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
<IfModule mod_access.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
</VirtualHost>
You don't have to write virtual host info into httpd.conf. Just uncomment line on which you load conf/extra/httpd-vhosts.conf, then go to this file and put your info there. Should work.
Example of my httpd-vhosts.conf:
NameVirtualHost *:80
<VirtualHost 127.0.0.1>
DocumentRoot "C:/wamp/www"
ServerName dev
</VirtualHost>
You need to include something similar to following line
NameVirtualHost *
Also, it seems you are using https connection to the server which doesn't play well with virtual hosts because of the SSL protocol limitation. The Host header in the http request is encrypted and by the time apache decrypts it, it has already passed on the request to one of the virtual host.
I changed
NameVirtualHost *:80
to
NameVirtualHost 127.0.0.1:80
and it works for me
Don't forget to check for lines like "Listen [::0]:80" in httpd.conf and add your ports there too, if NameVirtualHost doesn't work.
Hey guys I've written a very straight forward tutorial which includes the whole process. Let me know if you run into any problems in a comment.
http://www.kintek.com.au/web-design-blog/configuring-multiple-domains-within-wamp-for-local-development/

How do I run Django and PHP together on one Apache server?

I can currently run either Django through mod_wsgi or PHP on my Apache server.
My Django projects run at: http://localhost and source is at C:/django_proj
My PHP projects run at: http://php.localhost and source is at C:/web
If I turn both on, php.localhost and localhost go to the Django project. I've already set them up through Apache virtual hosts.
Here are some relevant lines in httpd.conf:
DocumentRoot "C:/web"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory "C:/web">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "C:/django_proj">
Order allow,deny
Allow from all
</Directory>
Include "C:/django_proj/apache/apache_django_wsgi.conf"
The relevant lines in apache_django_wsgi.conf is:
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Order allow,deny
Allow from all
</Directory>
Inside httpd-vhosts.conf:
<Directory C:/web>
Order Deny,Allow
Allow from all
</Directory>
<Directory C:/django_proj>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
</VirtualHost>
My PHP project is current inaccessible. Does anyone have any ideas what I'm missing?
I run dozens of mod_wsgi/Django sites, PHP sites, and a Rails site with a single Apache.
It's mostly done using virtual hosts but I have some that are running both on the same domain.
You just need to put your WSGIScriptAlias /... after any other Location/Alias directives.
Lets say, for example, I want to run phpMyAdmin on the same domain as a Django site. The config would look something like this:
Alias /phpmyadmin /full/path/to/phpmyadmin/
<Directory /full/path/to/phpmyadmin>
Options -Indexes
...etc...
</Directory>
WSGIScriptAlias / /full/path/to/django/project/app.wsgi
<Directory /full/path/to/django/project>
Options +ExecCGI
...etc...
</Directory>
Edit:
Your configuration should look something like this:
<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
Alias / C:/web
<Directory C:/web>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
You don't need those <Directory> directives in http.conf... do all your configuration in the Virtual hosts.
Also, completely get rid of the <Directory /> block.
Your WSGIScriptAlias / ... directive is telling Apache that everything request starting with "/" should get fed through Django's WSGI handler. If you changed that to read WSGIScriptAlias /django-proj/ ... instead, only requests starting with "/django-proj" would get passed into Django.
An alternative would be to start setting up virtual hosts for each project. This way you could configure Apache to put each project at the / of it's own domain, and you wouldn't need to worry about the configuration for one project affecting one of your other projects.
I had the same problem.
Try removing this block <Directory /> in httpd-conf.
Include httpd-vhost.conf and and try puting my WSGIScriptAlias / "/somewhere/file.wsgi" in virtual host section of httpd-vhosts which listens to port 80.
I would like to add that if you are using Apache ProxyPass, it's possible to deny certain URL patterns so that it falls to mod_php.
ProxyPass /wordpress !
<Location /wordpress>
Require all granted
</Location>

Categories