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?
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