logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> How-to's & Documentation & Tips View previous topic :: View next topic
Reply to topic   Topic: A Fast, Reliable and Proven Setup :: Apache PHP MySQL
Author
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Sat 12 Apr '08 17:13    Post subject: A Fast, Reliable and Proven Setup :: Apache PHP MySQL Reply with quote

One of the biggest topics here at Apache Lounge is running PHP on Apache.
It is a topic that is constant and for some reason often confused.
Confused namely by varying ways to accomplish the same thing.
The following has been tested numerous times, and seems (at least so far) to be easy to follow.Mr. Green

IF YOU PREVIOUSLY INSTALLED APACHE USING A MSI FILE THEN (otherwise skip down to CONTINUE section):
I have not tested Apache Software Foundation (ASF) MSI installation applications lately,
but the last time I tested the MSI FAILED to remove to some things that can cause problems later on during
the Un-Install process. BEFORE you UN-INSTALL the MSI file, open a command prompt run as administrator and navigate to
the BIN folder of Apache. Now, uninstall the service:
Code:

httpd -k uninstall

Now that the service is removed, uninstall Apache and then ALL PHP entries


ArrowCONTINUE:

Install the Microsoft VC++ Redistributable

For 2.4.x VC16/VS17 , Win 64 bit (x64) 2.4 from http://www.apachelounge.com/download/


Apache Setup
Download httpd-2.4.x-winYY-VCxx.zip from http://www.apachelounge.com/download/

Note: you can substitue drive letters to what will work best for you or your situation.
I advise however, that you DO NOT place PHP in the Apache root though.


Unzip this ZIP file in a new folder called something like C:\Apache24. Then, read the file named Read Me First !!.txt.
It will tell you how to setup the service for Apache.
If you want or need ApacheMonitor you can create shortcuts however you like. Before installing ANYTHING else, ensure Apache will start.
Open a command prompt and navigate to the BIN folder of Apache:

    1. At the prompt enter httpd
    2. If there's nothing seriously wrong, Apache will start: the cursor will drop down one line and blink (you should be able to use your browser and navigate to http://localhost and see "It Work!".
    3. With focus on the Command Prompt window - Hold down CTRL hit the C key then release both - within a couple of seconds the prompt will be released (Apach will Stop).


After you validate that Apache starts, stop it before proceding.

PHP Setup

Note: you can substitue drive letters and folder names to what will work best for you or your situation. I advise however, that you DO NOT place PHP in the Apache root though.
If you are still with me, now download PHP 8.x from http://windows.php.net/download/ you want this file: php-8.x.xx-Win32-VC11/14/15-x86/64.zip (where x relates to the latest stable version number)

Yes there is a way to install PHP as module, but it has been proved best to install PHP over mod_fcgid (fastcgi, best practice)

Please download the VC11/14 x86/x64 Non Thread Safe OR VC14/VC15/VC16 x86/x64 Thread Safe.

Extract the contents of this file and move the contents to where you intend to use PHP such as C:\php8.

Navigate to the PHP folder find php.ini-production, rename it to php.ini and edit it. FIND the extension_dir directive and change so that it properly locates the EXT folder. Enter a full path.

extension_dir = "C:\php82\ext"

If you want to use the mysql extension you have to enable it un commenting it (removing ";") in that line of php.ini. A good start is ot enbale mysql and mysqli

Code:

extension=php_mysqli
extension=mbstring
extension=pdo_mysql


it is recommended to enable op cache. ( directly after all the other extensions)

Code:

zend_extension=php_opcache.dll


Below search for [opcache]

Code:

opcache.enable=On
opcache.cli_enable=On


If you need xdebug (optional, only for development!) make you set a full path and load it AFTER opcache. It needs to be the last one.
xdebug is not recommended for production use.

use the https://xdebug.org/wizard to download the correct version of xdebug for your PHP version.

example
Code:

zend_extension = C:\php82\ext\php_xdebug-3.2.0-8.2-vs16-x86_64.dll

