VAAMP - part 3

In this three-part series, I'll walk you through setting up a cross-platform LAMP server for local development.  I'll be using VirtualBox, Alpine Linux, Apache, MySQL, and PHP so I've nicknamed it VAAMP.

___________________________________________________


VirtualHost, shared folder, and DNS setup

#########

tl;dr

If you don't want to manually perform all the steps below, you can just download the VirtualBox appliance here.

#########

At this point we have a basic LAMP server running in VirtualBox.  Unfortunately, it's not especially useful for local development.  Creating and editing files via a command line interface is clunky and we're currently limited to running only a single local website.  Let's fix all that!

1.  Make sure to follow Part 1 and Part 2 of this guide and login as root

2.  Install a few needed packages by running this command:

apk add git sudo mysql-client php-phar php-openssl

3.  Next, install the Composer PHP dependency manager by running these commands:

curl -sS https://getcomposer.org/installer | php

mv composer.phar /usr/local/bin/composer

4.  Now we need to configure Apache to support multiple websites via VirtualHosts.  We could create separate config entries for each website but that gets rather tiresome so a more elegant solution is to use a single wildcard VirtualHost to support unlimited websites.  Also, we'll want to tweak the Apache configuration to support .htaccess files and run under a non-root account.

mkdir /usr/local/sites

adduser -D www www && echo "www:web-master" | chpasswd

sed -i '/^ProxyPassMatch\W/d' /etc/apache2/httpd.conf
sed -i '/^DirectoryIndex\W/d' /etc/apache2/httpd.conf
sed -i 's/AllowOverride\sNone/AllowOverride All/Ig' /etc/apache2/httpd.conf
sed -i 's/^User\sapache/User www/' /etc/apache2/httpd.conf
sed -i 's/^Group\sapache/Group www/' /etc/apache2/httpd.conf
sed -i 's/^user\s=\snobody/user = www/' /etc/php/php-fpm.conf
sed -i 's/^group\s=\snobody/group = www/' /etc/php/php-fpm.conf

cat <<EOT>> /etc/apache2/httpd.conf
<VirtualHost>
  ServerName local.dd
  ServerAlias *.local.dd
  UseCanonicalName Off
  VirtualDocumentRoot "/usr/local/sites/%0"
  RewriteEngine On
  RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/usr/local/sites/%{SERVER_NAME}/\$1 [P]
  DirectoryIndex index.htm index.html index.php
  ErrorLog /var/www/logs/error.log
  CustomLog /var/www/logs/access.log vhost_combined
</VirtualHost>
EOT

5. Basically, the commands above set Apache/PHP to run as the www user and will redirect any *.local.dd http request to a corresponding *.local.dd folder in /usr/local/sites  Let's go ahead and test it out by moving adminer into its own site

mkdir /usr/local/sites/adminer.local.dd && mv /var/www/localhost/htdocs/adminer.php /usr/local/sites/adminer.local.dd/index.php

6. Then we set the proper permissions and restart Apache/PHP

chown -R www:www /usr/local/sites

/etc/init.d/apache2 restart
/etc/init.d/php-fpm restart

7.  Before we can load http://adminer.local.dd/ in our browser, though, we need to tell our computer what it is since that domain isn't a valid Internet address.  Follow these instructions to add this line to the bottom of your hosts file:

192.168.50.50 adminer.local.dd

8.  Okay, let's try it out!  Open a browser window and go to http://adminer.local.dd/   You should see the Adminer login screen:


9.  You're probably thinking "Umm...we loaded Adminer in Part 2 of this series.  What's the big deal?"  Well, in addition to this Adminer website, we can now run additional separate websites in our virtual machine.  As an example, let's install Drupal.  As a prerequisite we'll need a database so let's set one up now.  In the Adminer screen, type root for the Username and then click Login

10. Next, click Create new database



11. Type mydrupalsite for the name and choose utf8_general_ci for the collation type:



12. Click the Save button and click the Privileges link


13. Click the Create user link, provide a username and password, click the All privileges checkbox, and then click Save



14. Now that we have a database for our Drupal site, let's download Drupal and set it up.  Since Apache/PHP runs as the www user, we need to switch accounts first:

sudo su - www

15. There are a couple ways to install Drupal, but I prefer using a Drupal automation tool called Drush.  Let's install the latest version:

