How To Set Up a Portable WEMP

Disclaimer: At the time I was working on this, the latest version of nginx available was 0.8.55.  Some time ago, right? :)This little tutorial will be updated for nginx 1.4.4 , MySQL 5.5, PHP 5.4 and phpMyAdmin 4 as soon as I get a chance to toy with nginx again.  You’re welcome to try this with newer versions; if it works, I’d be glad to hear about it.

Hey geeks! Time for my very first techie blog post. Have you ever wanted to set up a robust, speedy web server on your machine that requires no installation?

This is what you’re supposed to answer.

If yes, you’ve probably already checked out EasyPHP and many others. Now, EasyPHP is great: it serves its purpose, it’s easy to set up, and I have rarely ever had trouble with it. Sadly, EasyPHP is portable version of a WAMP installation – that is to say, it runs Apache, PHP, and MySQL together. For any reason, you might not want to deal with Apache: it’s matured and pretty standard, but it’s also heavy and sometimes, pretty slow. Web servers like lighttpd or nginx come to mind to replace Apache if you need something fast and smart.

Have no idea where to start? Keep reading! You’re at the right place.

Now, I wrote this tutorial back when I was in need of a quick, basic way to install a WEMP distribution: Windows, nginx, MySQL and PHP.  It took me some time to gather all the information available on the Net to install all five components (add phpMyAdmin for quick access to the databases, and xdebug to investigate PHP errors easily) and make them run smoothly together, but there we go! Hopefully this tutorial can help you. Follow the steps, and you should be good.

0. I Can’t Really Stress This Enough…

…Make sure that you will be using these products in agreement with their licences! MySQL’s licence has changed recently. It’s not always possible to use it in a professional environment if you have commercial interests (not without redistributing the sources anyway). Do yourself a favor: double check the licence agreements.

1. Make Sure You’ve Got The Right Stuff at The Right Place

This is the configuration we will use throughout this tutorial:

Version Directory Port used
nginx 0.8.55 <root_nginx> 80
PHP 5.3.6 <root_nginx>/PHP5 9002
MySQL 5.1.73 (No Install version for Windows) <root_nginx>/MySQL 9003
phpMyAdmin 3.3.10 <root_nginx>/html/phpmyadmin

<root_nginx> is a directory of your choice. Make sure the directory is accessible by the programs and that there is no confusion with user rights.

2.  Install and configure nginx

  •  Download nginx 0.8.55 here and unzip it into the <root_nginx> directory.
  • If you have web files, for example a test index.html page, move them to <root_nginx>/html. You can set up aliases later (like the phpmyadmin alias displayed belowed in this configuration).
  • Open the file called <root_nginx>/conf/nginx.conf and modify it so you have something close to this :
    location / {
      root html;
      index index.php; /* Au lieu de index.html */
    }
    
    location ~ .php$ {
      root html;
      fastcgi_pass 127.0.0.1:9002;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME <root_nginx>/html/$fastcgi_script_name;
      include fastcgi_params;
    }
    
    location /phpmyadmin {
      root html;
      index index.php;
    }
  • You’re done with the basic configuration. You can test it by executing <nginx_root>nginx.exe
  • Try to connect to http://localhost/
  • If no index.html file has been created, you should see nginx’ default 404 page.

3. Set up PHP

  • Grab the binaries of PHP 5.3 for Windows (Thread Safe version) on PHP’s downloads website 
  • Unzip them into the directory <nginx_root>/php
  • Create a temporary logs directory for PHP logs. For this tutorial, this will be the directory
    <nginx_root>/html/<php_log>
  • Take the file <nginx_root>/php/php.ini-production and copy its contents into a file called php.ini, at the same place.
  • Make sure that the following files can be found in <nginx_root>/php/ext/ :
    • php_mysql.dll
    • php_mysqli.dll
  • Modify the file named php.ini, and do the following edits to set up the variables:
    open_basedir = "<nginx_root>\html"
  • display_errors = On
  • error_log = "<nginx_root>\html\<temp_php_log>\php_errors.log"
  • extension_dir = "<nginx_root>/php/ext"
  • mysqli.default_port = 9003
  • session.save_path = "<nginx_root>\html\<temp_php_log>
  • Uncomment the following lines to configure what PHP extensions you would like to use. The extensions section should look like this:
    ;extension=php_ldap.dll
    extension=php_mbstring.dll  /* PHPMyAdmin */
    ;extension=php_exif.dll     
    extension=php_mysql.dll     /* MySQL */
    extension=php_mysqli.dll    /* MySQL */
    ;extension=php_oci8.dll
  • That should be all for PHP’s configuration! You can test it by launching your nginx server, then launching the PHP CGI exe, located at <nginx_root>/php/php-cgi.exe, with the following options:
    > php-cgi.exe -b 127.0.0.1:9002 -c <nginx_root>\php\php.ini
  • Then, create a default index.php page at <nginx_root>/html/ which only deplays the PHP configuration info. Your index.php file should contain only this:
    <?php
       phpinfo();
    ?>
  • Connect to http://localhost. The configuration info of PHP should display. Make sure that the configuration file it points to is <nginx_root>/php/php.ini

4. OPTIONAL: Set up XDEBUG to make sure you can debug your PHP code

  • Download XDebug 2.1.0 for PHP 5.3 VC9 here 
  • Move the DLL “php_xdebug-2.1.0-5.3-vc9.dll” to <nginx_root>/php/ext
  • Add the following line to <nginx_root>/php/php.ini:
    zend_extension = <nginx_root>\php\ext\php_xdebug-2.1.0-5.3-vc9.dll 
  • You’re done!