[xdebug]
xdebug.enable = 1
xdebug.remote_enable = 1
xdebug.idekey = "PHPSTORM"
xdebug.profiler_enable = 0


Since version 3 it might look like

example
Code:

[xdebug]
zend_extension="C:/php8.0.6/zend_ext/php_xdebug-3.0.4-8.0-vs16-x86_64.dll"

;xdebug.mode allowed are : off develop coverage debug gcstats profile trace
xdebug.mode = debug
xdebug.max_nesting_level = 1000
xdebug.discover_client_host = true

xdebug.profiler_output_name = "cachegrind.out.%t.%p"
xdebug.output_dir ="C:/wamp64/tmp"
xdebug.show_local_vars=0


Redis as session Handler (instead of files) // this is optional

Download the DLL from http://pecl.php.net/package/redis

Put that dll into the ext folder. e.g. C:\php81\ext

in your php.ini load the redis extension. This must be done before loading opcache!

Code:

extension=redis


Code:

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"


https://github.com/microsoftarchive/redis/releases

hide PHP from apache response headers

Code:

expose_php = Off



Fix the path info in the scripts

Code:

cgi.fix_pathinfo=1



Set your timezone. Find your zone at http://php.net/date.timezone

Code:

date.timezone = "Europe/Berlin"


Save the file as php.ini.

Ensure you can run PHP from the Windows Command Line (
Code:
php -m
should work and produce a list of modules loaded).

Finally, this should be the last step, edit the Apache CONF file (httpd.conf) with
make sure you load mod fcgid. ( mod_fcgid is an additional download)
Code:

LoadModule fcgid_module modules/mod_fcgid.so

<IfModule fcgid_module>


   FcgidMaxProcesses 300
   FcgidMaxProcessesPerClass 300

   FcgidOutputBufferSize 65536
   FcgidConnectTimeout 10
   FcgidProcessLifeTime 0
   FcgidMaxRequestsPerProcess 0
   FcgidMinProcessesPerClass 0
   FcgidFixPathinfo 0
   FcgidProcessLifeTime 0
   FcgidZombieScanInterval 20
   FcgidMaxRequestLen 536870912
   FcgidIOTimeout 120
   FcgidTimeScore 3

   FcgidPassHeader Authorization

   FcgidInitialEnv PHPRC "C:\\php82"
   FcgidInitialEnv PATH "C:\\php82;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"
   FcgidInitialEnv SystemRoot "C:\\Windows"
   FcgidInitialEnv SystemDrive "C:"
   FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"
   FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"
   FcgidInitialEnv windir "C:\\WINDOWS"
   <Files ~ "\.php$">
      Options Indexes FollowSymLinks ExecCGI
      AddHandler fcgid-script .php
      FcgidWrapper "C:/php82/php-cgi.exe" .php
   </Files>
</IfModule>


- php_value name value
- php_admin_value name value
- php_flag name on|off

mod_fcgid does not offer that. But you can add a define parameter to the CGI interpreter.

Code:

FcgidWrapper "C:/php82/php-cgi.exe -d upload_max_filesize=1G -d max_execution_time=360" .php


Save the httpd.conf file. Start Apache.

Now test apaches config file and see if everything loads. Open the command line again, and enter
Code:

httpd -S


If you have an issue like

Quote:

mod_fcgid: HTTP request length 131400 (so far) exceeds MaxRequestLen (131072)


You need to adjust FcgidMaxRequestLen bytes

The default value 131072 (bytes) which is 128 kB to inscrease this for example to 15MB (15728640 (bytes)).

e.g.
Code:

FcgidMaxRequestLen 15728640


For running mod_php and fcgid combined and or multiple php versions see https://www.apachelounge.com/viewtopic.php?t=3430

Configuration, see http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html

Save the httpd.conf file. Start Apache.

***Since some have asked where to place this in the httpd.conf - Here's what I do:
***
*** LoadModule directive goes at the end of the long list of LoadModule directives
*** I add a blank line after the LoadModule directive then add the AddHandler directive
*** I place the PHPIniDir directive right after the AddHandler directive
***



