Skip to main content

Setting up a simple LAMP stack in WSL/Ubuntu

As I've mentioned before, I've been running WSL (Windows Subsystem for Linux) for roughly a year now. It's still a little mind-blowing to me - when I got started Microsoft was about as anti-linux as can be. Weird times!

Anyways, I have a fairly simple setup right now that's been working well for me. I'm using an older version of WSL right now, and apparently the newer version is much faster - but this works well enough for my needs right now.

This guide assumes you already have WSL set up and working, and can get a bash command line...

Install Apache and MySQL
This is easy, just run:
sudo apt install apache2
and
sudo apt install mysql-server
and while you're at it, you can start them up:
sudo service apache2 start
sudo service mysql start

You may run into some weird issues - I used this guide if you need any other info.

Now you have the most basic bits set up, it's time to get your actual sites up and running locally!

Setting up your projects
For me, I just created a 'projects' directory (in Windows, but could also do it in linux) and within that directory, I created a directory for each project (usually just a git repo, but you do you). Then we need to tell Apache where the project runs from - aka the web root. In Ubuntu, this is in:
/etc/apache2/sites-enabled/000-default.com
This is the main apache config file, take note of it's location! Inside that file I created there's a VirtualHost section - and inside that I created multiple DocumentRoot lines, corresponding to each of the project directories. Make sure you get the linux directory paths correct. Comment out each line with a '#' at the beginning of the line, then uncomment the project you want to have running right now.

Now, save the config file and run this:
sudo service apache2 restart

This will restart apache with the new configuration file. Your website should now be up and running at 'localhost'! Or is it...

Redirects
One issue you may run into depending on how the site is set up is redirects. For example, there could be a .htaccess file in the web root that is making sure all traffic is redirected to the https version of the site, or a version of the domain with (or without) 'www'. The effect of this will probably be that when you try to go to 'http://localhost' it redirects to the live site. Obviously that won't work!

This is a pain, what I usually do is change the .htaccess file to remove any redirects like that, and set up git to ignore the the .htaccess files using .gitignore - it's not ideal, but it works.

The bit of an htaccess that does these kinds of redirects will probably look something like this - no guarantee though:

# force www. in domain name
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

# enforce https
#RewriteCond %{HTTPS} !=on
#RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]


You can see here that I've commented these bits out. You don't need to restart apache after a change to htaccess. If you're still  not getting success - perhaps your set up has apache ignore htaccess files?

After adding htaccess to the gitignore file, changes to the .htaccess file are then not tracked by git - which kind of makes sense since the .htaccess could be considered to be server-specific.

Databases
Of course your site will probably also need a database. I usually grab a snapshot of the live database using mysqldump, load it into the local version of mysql and then restart mysql. So far that's worked pretty well!

File Permissions
You may also run into permission problems. It wouldn't be webdev without them! Because this is only running on my local laptop, I generally just put open permissions on everything. Also, WSL can do some weird stuff with permissions. But, you want to make sure that git is not tracking permissions settings on files - which thankfully is pretty easy:

git config core.filemode false

There you go! I'm sure you'll run into other problems - that's the nature of the job. But all in all, being able to run linux stuff under windows has made this a really viable dev platform for me. I've read that others have had a lot of success running Docker and things like that. That's more complication than I need right now, but who knows what the future holds?

Comments

Popular posts from this blog

Another VI tip - using macros, an example

God I love VI. Well, actually, vim but whatever. Here's another reason why. Suppose you need to perform some repetitive task over and over, such as updating the copyright date in the footer of a static website. (Yes, yes I know you could do a javascript thing or whatever, just bear with me.) Of course you could just search and replace in some text editor, changing "2007" to "2008" (if you're stupid) - and you'll end up with a bunch of incorrect dates being changed, most likely. What you need to do is only change that date at the bottom. And suppose that because of the formatting, you can't use the "Copy" part of the string in a search replace - perhaps some of the pages use "©", some spell out "Copyright" etc. This is where vi macros come in handy. A macro in vi is exactly what you expect, it records your actions and allows you to play them back. To start recording, press q followed by a character to use to "stor

Using FIle FIlters in FileZilla

Here's a handy tip for situations when you want to download a large number of files - but only of a certain type. For example, perhaps you want to download all the PHP files from a largish website, scattered through many subdirectories. Perhaps you're making a backup and don't want any image files, etc. FileZilla (still the best FTP in my opinion) has a handy feature called filename filters - located under the Edit menu. Here you can set various filters that filter out files based on their filename. Took me a minute to figure that out - you're saying show only PHP files, rather you're saying filter out files that do not have ".php" as their suffix. For some reason, that seems a little backwards to me, but whatever. It works quite well. You can also check whether the filter applies only to files, only to directories - or both. In this example, you'd want to check only files, as otherwise you won't see any directories unless they happen to end in

Debugging a DOS

I'm not a sysadmin, but I end up doing my best now and then when one of my sites gets into trouble. This is a sort of "after action report" of an incident that I just resolved (hopefully). I woke up and happened to check email on my phone (don't always do this, will now) and was greeted with a uptime robot email that one of my sites was down, and had been for about 4 hours. I quickly checked the site on my phone and yup, it wasn't loading. Ran to the office and hopped on my laptop. SSH to the server, and everything seems fine. Very little load on the server (AWS instance). Did a restart of apache/php/mysql and the site is still down. Weird. Running the site's index.php file on the command line works as expected and fast. Ask a few other people to check, and it's down for them. Then I logged into the AWS console and checked on status there - everything is up and running.... WTF? This is a lightsail instance, and then I noticed the outgoing network traffic h