5. Install and configure MySQL 5.1

  • Start by downloading MySQL 5.1 no-install version for Windows here 
  • Unzip the archive into <nginx_root>/mysql
  • Optional: you can add the following directory to your Windows Path environment variable so that you can use the mysql command at any time: <nginx_root>\mysql\bin
  • Copy the file <nginx_root>/mysql/my-medium-ini into a file called my.ini at the same place
  • Open my.ini and configure it this way:
    [client]
    port		= 9003	
    socket		= /tmp/mysql.sock
    
    [mysqld]
    port		= 9003
    socket		= /tmp/mysql.sock
    skip-locking
    key_buffer_size = 16K
    max_allowed_packet = 1M
    table_open_cache = 4
    sort_buffer_size = 64K
    read_buffer_size = 256K
    read_rnd_buffer_size = 256K
    net_buffer_length = 2K
    thread_stack = 128K
  • Test your configuration: launch the MySQL server with the command:
    > "<nginx_root>\mysql\bin\mysqld" --console
  • Make sure the terminal shows the following lines at the end of the command’s execution:
    mysqld: ready for connections
    Version: '5.1.73'  socket: ''  port: 9003
  • Create a root password:
    > mysqladmin -u root password "password"
    ou
    > mysql -u root
    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password');
    mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('password');
  • Check the connection to the database with this password:
    > mysql -u root –p
    Enter password: password
    mysql> SHOW_DATABASES;
  • You should see the following lines:
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql |
    | test			            |
    +--------------------+
    3 rows in set (0.00 sec)
  • MySQL is ready!

6. Install and configure phpMyAdmin

  • Download phpMyAdmin 3.3.10 here
  • Unzip the archive into the directory <nginx_root>/html/phpmyadmin
  • Copy the configuration template <nginx_root>/html/phpmyadmin/config.default.php into a file called config.inc.php, at the same place
  • Modify config.inc.php so that it looks like this (don’t forget to put in the correct root password you set up in part 5):
    $cfg['PmaAbsoluteUri'] = 'http://localhost/phpmyadmin/';
    $cfg['Servers'][$i]['host'] = 'localhost';
    $cfg['Servers'][$i]['port'] = '9003';	 		/* MySQL port */		
    
    $cfg['Servers'][$i]['connect_type'] = 'tcp';				
    
    $cfg['Servers'][$i]['extension'] = 'mysqli';		/* MySQL extension */	
    
    $cfg['Servers'][$i]['controluser'] = 'pma';		/* Control user */
    $cfg['Servers'][$i]['controlpass'] = 'pmapass';
    
    $cfg['Servers'][$i]['auth_type'] = 'http';		/* HTTP login */
    
    $cfg['Servers'][$i]['user'] = 'root';			/* MySQL admin login */
    $cfg['Servers'][$i]['password'] = 'password';		/* MySQL admin PW */
  • Launch the MySQL server with the root password you specified in part 5
    > mysql -u root -p 
    > Enter password: password
  • Do the following commands to allow access to a phpMyAdmin user:
    mysql> GRANT USAGE ON mysql.* TO 'pma'@'localhost' IDENTIFIED BY 'pmapass';
    mysql> GRANT SELECT (Host, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv) ON mysql.user TO 'pma'@'localhost';
    mysql> GRANT SELECT ON mysql.db TO 'pma'@'localhost';
    mysql> GRANT SELECT ON mysql.host TO 'pma'@'localhost';
    mysql> GRANT SELECT (Host, Db, User, Table_name, Table_priv, Column_priv) ON mysql.tables_priv TO 'pma'@'localhost';
    
    mysql> source <nginx_root>\html\phpmyadmin\scripts\create_tables.sql
  • You can now test your whole setup! Make sure nginx, PHP-cgi and MySQL are all running, then connect to http://localhost/phpmyadmin
  • If phpMyAdmin’s main page shows up, you’re all good!
  • If not… well… good luck figure out what went wrong

Note: You can toy with the configurations to change the type of authentication for phpmyadmin, for example, or other things.

7. Optional: Create Batch Scripts to Launch/Stop/Restart Your Server

I really recommend creating batch scripts to launch nginx, PHP-CGI and MySQL quickly:

  • Create a file called start-server.bat
  • Edit the file so that it contains all the commands you need:
    @ECHO OFF
    ECHO Starting MySQL server...
    start <nginx_root>\mysql\bin\mysqld.exe
    ECHO Starting PHP FastCGI...
    start <nginx_root>\php\php-cgi.exe -b 127.0.0.1:9002 -c <nginx_root>\php\php.ini
    ECHO Starting nginx server...
    start <nginx_root>\nginx.exe
    ECHO Exiting.
    @ping 127.0.0.1 -n 2 -w 1000 > nul
    @ping 127.0.0.1 -n %1% -w 1000> nul
    EXIT
  • Create a file called stop-server.bat
  • Edit the file so that it contains all the commands you need:
    @ECHO OFF
    ECHO Stopping nginx...
    taskkill /im nginx.exe /f
    ECHO Stopping PHP FastCGI...
    taskkill /im php-cgi.exe /f
    ECHO Stopping MySQL Server...
    taskkill /im mysqld.exe /f
    ECHO Exiting.
    @ping 127.0.0.1 -n 2 -w 1000 > nul
    @ping 127.0.0.1 -n %1% -w 1000> nul
    EXIT

Note 1 : To get rid of annoying, disgraceful consoles, you can try installing RunHiddenConsole.
Note 2 : “@ping 127.0.0.1 -n 2 -w 1000 > nul” replaces a UNIX Sleep so that you can see the errors if there are some. Totally optional!

And you should be all set! If you have further questions or remarks, don’t hesitate to leave a comment.

Hope this helps!

Advertisements

Got something to say?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s