END PHP Setup


If you receive no errors, and you want to run Apache as a Service then open a command prompt run as administrator and navigate to the BIN folder of Apache. Now, install the service:
Code:
httpd -k install

That command will add Apache2 to the Services MSC.

If do not want Apache starting automatically at start-up/reboot:

    1. START | RUN
    2. Type in services.msc, hit Enter or click OK
    3. Locate Apache2 service and double-click (or right-click for Properties)
    4. Find the caption Startup type: (in the middle of the dialog box), use the pull-down and select Manual
    5. Click OK



If you have troubles with your PHP script take a look at Basic PHP Troubleshooting

MariaDB (MySQL) Setup


    1. Download from mariadb community download page
    2.double click the msi installer
    3. Edit my.ini in e.g. C:\Program Files\MariaDB 10.6\data\ and change any parameters for your site.


After you install the service be sure to:

Secure the initial MySQL accounts MySQL access controls. Be aware that default both root and the anonymous account are set up with no password, so anyone can connect to the MySQL server as root.


Optional:

    7. Download phpmyadmin which is a good interface over your browser to mysql.

    Only you can access the htdocs folder from the server or ftp
    8a. unzip it to C:\Apache2\htdocs\ and rename it to phpmyadmin

    9a. install phpmyadmin with the installer script in http://yourdomain.tld/phpmyadmin/scripts/setup.php

    10a. Delete the scripts folder in phpmyadmin. Create a .htaccess with

    Code:

    <RequireAll>
       Require ip 192.168 127.0.0.1 ::1
    </RequireAll>


    so no one can access that, but from your network and the server itself.



Other users can access htdocs folder via ftp.


On Windows 10 there is sometimes an error that port 80 is blocked, it might be W3SVC service. Set it to "manual" starting.

Code:
french name is: "Service de publication World Wide Web"
english name is: "World Wide Web Publishing Services"
german name is: "WWW-Publishingdienst"
Polish name is: "Usluga publikowania w sieci WWW"
Russian name is "Служба веб-публикаций"


other Programs that may causes apache not running

Troubleshooting

Sometimes apache doesn'tr start at all. Try adding the following block at the end of httpd.conf

Code:

EnableMMAP Off
EnableSendfile Off

AcceptFilter http none
AcceptFilter https none




Performance

Disable mod_status at all or at least ExtendedStatus Off

Load only modules that you really need

Something is blocking the Apache port(s)?

run cmd.exe as Administrator and run

Code:

netstat -ano | findstr /R 0.0:80 && netstat -ano | findstr /R 0.0:443


SSL Config

Code:

<If "%{SERVER_PORT} == '443'">
    <IfModule mod_headers.c>
    # you can enable this, but make sure that you understand it
    #    Header always set Strict-Transport-Security "max-age=15553000;"
    </IfModule>
</If>
TraceEnable Off

SSLUseStapling On
SSLSessionCache shmcb:C:/Windows/Temp/ssl_gcache_data(512000)
SSLStaplingCache shmcb:C:/Windows/Temp/ssl_stapling_data(512000)
SSLOptions +StrictRequire +StdEnvVars -ExportCertData
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCompression Off
SSLHonorCipherOrder On
SSLCipherSuite SSL ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES256-GCM-SHA384
SSLCipherSuite TLSv1.3 TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384

SSLOpenSSLConfCmd ECDHParameters secp521r1
SSLOpenSSLConfCmd Curves secp521r1:secp384r1


Virutal hosts

Note: if you add your first virutal host, the Domain from the httpd.conf will "disappear".

Code:

