Most popular prime-time shows aren’t run by a producer and star who has to finish shooting by 6 p.m. to rush to a night job waiting tables.

Then again, most shows aren’t like “It’s Always Sunny in Philadelphia.”

The irreverent comedy — created by Rob McElhenney, 33, who six years ago was making ends meet by working at a restaurant — revolves around a clutch of morally challenged misfits who own a dingy bar in South Philadelphia. McElhenney and two buddies write and star in the series, which this month began its sixth season on the FX cable channel.

“Sunny” has defied the odds in Hollywood. It spawned a fervent following by wading into taboo topics, including abortion, homophobia and child molestation. It overcame weak ratings and doubts of executives within Rupert Murdoch’s media conglomerate, News Corp., which owns FX.

Most important, the show has demonstrated that there’s a way to make low-cost, high-quality, scripted comedies at a time when studios are struggling to rein in costs.

Now, other networks are seeking to copy the success of “Sunny.” They also want to produce a half-hour comedy for $400,000 per episode — about one-fourth the industry average. The survival of Hollywood’s bread-and-butter business of scripted TV depends upon it: audiences are scattering, cheap reality programs are multiplying and technology threatens to unravel the entire system.

“Television is not like making bluejeans; we can’t make it in China,” said John Landgraf, general manager of FX and a 20-year television veteran. “There is a labor base in America, here in California, and if we don’t figure out different business models, then we won’t be able to make as much content.”

Landgraf worries a lot about how to make compelling shows at affordable prices. After he arrived at FX in 2004 — leaving his job as head of Danny DeVito’s Jersey Television — he wanted to add comedies to bring new viewers to the network. FX had made a name for itself with dark dramas “The Shield” and “Nip/Tuck” but had no luck with lighter fare.

Its best effort, “Lucky,” a show about compulsive gamblers, failed to muster high-enough ratings to cover the show’s cost of $720,000 an episode.

“My belief was that we didn’t have to spend that much money to make a quality comedy,” Landgraf said.

About that time, McElhenney’s agents were touting his concept for a show. Landgraf agreed to take a meeting “as a favor” to McElhenney’s managers. It wasn’t the usual show pitch. Instead of describing characters and plot lines, McElhenney popped a copy of a show he had made with his friends into the DVD player in Landgraf’s office.

The 26-minute episode, called “Charlie Has Cancer,” centered on three self-absorbed actors, played by real-life friends McElhenney, Charlie Day and Glenn Howerton. In the pilot, all were vying for the same role of a terminally ill patient, and Day’s character, Charlie, fakes having cancer to enhance his chances. McElhenney wrote, directed and operated a video camera — and nobody was paid. Total cost: $100 for camcorder tapes and pizza.

Landgraf, a former NBC programming executive, was struck by the off-kilter humor and gave them $400,000 to reshoot the pilot with a professional crew. He didn’t like the premise about struggling actors obsessing over their careers, so the characters were switched to running a dive bar in South Philly, where McElhenney grew up. A female lead was recast.

Landgraf gave the group a wide berth creatively, encouraging them to find their voice for the show.

Expectations were low. McElhenney, Howerton and Day were promising actors but had no experience producing television. When Landgraf ordered the first season, he set a budget of $450,000 per episode, less than a third the cost of a network sitcom.

To stay on budget, they cut corners. Network comedies typically take a week to shoot, but the “Sunny” crew shot scenes out of order and three episodes at a time. They used the old Los Angeles Herald-Examiner plant as a set, which was cheaper than a soundstage, and the stars shared a trailer, which reeked of urine. McElhenney estimated he earned $70,000 during the first season — on par with what a mid-level actor can earn per episode for a network show.

And, just to play it safe, he kept waiting tables at Cafe D’Etoile in West Hollywood. “They weren’t paying us enough money at the time to completely support myself,” McElhenney said. “I would direct episodes, produce and act and wrap shooting around 6 so I could go home and change into my uniform.”

The comedy was unlike anything seen on TV. In “Charlie Wants an Abortion,” they get into the abortion debate. McElhenney’s character, Mac, joins up with female anti-abortion activists simply to score. In “Underage Drinking: A National Concern,” the gang realize their new customers are high school students. Instead of kicking them out, Mac decides “we have a social responsibility to provide a safe haven for these kids to be kids.” Solution: Serve watered-down drinks.

Viewers weren’t tuning in. Still, Landgraf thought the episodes were funny, and given the relatively low cost, the network could afford to stick with it in the hope it might catch on. He had also been sending episodes to his former business partner DeVito, believing the actor’s comedic sensibilities were in sync with the dark tone of “Sunny.”

Hoping a bigger name would win the show some attention, Landgraf suggested DeVito join the series, even though he hadn’t regularly appeared on TV since 1983, when “Taxi” ended.

