A Fast Reliable and Proven Setup Apache PHP MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puertoblack2003
    OG Member
    • Nov 08
    • 8217

    [INFO] A Fast Reliable and Proven Setup Apache PHP MySQL

    A Fast, Reliable and Proven Setup :: Apache PHP MySQL from [ICODE]Apachelounge[/ICODE]

    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.:mrgreen:

    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


    CONTINUE:

    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"
    [URL unfurl="true"]https://github.com/microsoftarchive/redis/releases[/URL]

    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>
    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

    mod_fcgid: HTTP request length 131400 (so far) exceeds MaxRequestLen (131018)
    You need to adjust FcgidMaxRequestLen bytes

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

    e.g.
    Code:
    FcgidMaxRequestLen 15188640
    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,
    Last edited by puertoblack2003; December 06, 2023, 09:53 AM.

    SPJ Bulletin @
    forum.smartphonejunkie.org
    Law Enforcement Forum www.phillyfinest369.com
Working...