composer global require drush/drush:dev-master

16. Now that Drush is installed, let's add it to our PATH so we can reference it anywhere on the command line:

exit

ln -s /home/www/.composer/vendor/drush/drush/drush /usr/local/bin/drush

sudo su - www

drush --version

16. Although we can use Drush to perform the entire Drupal install, I'm just going to use it to download Drupal and then walk through the rest of the steps manually so you can follow along:

cd /usr/local/sites/ && drush dl drupal && mv drupal-* mydrupalsite.local.dd

17. Use the instructions in Step 7 to add the following to your computer's hosts file:

192.168.50.50 mydrupalsite.local.dd

18. Load http://mydrupalsite.local.dd/ in your browser and follow the Drupal installation wizard (when prompted for database credentials, use the database name/username/password from steps 10-13 above)



19. You should now have Drupal running inside your virtual machine.  Woot!



20. Your next question is probably "Great, but how do I access the files now?"  Well, normally you would set up a VirtualBox shared folder to synchronize the websites directory with a folder on your local machine but unfortunately Alpine Linux doesn't support the VirtualBox guest additions yet so we need another approach.  Although you could do something like FTP, Syncthing, etc., the most elegant solution I've found is sshfs.

Note: since this virtual machine isn't accessible from anything but the host, we'll forsake some security in lieu of convenience by using a pre-generated ssh keypair.



If you feel more comfortable, you can delete the /home/www/.ssh folder and setup your own keypair.  
21. Switch back to the Alpine Linux virtual machine command line, make sure you're in the www account (by typing whoami), add then add the pre-generated public key:

mkdir ~/.ssh && chmod 700 ~/.ssh && cd ~/.ssh && curl -s http://ix.io/k5x > authorized_keys && chmod 600 authorized_keys

22. On your host machine, create a file called vaampkey.txt and copy the contents of http://ix.io/k5w into it.

23. Now you need to install the sshfs client:

OS X: Install osxfuse and sshfs by following this guide
Windows: Download and install https://win-sshfs.googlecode.com/files/win-sshfs-0.0.1.5-setup.exe (note: the installer may warn you about missing components but they will be automatically downloaded and installed as part of the sshfs installation process).  Also, the install may require a computer restart. 
Red Hat: sudo yum install fuse-sshfs 
Ubuntu: sudo apt-get install sshfs

24. Open sshfs and click the + Add button



25. Now, enter the following information:
  • Drive Name = vaamp
  • Host = 192.168.50.50
  • Port = 22
  • Username = www
  • Authentication method = PrivateKey
  • PrivateKey = YOUR/PATH/TO/vaampkey.txt
  • Directory = /usr/local/sites
  • Drive Letter = V
26. Then click Save and then Mount (note: you MUST click Save first due to bug #85)



27. You can now add/update/delete website files via your V: drive!


28. Last but not least, we need an easy way to check the server logs.  I like Pimp my Log so let's use that.

mkdir /usr/local/sites/logs.local.dd

cd /usr/local/sites/logs.local.dd

git clone https://github.com/potsky/PimpMyLog.git .

29.  Now we need to turn on PHP logging by exiting the www user account and logging into the root account and then running:

sed -i 's/^;error_log\s=\sphp_errors\.log/error_log = /var/log/apache2/php_errors.log/' /etc/php/php.ini

/etc/init.d/php-fpm restart

touch /var/log/apache2/php_errors.log

30. Finally, we need to add the following line to our computer's hosts file

192.168.50.50 logs.local.dd

31. When you load http://logs.local.dd/ in your browser you should see a welcome page:



32. Click Configure Now and then click No when asked if you want to protect the log website with an admin account (note: normally this is an important security measure, but since no one can access this virtual machine except our host, we forego the extra overhead)




33. The wizard should detect your Apache and PHP setup.  Click Continue



34. The wizard should detect your Apache logs.  Click Continue



35. The wizard should detect your PHP logs.  Click Continue



36. That's it!  Click Pimp My Logs Now to see your logs.



If you've made it this far, congratulations!  I hope you enjoyed the experience and learned a few things along the way that may help you discover and configure your preferred local development experience.

P.S. Check out the quickstart guide for a handy reference.


_______________


References:

0 comments:

Post a Comment

Keep it clean and professional...