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 exploit the surrounding potential before a rival. They therefore proposed serving the busy port at Portsmouth, by building an 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. The site of Fareham railway station would have been quite a distance from the urban area, but new 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.
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.
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 while maintaining capacity. When it re-opened, it had only a single track, but the Deviation Line remained to cover travel towards Eastleigh, with the tunnel covering 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 Deviation Line was reduced to single track in the early 1970s, and then the construction of the M27 in 1974 caused it to be permanently closed. 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 interesting is the Meon Valley Line. Opened in 1903 (and justifying the Deviation Line's continued use), 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. On the west side were cattle pens (for transfer to the market) and coal hoppers, while on the east side were goods sheds and engineering works.
The station footbridge first appears on the 1909 map, although the barrow crossing remained an option until the 1990s. In 2012 the newer footbridge opened. Until the 1970s there was a café and lounge outside the station entrance.
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.
The new junctions were managed by two signal boxes: Fareham West to the south of the bridge and Fareham East to the north.
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 the Railway Tavern, to Railway Hotel, to a factory and then to a seedy nightclub (also Coyote Station; Cockerills), and is now a car park.
The whole station area is included under a housing review which could see it redeveloped to make more space for residences.
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 despite the Union attempting to keep it open.
The station had been 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, prompting the station name to change.
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.
In 1865, a railway was authorised from Fareham to Hill Head, but it was never built.