<VirtualHost *:80>
   ServerName test.local

   DirectoryIndex index.php

   <IfModule fcgid_module>
      FcgidInitialEnv PHPRC "C:/php82"
      FcgidInitialEnv PATH "C:\\php82;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"
      FcgidInitialEnv SystemRoot "C:\\Windows"
      FcgidInitialEnv SystemDrive "C:"
      FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv windir "C:\\WINDOWS"
      FcgidPassHeader Authorization
      <Files ~ "\.php$">
         Options Indexes FollowSymLinks ExecCGI
         AddHandler fcgid-script .php
         FcgidWrapper "C:/php82/php-cgi.exe" .php
      </Files>
   </IfModule>

   CustomLog "C:\nul" common

   DocumentRoot "C:/public"
   <Directory "C:/public">
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted

      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php [QSA]
   </Directory>
</VirtualHost>

<VirtualHost *:443>
   ServerName www.example.com

   DirectoryIndex index.php

   <IfModule fcgid_module>
      FcgidInitialEnv PHPRC "C:/php7"
      FcgidInitialEnv PATH "C:\\php7;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"
      FcgidInitialEnv SystemRoot "C:\\Windows"
      FcgidInitialEnv SystemDrive "C:"
      FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv windir "C:\\WINDOWS"
      FcgidPassHeader Authorization
      <Files ~ "\.php$">
         Options Indexes FollowSymLinks ExecCGI
         AddHandler fcgid-script .php
         FcgidWrapper "C:/php7/php-cgi.exe" .php
      </Files>
   </IfModule>

   CustomLog "C:\nul" common

   DocumentRoot "C:/public"
   <Directory "C:/public">
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted

      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php [QSA]
   </Directory>

   SSLEngine on
   SSLCertificateFile conf/certs/internal/fullchain.pem
   SSLCertificateKeyFile conf/certs/internal/privkey.pem

   <Files ~"\.(cgi|shtml|phtml|php|htm|html?)$>
      SSLOptions +StdEnvVars
   </Files>
</VirtualHost>


<VirtualHost *:443>
   ServerName other.example.com

   DirectoryIndex index.php

   <IfModule fcgid_module>
      FcgidInitialEnv PHPRC "C:/php7"
      FcgidInitialEnv PATH "C:\\php7;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"
      FcgidInitialEnv SystemRoot "C:\\Windows"
      FcgidInitialEnv SystemDrive "C:"
      FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"
      FcgidInitialEnv windir "C:\\WINDOWS"
      FcgidPassHeader Authorization
      <Files ~ "\.php$">
         Options Indexes FollowSymLinks ExecCGI
         AddHandler fcgid-script .php
         FcgidWrapper "C:/php7/php-cgi.exe" .php
      </Files>
   </IfModule>

   CustomLog "C:\nul" common

   DocumentRoot "C:/other"
   <Directory "C:/other">
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted

      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php [QSA]
   </Directory>

   SSLEngine on
   SSLCertificateFile conf/certs/internal/fullchain.pem
   SSLCertificateKeyFile conf/certs/internal/privkey.pem

   <Files ~"\.(cgi|shtml|phtml|php|htm|html?)$>
      SSLOptions +StdEnvVars
   </Files>
</VirtualHost>


Vhosts several files.

it might coming handy to use one file for each domain. You can do that by a include trick in the httpd.conf
Apache will include any .conf file in the APACHEROOTFOLDER/conf/vhosts

Code:

