Heat the olive oil in a large skillet over medium heat.
Add the minced garlic and diced onion to the skillet and sauté, stirring occasionally, until the onion is soft and fragrant, about 3–5 minutes.
Add the whole peeled tomatoes with their juice, crush them slightly with a spoon, then season with salt, freshly ground black pepper, and crushed red pepper flakes (if using).
Bring the sauce to a gentle simmer and cook, uncovered, stirring occasionally, for about 20 minutes to blend the flavors and thicken slightly.
While the sauce simmers, bring a large pot of salted water to a boil. Cook the spaghetti until al dente, about 8–10 minutes, then reserve 1/2 cup of the pasta cooking water and drain the pasta.
Stir the chopped basil into the sauce. Add the drained spaghetti to the skillet and toss to combine, adding reserved pasta water a little at a time to loosen the sauce as needed.
Divide the spaghetti among plates, garnish with extra basil, and serve with grated Parmesan cheese if desired.