install amp on windows with chocolatey

Install AMP on Windows with Chocolatey

This is a guest blog post by Walt Sorensen.

Installing Amp on Windows can be a challenge at times, but thanks to Chocolatey: the package manager for Windows the whole process is a lot simpler. Chocolatey is a lot like the Linux tool apt-get and makes installing and managing packages simple.

Step 1: Install Chocolatey

Installing Chocolatey is a simple command line action from an administrative PowerShell v3+.*

iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

* (If you need to upgrade from PowerShell v2 on Windows 7 read this blog which notes some issues you might run into (and why Chocolatey would have made things a bit easier). I didn't have to worry about losing the PSModulePath customizations, so I just upgraded to PowerShell v5.1 via the Windows Management Framework 5.1.

Note: Windows 10 comes with PowerShell 5.0 so there is no need to install or upgrade PowerShell to simplify the Chocolatey installation.

Note: You must have your execution policy set to unrestricted (or at least in bypass) for this to work (Set-ExecutionPolicy Unrestricted) from administrator elevated PowerShell.

Close and reopen your shell window and we are good to go. Note: if you install Chocolatey to the default install location, you will always have to run choco from an elevated prompt.

Step 2: Install Apache, PHP and MySQL

It just takes 3 more command line actions to install the AMP components. To simplify things we are just going to install the latest versions and use choco install or the shortcut cinst. We will also bypass the install questions with the -y flag to autoconfirm. Also note we have to install the ThreadSafe version of PHP and threadsafe versions of any PHP modules. Read more here about NTS vs TS PHP here and when to choose each.

choco install apache-httpd -y
choco install mysql -y
choco install php -y --params '"/ThreadSafe"'

Step 3: Configuration

Now that we have default latest AMP components installed we just need to link everything together.

Let us start with noting that the Environment Vars (like PATH) have changed during the installs. Close/reopen your shell to see the changes (or in PowerShell just type refreshenv).

By default, Apache is installed to run from localhost:80 you can change this and other Apache settings in the httpd.conf file located at C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf

I had to make this change on my system as another service was already using port 80 causing a conflict, I also had to specify the ServerRoot folder as an explicit path in the httpd.conf file as the relative path value in Windows 7 had prevented the Apache service from starting throwing a code 1 event error.

I manually edited the values with notepad but you could easily backup and then change the port with PowerShell

copy "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf" "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd_backup.conf"
(gc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf").replace('Listen 0.0.0.0:80','Listen 0.0.0.0:8080') | sc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf"
(gc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf").replace('Listen [::0]:80','Listen [::0]:8080') | sc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf"
(gc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf").replace('ServerName localhost:80','ServerName localhost:8080') | sc "C:\tools\Apache\httpd-2.4.20\Apache24\conf\httpd.conf"

Configure Apache to use PHP

There are a few lines you need to add to the httpd.conf file to get Apache to talk to PHP. At the bottom or top of the httpd.conf file add these lines.

AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .php .html
LoadModule php7_module "C:/tools/php71/php7apache2_4.dll"
PHPIniDir "C:/tools/php71"

You also need to modify the IfModule dir_module section to add the value index.php before index.html

<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>

Configure PHP

Thanks to Chocolatey the php.ini-production file has already been copied as php.ini so the configuration is done unless you need specific modifications.

For example, say we want to Run Joomla we would need some specific modules enabled. Let's use PowerShell to modify the php.ini file. As a bonus I have also added a few other configuration changes that I find helpful for php projects. Note: you can also easily do this by manually editing the php.ini file in a text editor.

copy "C:\tools\php71\php.ini" "C:\tools\php71\backup.php.ini"
(gc "C:\tools\php71\php.ini").replace(';date.timezone =','date.timezone ="UTC"') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace('memory_limit = 128M','memory_limit = 512M') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace('upload_max_filesize = 2M','upload_max_filesize = 12M') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_curl.dll','extension=php_curl.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_gd2.dll','extension=php_gd2.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_fileinfo.dll','extension=php_fileinfo.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_mbstring.dll','extension=php_mbstring.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_mysqli.dll','extension=php_mysqli.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_pdo_mysql.dll','extension=php_pdo_mysql.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_openssl.dll','extension=php_openssl.dll') | sc "C:\tools\php71\php.ini"

If you prefer you can just add content to the php.ini file rather than scripting out all of the replacements. (I personally don’t care for the `n newline look in Add-Content as I find it harder to read in some cases, and I’m also not a fan of just adding content to the php.ini file).

copy "C:\tools\php71\php.ini" "C:\tools\php71\backup.php.ini"
Add-Content php.ini "`nmemory_limit = 512M""
Add-Content php.ini "`nupload_max_filesize = 12M"
Add-Content php.ini "`ndate.timezone ="UTC""
Add-Content php.ini "`nextension=php_curl.dll"
Add-Content php.ini "`nextension=php_gd2.dll"
Add-Content php.ini "`nextension=php_fileinfo.dll"
Add-Content php.ini "`nextension=php_mbstring.dll"
Add-Content php.ini "`nextension=php_mysqli.dll"
Add-Content php.ini "`nextension=php_pdo_mysql.dll"
Add-Content php.ini "`nextension=php_openssl.dll"
Add-Content php.ini "`n"

In some cases these changes might fail to work under Apache (i.e. they show in php -m from the command line but not in when Apache runs phpinfo() ). If you hit this issue try making the Directory in which the loadable extensions (modules) reside and explicit path value rather than a relative path.

(gc "C:\tools\php71\php.ini").replace('extension_dir = "ext"','extension_dir = "C:\tools\php71\ext"') | sc "C:\tools\php71\php.ini"

With these configuration changes completed, we need to restart Apache. Let's use PowerShell again to restart Apache.

NET STOP apache
NET START apache

At the time of writing, following this procedure installed Apache 2.4.20, MySQL 5.7.17, PHP 7.1.2 and all the related dependencies necessary for running on Windows. Chocolatey also created the related services for Apache and MySQL.

MySQL Configuration

There are some basic configuration values you need to know about the default MySQL install. For example, the password is an empty string "".

server: localhost
user: root
password: [empty value]

Following best practices, we should assign a password to the root user. For additional details see the MySQL documentation page on Assigning a password to the root user on insecure installs.

Connect to MySQL in PowerShell

mysql -u root

Once connected, (you should see a connection notice along with mysql > in the prompt) You can assign a password to the MySQL root account Replace new_password with the password that you want to use.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Step 4: Staying up to date

At some point you may want to upgrade one or all of your packages, Chocolatey makes this simple. From powershell just type:

chocolatey update somePackageName

or just

cup somePackageName

Want to upgrade all of your packages? Just type

chocolatey update all

Or the shortcut

cup all

But wait, there's more!

Let us look at installing other database options and what else Chocolatey can install.

Need SQLite? Just install it with Chocolatey and configure PHP. 

choco install sqlite -y

Next configure PHP to use SQLite by enabling the modules php_pdo_sqlite.dll and/or php_sqlite3.dll

(gc "C:\tools\php71\php.ini").replace(';extension=php_pdo_sqlite.dll','extension=php_pdo_sqlite.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_sqlite3.dll','extension=php_sqlite3.dll') | sc "C:\tools\php71\php.ini"

Want to switch to PostgreSQL? Just install PostgreSQL with Chocolatey and configure PHP.

choco install postgresql -y

Next configure PHP to use PostgreSQL by enabling the modules php_pdo_pgsql.dll and/or php_pgsql.dll

(gc "C:\tools\php71\php.ini").replace(';extension=php_pdo_pgsql.dll','extension=php_pdo_pgsql.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_pgsql.dll','extension=php_pgsql.dll') | sc "C:\tools\php71\php.ini"

We can also install pg admin 4 for PostgreSQL.

choco install pgadmin4 -y

Want to switch to MariaDB? Just install MariaDB with Chocolatey and configure PHP.

choco install mariadb -y

Next configure PHP to use MariaDB the same way you would for MySQL.

(gc "C:\tools\php71\php.ini").replace(';extension=php_mysqli.dll','extension=php_mysqli.dll') | sc "C:\tools\php71\php.ini"
(gc "C:\tools\php71\php.ini").replace(';extension=php_pdo_mysql.dll','extension=php_pdo_mysql.dll') | sc "C:\tools\php71\php.ini"

Note: the default MariaDB install is insecure and has and empty password for the root user.

Want to Switch to Microsoft SQL Server Express? Just install SQL Server Express via Chocolatey and configure PHP.

choco install sql-server-express -y

To configure PHP to use SQL Server is slightly more involved, but still fairly easy.

First you need to acquire the x64 threadsafe SQL module DLL files from PECL for sqlsrv and/or php_pdo_sqlsrv and place those in your C:\tools\php71\ext folder.

Now you can add the modules to your php.ini file either or both.

extension=php_sqlsrv.dll 
extension=php_pdo_sqlsrv.dll

or via PowerShell

Add-Content php.ini "`nextension=php_sqlsrv.dll"
Add-Content php.ini "`nextension=php_pdo_sqlsrv.dll"
Add-Content php.ini "`n"

Want to use the alternative HTTP server nginx? Just install it with Chocolatey and configure it to use your PHP installation.

choco install nginx -y

But wait, there's still more.

Want a useful tool to manage MySQL, Oracle, DB2, Microsoft SQL Server, or PostgreSQL databases? Try DBeaver by installing via Chocolatey.

choco install dbeaver -y

Want a GUI for Chocolatey management? Just install the ChocolateyGUI.

choco install chocolateygui -y

The web was meant to be read, not squished.
This isn't the way to test a responsive design.