# Virtual hosts
Include conf/vhosts/*.conf


Enjoy,

James Blond

Admin note:
If you still have issues or find something confusing, please post in one of the other forums.


Last edited by James Blond on Fri 13 Sep '24 15:23; edited 49 times in total
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 10 Jul '13 14:44    Post subject: Reply with quote

I've updated the tutorial by some suggestions. Also thanks to Steffen and user TAB.

If you find any issues please report in the normal forum (not in How-to's & Documentation & Tips NOR this topic)
Back to top
XenonKilla



Joined: 06 Apr 2012
Posts: 8

PostPosted: Fri 29 Jan '16 3:39    Post subject: Reply with quote

Hey James,

Just a heads up, you've got "FcgidIOTimeout" in there twice
Back to top
XenonKilla



Joined: 06 Apr 2012
Posts: 8

PostPosted: Fri 29 Jan '16 4:08    Post subject: Reply with quote

You've also got the FcgidOutputBufferSize 64 and I'm assuming you want 64KB however simply putting "64" would only give you 64 bytes not kilobytes. The correct value would be FcgidOutputBufferSize 65536 for 64KB which is also the default value.

You've also got ProcessLifeTime 0 which should be FcgidProcessLifeTime 0

Ok that's all the critiquing I have for you. Laughing

Thanks for this guide!
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 04 Feb '16 17:36    Post subject: Reply with quote

Thanks a lot. I have changed that.

Well once upon a time there wasn't the prefix Fcgid for all directives... I wonder that so many ppl read it and never noticed it.
Back to top
Steffen
Moderator


Joined: 15 Oct 2005
Posts: 3092
Location: Hilversum, NL, EU

PostPosted: Thu 14 Feb '19 18:15    Post subject: Reply with quote

Why;

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

?

Not a L ? (L, QSA)

When not a directory and not a file, go to index.php and appending all after ? in the request.

Never seen before this solution, miss the clue Smile

Please explain.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Fri 15 Feb '19 9:35    Post subject: Reply with quote

The rewriting sends all 404s to index.php like most modern web apps do it these days. All urls are handled by the programming and not by files. Wordpress for example where you have the headline in the url does it almost the same way.
The missing L is not required since there is only one rule. QSA is Query String Append. So everything after a ? in the url. Don't loose that information only because of rewriting.
Back to top
Steffen
Moderator


Joined: 15 Oct 2005
Posts: 3092
Location: Hilversum, NL, EU

PostPosted: Wed 12 Jun '19 11:02    Post subject: Reply with quote

To send all 404’s to a script, you can use:

FallbackResource

See: https://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Tue 15 Oct '19 11:20    Post subject: Reply with quote

I know of that directive. I have used it sometimes. Sure for simple apps that works fine.

I also updated the how to.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Fri 16 Oct '20 11:10    Post subject: Reply with quote

I updated the how to once more.

it would be nice if someone would help and update the MySQL to a MariaDB setup tutorial.
Back to top
Otomatic



Joined: 01 Sep 2011
Posts: 212
Location: Paris, France, EU

PostPosted: Fri 28 May '21 18:53    Post subject: Re: A Fast, Reliable and Proven Setup :: Apache PHP MySQL Pe Reply with quote

James Blond wrote:

If you need xdebug (optional, only for development!) make you set a full path and load it AFTER opcache. It needs to be the last one.
xdebug is not recommended for production use.

use the https://xdebug.org/wizard to download the correct version of xdebug for your PHP version.

example
Code:

zend_extension = C:\php7\ext\php_xdebug-2.9.0-7.4-vc15-x86_64.dll

[xdebug]
xdebug.enable = 1
xdebug.remote_enable = 1
xdebug.idekey = "PHPSTORM"
xdebug.profiler_enable = 0


Hi,

Since xdebug 3.0.0 it must be something like :
Code:

[xdebug]
zend_extension="C:/php8.0.6/zend_ext/php_xdebug-3.0.4-8.0-vs16-x86_64.dll"
;xdebug.mode allowed are : off develop coverage debug gcstats profile trace
xdebug.mode = develop
xdebug.profiler_output_name = "cachegrind.out.%t.%p"
xdebug.output_dir ="C:/wamp64/tmp"
xdebug.show_local_vars=0

Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Sat 29 May '21 18:37    Post subject: Re: A Fast, Reliable and Proven Setup :: Apache PHP MySQL Pe Reply with quote

Otomatic wrote:

Hi,

Since xdebug 3.0.0 it must be something like


Thank you for heads up. I updated the tutorial.
Back to top


Reply to topic   Topic: A Fast, Reliable and Proven Setup :: Apache PHP MySQL View previous topic :: View next topic
Post new topic   Forum Index -> How-to's & Documentation & Tips