Monday, 27 February 2012

How to delete a Virtual Server using Virtualmin

Deleting a Virtual Server from your server  - Replacing the default site domain on a server with an IP address


The other day I talked about how you can setup a website for testing on your LINUX server using host headers so that you can test it before purchasing a domain name.

Now I did this on one PC and gave the virtual server a particular name in the hosts file so I could access it but because I was at work and behind a proxy it wouldn't allow me to access the website by the desired hostname. Therefore to get to the site I just put in my servers IP address e.g http://174.34.34.114.

However on my home laptop I changed my mind about the name I wanted to use for the new site and instead of editing the existing virtual server I created a new one with a new domain name (again one I hadn't purchased yet).

However because I wasn't behind an office proxy my local hosts file allowed me to access the website by the domain name I had given it in my file with a command like so e.g:

174.34.34.114 www.robsnewblog.com

However now I am back in the office and want to access the new site but due to the proxy issue when I enter the IP address I am met with the old site as that is the default site for the server. Changing the host file has no effect due to the issue with the Proxy. Therefore I cannot access my new domain www.robsnewblog.com either by the IP address OR the hosts file command.

Therefore the solution is to delete the old Virtual Server from my host in Virtualmin and reset the new site as the default server so that I can access it by the hosts IP address alone.

To do this you need to do the following:

1. Login to your host e.g https://myhostname.blah.com:10000/
2. Choose the virtual server you want to delete from the drop down box on the left e.g robstestdomain.
3. Extend the "Disable and Delete" menu option on the left menu and choose "Delete Virtual Server"
4. The main section will list all the services and files that will be deleted. Confirm that you want to delete the server.
5. You will be given an output like the following:


Delete Server
In domain robstestdomain.com
Deleting mail aliases ..
.. done
Deleting AWstats configuration file and Cron job ..
.. done

Removing password protection for AWstats ..
.. done

Deleting MySQL database robstestdomain ..
.. done

Deleting MySQL login ..
.. done

Disabling log file rotation ..
.. done

Deleting scheduled Webalizer reporting ..
.. done

Deleting virtual website ..
.. done

Deleting Apache log files ..
.. done

Removing from email domains list ..
.. done

Deleting home directory ..
.. done

Deleting administration user ..
.. done

Deleting administration group ..
.. done

Deleting server details for robstestdomain.com ..
.. done

Applying web server configuration ..
.. done

6. The virtual server will now have been deleted. A quick test by going to the domain or IP will show you the site cannot be accessed anymore and if you try to FTP to the site you will get an error trying to connect.

7. As you haven't bought a domain for the new site and if like me you are behind a proxy that is preventing you from using your hosts file to access the new domain then you will want to access the site from the IP address alone.

If this is the case and your hosts file is not working (after editing, and re-starting your browser) then you need to ensure that the Virtual Server you want to use is set as the default by going into VirtualMin, editing the Server, choosing "Server Configuration" from the menu and then "Website Options" and then making sure the "Default website for IP address?" radio button is ticked if it isn't already. This will ensure all requests to the IP address are forwarded to the Virtual Server in question.

7. Restart Apache - From WebMin (which you should have installed when you setup the server if you prefer graphical interfaces to using the SSH console) you can do this by going to System > Bootup and Shutdown. Tick the Apache2 box and then at the bottom hit the "Restart" button. Either that or use the command line and this command: /etc/init.d/apache2 restart.

8. If you have any problems go to System Settings and choose the "Re-Check Configuration" option to see if any errors appear or run apache2ctl configtest from the command line to ensure all configuration files are correct.


9. Go to the IP address of your site and hopefully it will now load up. Obviously if you are using an IP address to access the site but the website is set-up with a non existent domain name then paths to scripts and CSS files will not load correctly as they will be pointing to places that don't exist.


To fix this you will need to edit your database either in VirtualMin or through a MySQL management tool like PHPMyAdmin or Navicat and go through the wp_options table editing every row that currently points to your domain name to your IP address. The main ones will be:

  • siteurl
  • home
  • dashboard_widget_options



Obviously if you have custom plugins that have added their own data into the database you might need to change those as well. A quick SQL query will help you hunt any rows down


SELECT  *
FROM    wp_options
WHERE  option_value = 'http://www.robstestdomain.com';


Where you would obviously replace the URL with the domain you want to replace with your IP address.


This should now let you see the website on your PC even a proxy is blocking your hosts file commands.

How to install WebMin on LINUX

How to install WebMin on LINUX

If you are from a Windows background and used to graphical interfaces then moving to a LINUX system can be quite a challenge due to the fact that most techies like to use commands from the console to carry out their work.

Whilst it is a good opportunity to learn some of the commands when you first rent a virtual server from Rackspace or buy your own box then you want to get your sites up and running quickly without having to read a lot of information first.

Therefore if you don't have the time to learn all the LINUX commands installing WebMin on your system is a quick way of providing you with a graphical interface to allow you to edit files and stop and start services like the Apache webserver or MySQL database server, as well as managing these services and configuring settings through a visual interface. You will need to have Java installed to use parts of WebMin.

If you want to learn how to use the console for carrying out your work then a good list of Linux applications and commands can be found here: Linux Commands and you should read some of the following articles I have written specifically about performance turning and managing a system such as Wordpress on a LINUX based system.

The Wordpress Survival Guide Part 1
The Wordpress Survival Guide Part 2
Performance Tuning Tools for MySQL
Using Host Headers to setup a test site on LINUX
Problems with LINUX Apache and PHP
Debugging Memory Issues on Wordpress

A quick cheat sheet of the most popular commands I find myself constantly using from the command line through a tool like PUTTY are below. Make a copy and save them to your desktop for quick and easy access.


CommandDetails
dateShow the current date and time on the server
cdchange drive e.g cd /var (go to the var directory)
cd ../go back up one directory
cd ../../go back up two directories
lslist out the contents of a directory
whoamisee who you are logged in as
su - [username]Assume the permissions of the specified user
sudo [command]Run a command as root but stay as the user you are logged in as
topShow the current running processes and server load
top -d .2Show the current running processes with .2 second refresh
tail -f access_logView the most current entries in the sites access log
grep "61.252.14.247" access_log | tailView the most current entries in the sites access log for a certain IP address
netstat -taShow all current connections to the server
grep "27/Feb/2012:" access_log | sed 's/ - -.*//' | sort | uniq -c | sort -nr | lessView the IP's that appear in your access log the most for a certain date ordered by the most frequent first.
/etc/init.d/apache2 restartRestart Apache
apache2ctl configtestTest the Apache configuration for configuration errors
/etc/init.d/mysql restartRestart MySQL
wget [URL]Remotely access, load and save a file to the current directory
chmod 777 [filepath]Grant full read/write/delete permission to everyone to a file or folder
chmod +x [filepath]Grant execute permission to a script
rebootReboot the server


How to install WebMin on a LINUX

Back to WebMin, to install WebMin on a LINUX based server you should do the following.

1. Install PUTTY if you haven't already.
2. Connect to your server through SSH using PUTTY.
3. Download the WebMin file with the following command:
wget http://prdownloads.sourceforge.net/webadmin/webmin_1.580_all.deb
4. Run the following command: dpkg --install webmin_1.580_all.deb

You should have now installed WebMin to /usr/share/webmin.

The administration username will be set to root and the password will be set to your current root password.

You should now be able to login to Webmin by accessing your hostname/IP on the port 10000.

For example e.g if your hostname is myhost.some-site.com then use http://myhost.some-site.com:10000/.

If you server is running in SSL mode (which it should) then it will initially give you a "Bad Request" error and ask you to try a secure URL instead e.g https://myhost.some-site.com:10000/

The WebMin options will appear as a tab to the right of the VirtualMin tab in the top left of the screen.

Saturday, 25 February 2012

Using Host Headers on a Virtual Server and creating a new site before buying a domain name

How to create and test a website before buying the domain name


Sometimes you may want to create a new website but don't have a domain name to point it at but still want to test everything is working.

Today I wanted to do this and there were a number of issues I had to work through so I thought I would list them for all the Windows developers who are new to LINUX and APACHE.

The site was a basic Wordpress site which was going to run on a Rackspace Virtual Server which already hosted a number of sites.

If you have installed VirtualMin and WebMin it's very easy to manage your server with a graphical interface otherwise you will need to do everything through a SSH client like PUTTY terminal serviced into your server.

1. Create a new virtual site
  • Login into your webserver which will be your hostname on port 1000 e.g: https://myhostname.blah.com:10000/
  • If you login as root you won't need to create Webmin logins for each virtual host.
  • Choose the "Create Virtual Server" option on the left menu.
  • Complete the form entering the name of the domain you want to use e.g  robstestdomain  I tend to use the domain name as the description field as well.
  • Enter a password - this will be used for your FTP and Database password unless you change it later - make it secure by using a mixture of letters and numbers both lower and uppercase. If you cannot remember a complex string use a combination of words or a sentence and add numbers or symbols between each word and at the beginning and end e.g 77Strictly2012Software1984
  • Under the Enabled features I ticked the following options:

Setup website for domain?
Accept mail for domain?
Setup Webalizer for web logs?
Create MySQL database?
Enable AWstats reporting?


  • Click the "Create Server" button and the system will generate everything it needs with an output like so

Setting Up Virtual Server

In domain robstestdomain.com
Creating administration group robstestdomain ..
.. done
Creating administration user robstestdomain ..
.. done
Creating aliases for administration user ..
.. done
Adding administration user to groups ..
.. done
Creating home directory ..
.. done
Creating mailbox for administration user ..
.. done
Adding to email domains list ..
.. done
Adding default mail aliases ..
.. done
Adding new virtual website ..
.. done
Adding webserver user www-data to server's group ..
.. done
Performing other Apache configuration ..
.. done
Setting up scheduled Webalizer reporting ..
.. done
Setting up log file rotation ..
.. done
Creating MySQL login ..
.. done
Creating MySQL database robstestdomain ..
.. done
Setting up AWstats reporting ..
.. done
Setting up password protection for AWstats ..
.. done
Applying web server configuration ..
.. done
Saving server details ..
.. done

Once you have done this open up your favourite FTP client like FileZilla and add the new domains details to the Site Manager. For example if you were creating a secure FTP site you would use these settings.

  1. Click "New Site" give it the name of your domain > "robstestdomain"
  2. On the right fill out the form with
  3. Host: myhostname.blah.com
  4. Protocol: SFTP - SSH File Transfer Protocol (secure FTP) you can choose FTP if your hosts firewall is setup to allow traffic through ports 20 and 21.
  5. Logon type: Normal
  6. User: robstestdomain
  7. Password: [the same password you entered when creating the Virtual Server] e,g 77Strictly2012Software1984
  8. Click "Connect" your FTP client should now connect to the new folder on your server which will be empty apart from some default folders like cgi-bin, homes, logs, public_html etc.


Download the latest version of Wordpress from their site: http://wordpress.org/download/

Extract the files and copy them up to the public_html folder on your server e.g: /home/robstestdomain/public_html

You will now have extra folders like wp-content, wp-includes and wp-admin under /public_html as well as a number of files including wp-config-sample.php.

You need to edit this file and replace the main database connection settings at the top of the file. These will be the same settings you used when creating the site unless you have manually gone in and changed them.


// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'robstestdomain');

/** MySQL database username */
define('DB_USER', 'robstestdomain');

/** MySQL database password */
define('DB_PASSWORD', '77Strictly2012Software1984');

/** MySQL hostname */
define('DB_HOST', 'localhost');

One thing I found earlier is that if you created a domain with a hyphen in it like "My-Domain" then when you create the username you should replace the hyphen with an underscore otherwise it won't be able to connect to the database.

You should also add in sentences for all the security keys to make your site more secure. The longer the words the better.


define('AUTH_KEY',         'some phrase or two');
define('SECURE_AUTH_KEY',  'another unique phrase 100003433');

....

define('NONCE_SALT',       'something else 23423423');

Once complete save the file as wp-config.php and upload to the root folder e.g /public-html/wp-config.php

Now that you have a virtual server setup you need to make sure that you can access it from your own computer as it currently isn't visible online as there is no DNS record setup pointing a domain to your site.

To do this you can edit your hosts file which is located om your computer (windows 7) under
c:\windows\System32\drivers\etc\hosts.

You need to add the following line to the host file that points the IP address of your webserver to the domain you have just created.

If you don't know the IP address of your host just open a command prompt and type "PING [hostname]" e.g PING myhostname.blah.com

Reply from 179.202.123.19: bytes=32 time=105ms TTL=49

In your host file at the bottom of any exiting list put the IP address of your host and the domain you want it to point to e.g

179.202.123.19 www.robstestdomain.com

If you have problems editing the file such as it saying that the file is "Read Only" even when it's not set as Read Only, or you don't have sufficient administrator privileges to edit the file then do the following:

1. Right click on Notepad and choose "Run as administrator"
2. Use the File > Open option to open the hosts file
3. Add the line in and then choose File > Save.

Make sure you shut down your browser after editing the host file before re-opening it and then typing in the domain name in the address bar e.g http://www.robstestdomain.com.

If everything has gone okay you should be taken to the Wordpress configuration page e.g http://www.robstestdomain.com /wp-admin/install.php where you can complete the setup by entering details of your sitename, email and password.

Once the form is completed click on the "Install Wordpress" button it will build all the tables in your database and activate your site. You can check the database has been build from within VirtualMin or by using a MySQL database management system like Navicat.

Remember if you have other sites on the same server that have DNS records setup they should still work by entering their domain names into the address bar but because you have told your computer to always take the domain and point it at your server in the host file you should be able to see your new site even if no-one else can.

You could enter your IP address alone instead of the domain name but either should bring you to the new site.

If you don't want people to access your new site during development by hitting your IP address (not that many real users would do) you should setup some htaccess or deny hosts rules so that only traffic from your own PC can directly use the IP address or your new test domain name and return a 403 status code to everybody else.

Once you have set your site up, installed your themes and plugins and read my Wordpress Survival Guides, one and two on the best plugins to use for performance and the best Apache config for your site you will be ready to rumble. Go and buy a domain from http://www.123-reg.co.uk/ and point it to your new virtual server.

If you have any problems you should also check your new sites Apache config file e.g /etc/apache2/sites-available/robstestdomain.com.conf and make sure its setup like you want. Open an existing config file for one of your other working sites and copy and paste the data changing the names to your new site.


Always restart your server after changing your conflig files but only after running a test for any syntax errors in any of your config files. You can do this in your console with the following lines.


myhost:~:# apache2ctl configtest
Syntax OK
myhost:~:# /etc/init.d/apache2 restart

You can also restart Apache through WebMin which is located under the System > Bootup and Shutdown menu option.

And that my friends is how you do it.

Update: 27-FEB-2012

If you change your mind about the domain name you want to use for your site you will need to delete the existing virtual server and set-up a new one. This might cause problems if you are behind a proxy at work that doesn't allow your hosts file to work. Therefore you should read this article on deleting a site in VirtualMin and accessing a Wordpress site by IP address alone.

Tuesday, 21 February 2012

Debugging on the iPhone

Debugging Console on iPhone

I have an iPhone 3GS and one thing I have always found annoying is that when I am developing web pages for display on the iPhone you cannot debug them as easily as you can with a PC.

Yes you can get simulators and use user-agent switchers but they are not the same as debugging on the real device.

If I use an agent switcher and change my user-agent to GoogleBot or IE 6 that isn't got to simulate a web page that tests for agents and devices properly or handle libraries that test for unique iPhone / iPad features available on the device and loaded in by specialist external libraries such as the two finger scroll etc.

A proper coder will test for a BOT in ways that don't rely purely on the user-agent including checking for known BOT IP addresses that the they crawl from, a reverse/forward DNS check or tests for JavaScript or Flash use or all 3 and a myriad of other techniques.

On the iPhone you can easily test whether a user-agent switcher is being used by creating a flash movie and then testing if it's been loaded as we all know iPhone doesn't support flash so a user-agent sniff for "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" is useless for simulating an iPhone and it's workings properly.

Therefore knowing whether there are JavaScript errors on the page or wishing to output debug messages to the console on an iPhone which you can do easily in all other browsers is a very useful thing to be able to do. I didn't actually find this out until this morning but on the iPhone you can actually do this by going to your settings menu option and enabling it with the following steps.

Go to Settings > Choose the Safari Option > At the bottom of the menu select the Developer option > Enable the "Debug Console" option.

If you then go to any web page you should see at the top of the page will now appear a new panel titled "Debug Console" and if there are no errors on the page it will say "No Errors". If there are errors but no output to the console (e.g with a console.log(msg) function call) then it will list the number of errors and if there are console messages it will tell you the number e.g "7 Logs".

Selecting the console option will show a new screen in which you can view all the console messages. At the bottom of the screen are options to view "All messages", "HTML", "JavaScript" and "CSS". Clicking the relevant tab will show you messages related to those errors.

If there are JavaScript errors it will show you the line number, the error message and a description but unlike a proper browser you cannot click on the error message and view the actual source code.

In fact that is the one bug bear I have about iPhone's Safari browser which is the lack of developer options such as being able to view the source and generated source code as well as controlling options such as white/blacklists for 3rd party cookies, JavaScript and so on. However at least you can view the errors on the device properly if you need to.

Sunday, 5 February 2012

Low Server Usage, Long Page Loads with APACHE Caching

Disabling Apache Caching improves LAMP Server Performance


On one of my virtual servers which runs LAMP on only a 1GB RAM box with a couple of quite high traffic sites I have been experiencing an intermittent problem which had the following symptoms.


  1. Long page load wait time - Spinning browser window with nothing happening
  2. Very low server load average < 10 despite large number of requests to website
  3. Large amount of disk swapping - where the hard drive is used to temporarily store data


This also coincided with these errors in the log files

[Thu Feb 02 16:30:57 2012] [error] (103)Software caused connection abort: cache: error returned while trying to return mem cached data

Running a TOP command would return something like this

op - 16:42:06 up 3 days, 21:29,  2 users,  load average: 0.12, 0.39, 0.67, 1.39
Tasks: 219 total,   1 running, 218 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1060972k total,   931768k used,   129204k free,     9668k buffers
Swap:  2097144k total,   328740k used,  1768404k free,   362284k cached

An APACHE restart always fixed this issue temporarily however on running

/etc/init.d/apache2 restart

I noticed it was carrying out an htcacheclean command and it was always on this clean commands completion that the page load would suddenly spin into action before terminating as the web-server stopped.

Both websites that ran on the server were using Wordpress systems that utilised caching plugins, namely WP-SuperCache and WP-Widget-Cache as well as .htaccess file rules that set future expiry headers on certain file types e.g JavaScript, CSS, Images etc.

I also ban over 55% of all traffic through rules that block known bad BOTS, spammers, hackers and known bad IP addresses. If you are not writing articles in Chinese or Russian and have no wish to pay for the bandwidth caused by BOTS like Yandex and Baiduspider to visit constantly then 403 them! They both pay no attention to the Robots.txt file so in my book can be labelled as "Bad BOTS" anyway.

As the main symptom was very low server loads AND hanging pages that were cured by running htcacheclean it made sense to try something recommended by someone I was in contact with at Tiger Technologies who recommended disabling the APACHE caching.

He recommended this because I was already utilising other caching technologies at the Wordpress plugin level and the errors in the log file suggested some sort of problem with APACHE's caching system. Apparently this is not a default option to be enabled and although I have no recollection of enabling it as I didn't set the server up it was obviously turned on.

After checking all the config files to ensure I wasn't making specific use of these Apache modules I disabled the following Apache Modules: disk_cache file_cache mem_cache with the following command in my console.

a2dismod

These all had to be disabled first before I could disable the main cache module that they all depended on.

After disabling all these caching modules I ran the following command to check that the syntax of all my Apache config files were correct and still in working order.

apache2ctl configtest


This returns "Syntax OK" if everything is okay.

A restart of Apache now didn't run the htcacheclean command and afterwards the problem seemed to dissipate.

One thing I have noticed coming from a Windows environment is just the pure amount of places that you can enable caching from the web-server level to MySQL, Wordpress, Plugins that make use of static files, PHP Accelerators such as APC and memcache for those with RAM to spare.

There are an awful lot of configuration possibilities with a LAMP setup and it seems that APACHE's own caching system was causing the problem probably due to the amount of memory it required for a busy site which was the cause of the large amount of disk swapping that was going on.

Maybe if I had a large amount of RAM to spare this wouldn't have been a problem but as I have shown in previous articles related to Twitter Rushes caused by large concurrent BOT visits after Tweeting links if you don't have lots of RAM and haven't lowered your MaxClients setting to an appropriate level from the default of 256 to 12-25, a 1GB RAM server can run out of memory very quickly indeed.

However whilst the Twitter Rush / MaxClients problem is easily detected by high server loads, a look at the visitor log file after Tweeting plus some maths to divide your RAM by the average amount of memory used by a page load plus any other applications that constantly run such as MySQL. This configuration issue was a bit harder to work out due to the fact I was experiencing very low server loads and thought I had done the right thing by utilising various caching techniques.

The Apache caching was obviously causing my server an issue and as always being able to methodologically diagnose a problem by stepping through the various possibilities ruling them out one by one is a technique that always works.