apache
Setting up virtual hosts with Apache
Thought I should post about setting up virtual hosts in Apache, since this was one of the first things I had to do. I got all this info from this site, which was incredibly useful. For all the following instructions, when you see these instructions:
enter some/text/here
You need to enter this text exactly as you see it in your terminal, which can be found at Applications>Accessories>Terminal
First, you need to set up your folders where the sites are gonna be stored:
mkdir /home/www/www.example.com mkdir /home/www/www.example.net
Then you need to tell Apache that you are gonna use virtual hosts. Create a file using:
sudo touch /etc/apache2/conf.d/virtual.conf
You need to add the following content, use:
sudo gedit /etc/apache2/conf.d/virtual.conf
and enter the following:
# # We're running multiple virtual hosts. #
NameVirtualHost *
Now, you have to create an entry in both the sites-enabled and sites-available directories in /etc/apache2. So, first create the following file:
sudo touch /etc/apache2/sites-available/www.example.com
and enter the following by using:
sudo gedit /etc/apache2/sites-available/www.example.com
and enter:
#
# Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *>
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/www/www.example.com/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog /home/www/www.example.com/logs/error.log
CustomLog /home/www/www.example.com/logs/access.log combined
</VirtualHost>
Next, do the same for your second site, so create a file using:
sudo touch /etc/apache2/sites-available/www.example.net
and enter the following by using:
sudo gedit /etc/apache2/sites-available/www.example.net
and enter:
#
# Example.net (/etc/apache2/sites-available/www.example.net)
#
<VirtualHost *>
ServerAdmin webmaster@example.net
ServerName www.example.net
ServerAlias example.net
# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/www/www.example.net/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/www/www.example.net/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog /home/www/www.example.net/logs/error.log
CustomLog /home/www/www.example.net/logs/access.log combined
</VirtualHost>
Finally, you need to create a copy of these files in the sites-enabled directory, using what is called a symbolic link. You can use a funky little program called a2ensite to do this. So, enter the following:
sudo a2ensite www.example.com sudo a2ensite www.example.net
And finally (I was lying last time), you need to restart Apache for this to all take effect:
sudo /etc/init.d/apache2 reload
And tada, it should all be working, yay!
Setting up Apache
mod_rewrite
After getting Apache up and running in Ubuntu, I needed to make sure it was all configured for WordPress. Everything was ok, excpet for mod_rewrite, which I apparently needed to enable. After some googling googling, I realised I needed to do this:
First, I needed to add the rewrite.load to /etc/apache2/mods-enabled/
So, open a terminal window and type:
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/
Second, edit the apache configuration for my virtualhosting. For example, in my computer I only have one virtual hosting (/var/www) that is default from installation, so I make some adjustment for that (In my case I have to edit this file /etc/apache2/sites-enabled/000-default)
Again in terminal type (this loads a text editor with the correct permission allowing you to edit the file:
sudo vi /etc/apache2/sites-enabled/000-default
Change the Allowoverride value to all for the document root directory
For example, I made change to this part of the configuration:
/var/www/
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
Finnaly, just restart the apache, so, again in terminal type:
sudo /etc/init.d/apache2 restart
.htaccess
I wanted to get WordPress to use nice page structures, rather than all that silly ?>443634 stuff. So, in settings in WordPress, I chose the permalink option and decided on the day,date,name option. However, for this to work, as wordpress warned me, I needed to allow wordpress to write to my .htaccess file.
The .htaccess is a file that can be used to do a lot to change the way your site works, it can be used to add security to your site (all though this isn’t always the best way). It turns out that my site didn’t have a .htaccess file, so I had to create one, this was as simple as creating an empty file in the root of my websites directoy and calling it, yes, you guessed it: .htaccess.
I then had ensure wordpress can write to this file. I did this temporarly (by typing sudo nautilus into terminal, which opens a file explorer, but as the super user, so you can do what you want (carefull, you can kill your system if you delete a file that is needed) and navigating to the root of my website directory and right clicking on the .htaccess file I created and choosing the permissions tab and changing the option so everyone can read/write the file and click OK). I then chose the correct option in wordpress via the admin interface to make fancy URLs. WordPress now no longer moans about not be able to write to the .htaccess file. Now, I go back to the .htaccess file in nautilus, right click it again and chase the permission for everyone back to read only. Ta da – fancy URLS and some security to boot.
