There are of course whole books written about Hampshire's railway network. We'll skip over much of it to focus only on parts which affect the borough of Fareham, but even doing that involves telling a long story.
A convenient location - the first effort
In 1840, the London and Southampton Railway company opened what is today the mainline between London and Southampton. Their railway was huge success, and with the railway industry becoming increasingly competitive, the owners were keen to access their surrounding network before a rival. They therefore proposed serving the busy port at Portsmouth, by building a short extension from Eastleigh. There appears to be no proposed route for this line, but logically it would have stopped at Fareham - but probably on the north side to avoid any viaducts having to be built. A look at a map suggests crossing the Uplands area (which at the time would have been mostly farmland) would be the most direct route, but local knowledge suggests this would have required significant earthworks. In a time before any major roads or local landmarks, this short-lived proposal could have changed the town we know considerably.
In the event, Portsmouth objected to its city being an afterthought on the existing busy Southampton route, and to its port being served by a company named after Southampton, so the plan was blocked.
A secondary angle
Still keen to hold a monopoly on the south coast, the London and Southampton Railway renamed itself London and South Western Railway and in 1841 opened a route to Gosport instead. The ruler-straight line was built from Eastleigh (at the time a very insignificant town near a railway), passing Fareham to the west. At the time, the site of Fareham railway station would have been quite a distance from the urban area, but facilities quickly filled the gap. This offered ferry connections to Portsmouth and to the Isle of Wight via Stokes Bay and Queen Victoria's favourite route via Royal Clarence Yard.
To the north of Fareham, the steep hill was avoided using a long tunnel. The clay in the land caused the tunnel to collapse during and after construction. In 1904, an alternative route was built in a hill cutting known as the Deviation Line, which allowed the tunnel to be repaired and maintained capacity. The Deviation Line was then used for travel to Eastleigh, and the tunnel for travel to Wickham via Knowle. As is a common theme on the railways, in the 20th century competition from road travel caused demand to decline. The construction of the M27 caused the Deviation Line to be permanently closed in 1974. Its route can still be clearly seen on aerial photos, and the majority of it can be followed as a trail.
During the railway's heyday, many extensions were proposed and built, but perhaps the most interest is the Meon Valley Line. Opened in 1903 (and justifying the Deviation Line mentioned above), this railway cut the corner between Wickham and Alton and in doing so created a new route from London to Fareham and the Isle of Wight. The line was well-built to high engineering standards, and with grand station buildings despite the majority serving insignificant places. Enthusiasts cannot agree on why this route was seen as so desirable, but it was closed due to very low usage in 1955.
With smaller lines opening and closing quite quickly during the 19th and 20th Century, other stations to have been built locally include Gosport, Fort Brockhurst, Lee-on-the-Solent, Knowle Asylum Halt/Knowle Platform, Wickham, Droxford and ownwards to Alton and London, Botley, Bishops Waltham, Bishopstoke/Eastleigh, Swanwick, Bursledon, Netley (and onwards to Southampton), Portchester, Paulsgrove, Cosham, Havant, Langstone, Hayling Island, East Southsea, Portsmouth Town and Portsmouth Naval Base. Many of the more rural stations did more with the movement of strawberries than it did passengers: Swanwick was one of the busiest in the country because of it.
At its peak, Fareham would have had four public platforms, with the numbering having changed several times. Today's platforms 2 and 3 are the original platforms for travel towards London or Gosport. New lines were then built either side, creating a new western platform for travel from Gosport to London, and an eastern that terminated where the car park is today. In addition, there were goods sheds either side of the station, now industrial units. The eastern became part of the goods operation and was used until the 1970s.
Outside the station entrance was a café and lounge until the 1970s.
Had the distinctive viaducts towards Portsmouth been built earlier, logically we could have expected a station to be built by the viaduct, next to the town centre. History playing out the way it did meant Fareham already had its current station, which means trains coming from Portsmouth go past the town centre before stopping away from it.
The link towards Portsmouth opened in 1848, and towards Southampton opened in 1889. It is the former link which is the more noticeable one, now being the only route south of the station and running along the large viaducts along the water's edge, for which Fareham is known.
Fareham station is located roughly where the downhill line leaves ground level and starts crossing an embankment, and of course the long brick viaducts. For this reason it has a lengthy, flat driveway, leading to the once-fine station building.
It is hard not to mention Prague Junction. This managed to go from a fine hotel, to a seedy nightclub (also Coyote Station; Cockerills), and is now a car park.
Nothing to do with Fareham station itself, but worth mentioning while we're talking about railways as it is (unless I've missed something) the only closed railway station within Fareham's boundaries.
Knowle Asylum Halt opened in 1907 to serve the county Lunatic Asylum in Knowle, deliberately placed far from civilisation. It closed in 1964 after the Union attempted to keep it open.
It was later renamed Knowle Platform and Knowle Halt, and was a short platform served only by the train coming from Wickham towards Fareham, and again in the other direction. Towards the end of its life, the station was served during visiting hours only. The Lunatic Asylum itself became more inoffensivly known as Knowle Hospital, hence the station name changing.
The single platform is heavily overgrown, although with more of the former hospital site being used for housing, there is often talk about the possibility of re-opening it.
The station was managed by two signal boxes: Fareham West to the south of the bridge and Fareham East to the north.