“We needed to do something because nobody was watching,” McElhenney said. DeVito’s character, Frank, was introduced as the father of two of the characters. He horns into the group’s schemes and moves into Charlie’s ratty apartment, where they both lounge about in long underwear. This season, they contemplate a gay marriage so that Charlie can qualify for Frank’s health insurance.

With DeVito on board for the second season, Landgraf doubled down. The budget of the show mushroomed to more than $800,000 per episode to pay for DeVito and raises for the four others. But people still weren’t watching. Unhappy at losing more money, Fox higher-ups threatened to pull the plug.

Now Landgraf needed more revenue to cover the higher costs. FX executives labored to persuade their counterparts in Fox’s home video division, which didn’t believe the episodes would sell, to release “Sunny” on DVD. Once they did, sales were surprisingly strong. It would go on to become a top seller for a Fox show, behind only “The Simpsons” and " Family Guy.”

Then a tweak in audience measurement yielded a dramatic result. In 2007, ratings firm Nielsen Co. began including college students in its sample. This was — no surprise — the show’s fan base, so FX began a marketing campaign on campuses. That fall, the show’s ratings jumped 24%.

The breakthrough, however, came when episodes were put on the video website Hulu — partly owned by Fox — when it launched early in 2008. “Sunny” quickly shot into the pantheon of “most popular” shows.

“People who had been hearing about the show suddenly had an easy opportunity to watch it at work,” McElhenney said. “We saw everything skyrocket from there.”

But when the fourth season began later that year, the wider exposure “Sunny” received on Hulu didn’t lead to an expected bounce in FX viewers.

“When the ratings didn’t go up in the fourth year, we were worried the incremental growth that we achieved had accrued to Hulu — and not to FX,” Landgraf said.

In 2009, the studio asked Hulu to remove episodes of “Sunny” — prompting howls of protest from fans. A few episodes were restored, but the controversy marked a realization among TV executives that Hulu — envisioned as a way to combat online piracy — might be doing more harm than good by drawing viewers away from TV, which pulls in the big ad dollars.

A few months later, the cast staged a live performance of an episode, “The Nightman Cometh,” at the Troubadour in West Hollywood. In late summer, they took their bizarre musical, with its references to child molestation, on the road. Fans showed up dressed like “Sunny” characters and, as if attending " The Rocky Horror Picture Show,” chanted lines from the songs.

“It turned out to be an amazing boost for the show,” DeVito said. “We knew there were rabid fans, but we finally got an up-close look at them.”

Last season opened with a bang; ratings shot up 60%. By now, 4.3 million people were watching the series on FX.

Viewers weren’t the only ones now following the show. That fall, a bidding war broke out for the rights to the reruns. Comedy Central agreed to a more than $33-million deal, paying FX $400,000 an episode along with a portion of the ad revenue. When combining the ad sales for the episodes that eventually will air on local TV stations, FX is expected to rake in more than $800,000 an episode. That is the amount NBC’s Emmy-winning comedy “30 Rock” will collect before advertising.

Five years after its debut, “Sunny” reached a milestone: It turned profitable. McElhenney also married his costar, Kaitlin Olson, and last month she gave birth to their first child.

The show’s budget now is about $1.5 million per episode — on par with a network sitcom. It shoots in HD on the Fox movie lot with more extras, adding to the cost. The size of the writing staff tripled, and, in a sign of its hip factor, includes the two sons of former News Corp. President Peter Chernin.

“It’s no longer inexpensive, but when you combine the syndication values with massive DVD sales and merchandising and everything else, it turns out to be a very profitable show,” Landgraf said.

TV studio chiefs say most producers will continue to chase the fatter paycheck of a network show. Those checks are a major reason the cost of TV shows is so hard to contain. But FX now has three low-cost comedies, “Archer,” “Louie” and “The League,” financially modeled after “Sunny.” On each show the writers and producers accepted lower upfront fees in exchange for a bigger share of the profits if the shows hit.

“We’ve actually kind of invented and perfected a new business model,” Landgraf said. “There is a connection between creative freedom and price.”

Rich Frank, a longtime TV executive, has a pilot at FX called “Wilfred,” starring Elijah Wood, star of “The Lord of the Rings.” The show is about a woman, her dog and her boyfriend. Only the boyfriend can see that the dog, Wilfred, is a man dressed in a bad canine costume. The pilot was shot in Venice and used the back of a church for some scenes. None of the actors had trailers, and the crew had to contend with the noise of jets flying overhead.

“Instead of taking 12 takes, we take two or three and move on,” Frank said. “The pilot looks as good as anything else on the air and it cost less than half what I would normally produce it for. And the best part is that we are doing it here in Los Angeles at a time when everyone else is leaving California.”

meg.james@latimes.com