{"id":26325,"date":"2022-11-27T09:27:06","date_gmt":"2022-11-27T02:27:06","guid":{"rendered":"https:\/\/tom.ji42.com\/?p=26325"},"modified":"2022-11-27T09:27:06","modified_gmt":"2022-11-27T02:27:06","slug":"how-to-install-php-on-windows-10-with-apache-mysql","status":"publish","type":"post","link":"https:\/\/tom.tomwork.net\/?p=26325","title":{"rendered":"How to Install PHP on Windows 10 (with Apache &amp; MySQL)"},"content":{"rendered":"<p><strong>This article explains how to install PHP 8 and Apache 2.4 on Windows 10 (64-bit).<\/strong><\/p>\n<p>Linux and macOS users often have Apache and PHP pre-installed or available via package managers. Windows 10 requires a little more effort. The steps below <em>may<\/em> work with other editions of Windows, PHP, and Apache, but check the documentation of each dependency for specific instructions.<!--more--><\/p>\n<p><h4>Why PHP?<\/h4>\n<p>PHP remains the most widespread and popular server-side scripting language in web development.<\/p>\n<p>It\u2019s installed by most web hosts, and has a simple learning curve, close ties with the MySQL database, superb documentation, and an excellent collection of libraries to cut your development time.<\/p>\n<p><a href=\"https:\/\/www.sitepoint.com\/premium\/books\/php-mysql-novice-to-ninja-7th-edition\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1440\" height=\"520\" alt=\"PHP &amp; MySQL: Novice to Ninja\" src=\"https:\/\/uploads.sitepoint.com\/wp-content\/uploads\/2021\/03\/1651459137phpmysqlcta.jpg\"><\/a><br \/>PHP may not be perfect, but it should be considered as an option for your next web application. It\u2019s the language of choice for Facebook, Slack, Wikipedia, MailChimp, Etsy, and WordPress \u2014 the Content Management System which <a href=\"https:\/\/w3techs.com\/technologies\/overview\/content_management\">powers 40% of the web<\/a>.<\/p>\n<h4>Why Install PHP Locally?<\/h4>\n<p>Installing PHP on your development PC allows you to safely create and test websites and applications without affecting the data or systems on your live server.<\/p>\n<h4>Alternative Installation Options<\/h4>\n<p>Before you jump in, there may be a simpler installation options\u2026<\/p>\n<h5>Use an All-in-One package<\/h5>\n<p>All-in-one packages are available for Windows which contain Apache, PHP, MySQL, and many other dependencies in a single installation file \u2014 such as <a href=\"http:\/\/www.apachefriends.org\/xampp.html\">XAMPP<\/a>, <a href=\"http:\/\/www.wampserver.com\/en\/\">WampServer<\/a> and <a href=\"http:\/\/www.devside.net\/server\/webdeveloper\">Web.Developer<\/a>.<\/p>\n<p>These packages are easy to use, but they may not exactly match your live server. Installing Apache and PHP manually will help you learn more about the system and configuration options.<\/p>\n<h5>Use a Linux Virtual Machine<\/h5>\n<p>Microsoft Hyper-V (provided in Windows 10 Professional) and <a href=\"https:\/\/www.virtualbox.org\/\">VirtualBox<\/a> are free hypervisors which emulate a PC so you can install another operating system.<\/p>\n<p>You can install any version of Linux, then follow its Apache and PHP installation instructions. Alternatively, distros such as <a href=\"https:\/\/ubuntu.com\/download\/server\">Ubuntu Server<\/a> provide them as standard (although they\u2019re rarely the latest editions).<\/p>\n<h5>Use Windows Subsystem for Linux 2<\/h5>\n<p><a href=\"https:\/\/www.sitepoint.com\/wsl2\/\">WSL2<\/a> is also a virtual machine, but it\u2019s tightly integrated into Windows so activities such as file sharing and <code>localhost<\/code> resolution is seamless. You can install several Linux distros, so refer to the appropriate Apache and PHP instructions.<\/p>\n<h5>Use Docker<\/h5>\n<p><a href=\"https:\/\/www.docker.com\/\">Docker<\/a> creates a wrapper (known as a <em>container<\/em>) around pre-configured application dependencies such as Apache, PHP, MySQL, MongoDB, and most other web software. Containers look like full Linux Virtual Machines but are considerably more lightweight.<\/p>\n<p>Once you\u2019ve installed <a href=\"https:\/\/dockerwebdev.com\/tutorials\/install-docker\/#install-docker-on-windows\">Docker Desktop on Windows 10<\/a>, it\u2019s easy to <a href=\"https:\/\/dockerwebdev.com\/tutorials\/docker-php-development\/\">download, configure, and run Apache and PHP<\/a>.<\/p>\n<p>Docker is currently considered the best option for setting up a PHP development environment. Read our complete guide to <a href=\"https:\/\/www.sitepoint.com\/docker-php-development-environment\">setting up a PHP development environment with Docker<\/a>.<\/p>\n<h4>Install Apache (optional)<\/h4>\n<p>If you\u2019re still reading, the following sections describe how to install Apache and PHP directly on Windows.<\/p>\n<p>PHP provides a built-in web server which can be launched by navigating to a folder and running the PHP executable with an <code>-S<\/code> parameter to set the <code>localhost<\/code> port. for example:<\/p>\n<pre><code>cd \\myproject\nphp -S localhost:8000\n<\/code><\/pre>\n<p>PHP pages can then be viewed in a browser at <a href=\"http:\/\/localhost:8000\/\">http:\/\/localhost:8000<\/a>.<\/p>\n<p>This may be adequate for quick tests, but your live server will use Apache or similar web server software. Emulating that environment as closely as possible will prevent development errors.<\/p>\n<p>To install Apache, download the latest Win64 ZIP file from <a href=\"https:\/\/www.apachelounge.com\/download\/\">https:\/\/www.apachelounge.com\/download\/<\/a> and extract its <code>Apache24<\/code> folder to the root of your <code>C:<\/code> drive. You\u2019ll also need to install the Visual C++ Redistributable for Visual Studio 2015\u20132019 (<code>vc_redist_x64<\/code>); a link is provided on the same page.<\/p>\n<p>Open a <code>cmd<\/code> command prompt (not PowerShell) and start Apache with:<\/p>\n<pre><code>cd C:\\Apache24\\bin\nhttpd\n<\/code><\/pre>\n<p>You may need to accept a firewall exception before the server starts to run. Open <a href=\"http:\/\/localhost\/\">http:\/\/localhost<\/a> in a browser and an <em>\u201cIt works!\u201d<\/em> message should appear. Note:<\/p>\n<ul>\n<li>If you need to change any settings, Apache\u2019s configuration file is located at <code>C:\\Apache24\\conf\\httpd.conf<\/code>.\n<li>The web server root folder is located at <code>C:\\Apache24\\htdocs<\/code>. Initially, it contains a single <code>index.html<\/code> file with the <em>\u201cIt works!\u201d<\/em> message.<\/li>\n<\/ul>\n<p>Also not that, if Apache won\u2019t start, it\u2019s likely another application is hogging port 80. <em>(Skype is the prime candidate and the Windows 10 app won\u2019t let you disable it!)<\/em> If this occurs, edit <code>C:\\Apache24\\conf\\httpd.conf<\/code> and change the line <code>Listen 80<\/code> to <code>Listen 8080<\/code> or any other free port. Restart Apache and, from that point onward, you can load web files at http:\/\/localhost:8080<\/p>\n<p>The server can be stopped with <kbd>Ctrl<\/kbd> + <kbd>C<\/kbd>. The <code>ReadMe<\/code> file in the ZIP also provides instructions for installing Apache as a Windows service.<\/p>\n<h4>How to Install PHP<\/h4>\n<p>Install PHP by following the steps below. Note that there are several ways to configure Apache and PHP, but this is possibly the quickest method.<\/p>\n<h5>Step 1: Download the PHP files<\/h5>\n<p>You\u2019ll need the PHP Windows installer. There are a number of versions of PHP available. Make sure you get the latest PHP 8 <strong>x64 Thread Safe<\/strong> ZIP package from <a href=\"https:\/\/www.php.net\/downloads.php\">https:\/\/www.php.net\/downloads.php<\/a>.<\/p>\n<h5>Step 2: Extract the files<\/h5>\n<p>Create a new <code>php<\/code> folder in the root of your <code>C:\\<\/code> drive and extract the contents of the ZIP into it.<\/p>\n<p>PHP can be installed anywhere on your system, but you\u2019ll need to change the paths referenced below if <code>C:\\php<\/code> isn\u2019t used.<\/p>\n<h5>Step 3: Configure <code>php.ini<\/code><\/h5>\n<p>PHP\u2019s configuration file is named <code>php.ini<\/code>. This doesn\u2019t exist initially, so copy <code>C:\\php\\php.ini-development<\/code> to <code>C:\\php\\php.ini<\/code>. This default configuration provides a development setup which reports all PHP errors and warnings.<\/p>\n<p>There are several lines you may need to change in a text editor (use search to find the current value). In most cases, you\u2019ll need to remove a leading semicolon (<code>;<\/code>) to uncomment a setting.<\/p>\n<p>First, enable any required extensions. This will depend on the libraries you want to use, but the following extensions should be suitable for most applications:<\/p>\n<pre><code>extension=curl\nextension=gd\nextension=mbstring\nextension=pdo_mysql\n<\/code><\/pre>\n<p>If you want to send emails using PHP\u2019s <code>mail()<\/code> function, enter the details of an SMTP server in the <code>[mail function]<\/code> section (your ISP\u2019s server should be suitable):<\/p>\n<pre><code>[mail function]\n; For Win32 only.\n; http:\/\/php.net\/smtp\nSMTP = mail.myisp.com\n; http:\/\/php.net\/smtp-port\nsmtp_port = 25\n\n; For Win32 only.\n; http:\/\/php.net\/sendmail-from\nsendmail_from = my@emailaddress.com\n<\/code><\/pre>\n<h5>Step 4: Add <code>C:\\php<\/code> to the path environment variable<\/h5>\n<p>To ensure Windows can find the PHP executable, you need to change the <code>PATH<\/code> environment variable. Click the Windows Start button and type <em>\u201cenvironment\u201d<\/em>, then click <strong>Edit the system environment variables<\/strong>. Select the <strong>Advanced<\/strong> tab, and click the <strong>Environment Variables<\/strong> button.<\/p>\n<p>Scroll down the <strong>System variables<\/strong> list and click <strong>Path<\/strong> followed by the <strong>Edit<\/strong> button. Click <strong>New<\/strong> and add <code>C:\\php<\/code>:<\/p>\n<p><img decoding=\"async\" alt=\"PHP path environment variable\" src=\"https:\/\/uploads.sitepoint.com\/wp-content\/uploads\/2021\/03\/1616168136php-environment.png\"><\/p>\n<p>Note that older editions of Windows have a single text box where each path is separated by a semi-colon (<code>;<\/code>).<\/p>\n<p>Click OK until you\u2019re out. You shouldn\u2019t need to reboot, but you may need to close and restart any <code>cmd<\/code> terminals you have open.<\/p>\n<h5>Step 5: Configure PHP as an Apache module<\/h5>\n<p>Ensure Apache isn\u2019t running and open its <code>C:\\Apache24\\conf\\httpd.conf<\/code> configuration file in a text editor. Add the following lines to the bottom of the file to set PHP as an Apache module (change the file locations if necessary):<\/p>\n<pre><code># PHP8 module\nPHPIniDir \"C:\/php\"\nLoadModule php_module \"C:\/php\/php8apache2_4.dll\"\nAddType application\/x-httpd-php .php\n<\/code><\/pre>\n<p>Optionally, change the <code>DirectoryIndex<\/code> setting to load <code>index.php<\/code> instead of <code>index.html<\/code> when it can be found. The initial setting is:<\/p>\n<pre><code>&lt;IfModule dir_module&gt;\n    DirectoryIndex index.html\n&lt;\/IfModule&gt;\n<\/code><\/pre>\n<p>Change it to:<\/p>\n<pre><code>&lt;IfModule dir_module&gt;\n    DirectoryIndex index.php index.html\n&lt;\/IfModule&gt;\n<\/code><\/pre>\n<p>Save <code>httpd.conf<\/code> and test the updates from a <code>cmd<\/code> command line:<\/p>\n<pre><code>cd C:\\Apache24\\bin\nhttpd -t\n<\/code><\/pre>\n<p><code>Syntax OK<\/code> should appear \u2026 <em>unless you have errors in your configuration.<\/em><\/p>\n<p>If all went well, restart Apache with <code>httpd<\/code>.<\/p>\n<h5>Step 6: Test a PHP file<\/h5>\n<p>Create a new file named <code>index.php<\/code> in Apache\u2019s web page root folder at <code>C:\\Apache24\\htdocs<\/code> and add the following PHP code:<\/p>\n<pre><code>&lt;?php\nphpinfo();\n?&gt;\n<\/code><\/pre>\n<p>Open a web browser and enter your server address: http:\/\/localhost\/. A <strong>\u201cPHP version\u201d<\/strong> page will appear showing the various PHP and Apache configuration settings.<\/p>\n<p>You can now create PHP sites and applications in any sub-folder of <code>C:\\Apache24\\htdocs<\/code>. If you need to work on multiple projects, consider defining <a href=\"https:\/\/httpd.apache.org\/docs\/2.4\/vhosts\/index.html\">Apache Virtual Hosts<\/a> so you can run separate codebases on different <code>localhost<\/code> domains or ports.<\/p>\n<p>Further information:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.sitepoint.com\/how-to-install-apache-on-windows\/\">How to Install Apache<\/a>\n<li><a href=\"https:\/\/www.sitepoint.com\/how-to-install-mysql\/\">How to Install MySQL<\/a>\n<li><a href=\"https:\/\/www.sitepoint.com\/mysql-myisam-table-pros-con\/\">MySQL: the Pros and Cons of MyISAM Tables<\/a>\n<li><a href=\"https:\/\/www.sitepoint.com\/mysql-innodb-table-pros-cons\/\">MySQL: the Pros and Cons of InnoDB Tables<\/a>\n<li><a href=\"https:\/\/www.sitepoint.com\/mysql-foreign-keys-quicker-database-development\/\">How to Use MySQL Foreign Keys for Quicker Database Development<\/a>\n<li>Book: <em><a href=\"https:\/\/www.sitepoint.com\/premium\/books\/php-mysql-novice-to-ninja-6th-edition\">PHP &amp; MySQL: Novice to Ninja, 6th Edition<\/a><\/em>\n<li>Book: <em><a href=\"https:\/\/www.sitepoint.com\/premium\/books\/jump-start-php-environment\">Jump Start PHP Environment<\/a><\/em>, as well as many more books in our <a href=\"https:\/\/www.sitepoint.com\/premium\/topics\/all?q=&amp;limit=400&amp;offset=0&amp;page=1&amp;content_types%5B%5D=Book&amp;slugs%5B%5D=php&amp;states%5B%5D=available&amp;order=\">library<\/a>.<\/li>\n<\/ul>\n<p>If you\u2019re more comfortable with Linux, you might prefer to set up your PHP environment under Windows Subsystem for Linux 2. Check out our <a href=\"https:\/\/www.sitepoint.com\/wsl2\/\">WSL2 guide<\/a> and in-depth <a href=\"https:\/\/www.sitepoint.com\/windows-terminal\/\">Windows Terminal tutorial<\/a> for more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explains how to install PHP 8 and Apache 2.4 on Windows 10 (64-bit). Linux and macOS users often have Apache and PHP pre-installed or available via package managers. Windows 10 requires a little more effort. The steps below may work with other editions of Windows, PHP, and Apache, but check the documentation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[13],"tags":[],"class_list":["post-26325","post","type-post","status-publish","format-standard","hentry","category-13"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6cOVM-6QB","_links":{"self":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26325"}],"version-history":[{"count":1,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26325\/revisions"}],"predecessor-version":[{"id":26326,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26325\/revisions\/26326"}],"wp:attachment":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}