Next came frontier duty in several Texas installations, including at Fort Graham in 1852 to 1853, at Fort McKavett from 1853 to 1854, at San Antonio in 1854, and at Fort McKavett in 1855. He was promoted to captain on March 3, 1855, and was stationed at Fort Riley, Kansas, from 1855 to 1856. Anderson was still serving in Kansas during the border troubles of 1856 and 1857, then was recruiting as well as his last stint at the Carlisle Barracks in 1858. He participated in the Utah War of 1858 and 1859, and was on duty at Fort Kearny, Nebraska, from 1859 to 1861. Anderson chose to follow his home state and the Confederate cause, and he resigned from the U.S. Army (accepted on March 3, 1861) to enter service with the Confederate Army. Anderson accepted a commission as colonel of the 1st South Carolina Regulars as of January 28. He was given command of the Charleston harbor area after the capture of Fort Sumter that April. He was promoted to brigadier general on July 19 and transferred to Pensacola, Florida, where he was wounded in the left elbow during the Battle of Santa Rosa Island on October 9. After recovering, Anderson joined the Confederate Army of the Potomac in February 1862 (which was absorbed into the Army of Northern Virginia later in the spring) as a brigade commander. Anderson distinguished himself during the Peninsula Campaign, temporarily assuming division command at Seven Pines when Longstreet was serving as a wing commander. At Seven Pines he gained the sobriquet "Fighting Dick", and was promoted to major general on July 14 and received command of General Benjamin Huger's former division. As part of Longstreet's corps, Anderson fought at Second Bull Run. His division engaged the final Union defensive line around Henry House Hill, but the sun was starting to go down and he did not press the attack. During the Maryland Campaign, General Cadmus Wilcox's division was added to Anderson's command. At the Battle of Antietam in September 1862, he was in overall command at the sunken road, or "Bloody Lane", in the center of the Confederate defense. He was wounded in the thigh and left the battle. His senior brigadier Roger A. Pryor took over command. After Anderson's departure, his division began to falter and eventually succumb to Union flank attacks that routed them from their position. At the Battle of Fredericksburg that December, his division was not heavily engaged.