Question

Which one to use for my App back-end: Nginx or Apache?

I have this app/social network i’m about to launch. The back-end uses PHP+ MySQL to serve the Mobile app (iOS and Android) I read a lot of things but they are very confused. They are not CLEAR if an application using APACHE (get data from database receiving in json, load images, load and send messages, post text, etc) will really be faster just because it’s a dynamic content WHEN we’re scaling (like many people using at the same time). Some say it’s the same, some say apache is faster, some test i saw showed nginx faster than apache even for dynamic content when there’s more than 10 requests at the same time. Anyone would tell me if an API (php+mysql) for a mobile app would be faster on APACHE or NGINX? (DEFAULT installation, im not talking about ninja tricks) ??

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@MrCruz Apache with mod_php is a bit faster than Nginx with php-fpm. I love the simplicity of Nginx and would always go with that. And I find it much easier setting up multiple Nginx in load balancing, which is great for scaling.

But talking about speed and serving dynamic content, then it really hasn’t much to do with Apache or Nginx. Since you need to make sure your application will respond as fast as possible, so it doesn’t hold up one of the connections in Apache/Nginx. So this leads us to PHP and MySQL.

You’ll get much more difference in what version of PHP you’re running. Nothing below version 7.0 is worth looking at, since it’s almost twice as fast as earlier versions.

You might want to switch from MySQL to MariaDB, since it has some extra features. And remember to create correct indexes in your database, since that can easily make 10x the speed difference. Actually you should be using PostgreSQL with it’s JSON storage, but that would be a big change, so let’s skip that.

Make sure everything is cut to the bone and optimized. Optimization/tweaking/tuning actually makes a huge difference. If you’re not interested in that, then just spend more on CPU/RAM and that’ll “fix” it.

I would do a Nginx, php-fpm, MariaDB, and cache with memcached or Redis. All of these can be setup on the same server or each on their own server and multiplied for scale.

Dang, just reloaded and saw the typical great answer from @jtittle :-)

@jtittle and @hansen Yeah i agree with booth of you. My concerns really were if by choosing NGINX (over Apache), it would perceptibly affect the performance of the overall response of requests, “using standards at the beginning for nginx or apache” in case i get like 40k new users per week, for example. I never used NGINX before, so i don’t know. And i really already know that most of the work is done by my code as you guys mentioned. I have scaled other apps with different servers, but in different scenarios. (i didn’t have to setup all servers myself) I pay the most attention to my codes than to anything else. And have created my own techniques over time. I barely rely (or never) on the chosen platform, database or anything like that to deal with the logic, data processing, security, etc.
(I know most [99% are average] programmers will be like* “your code won’t ever be better than theirs, don’t re-invent the wheel”* but that i disagree) Yet i know the importance of taking the advantages from whatever we’re using, from hardware and low-level programming for performance. I started coding with c, c++ and Assembly. But i’m not a Masochist and like evolution too lol It’s cool that you guys mentioned MariaDB. I say MySQL but in fact i ALWAYS used MariaDB. So i’m happy you guys made it easier for me to decide keeping using it. PHP 7 i saw some people saying devs should wait it gets older and more stable. But looks like it’s a 100% GO, right? I will go for the PHP7. So… This time i want to set up the server myself for the beginning and only the essential for now. IF my plan works, more money will be injected and of course, someone will be doing advanced settings modifications, etc as you guys wisely pointed out.

@MrCruz

In regards to creating an API, neither NGINX or Apache would be handling any more than the initial request made by the user, which is then passed on to PHP (mod_php) or PHP-FPM (NGINX).

Your application would be responsible for handling everything else, such as encoding or decoding a JSON GET or POST request using PHP’s json_encode() or json_decode() functions, differentiating the requests (i.e. GET, POST, HEAD, etc), querying MySQL using PDO or MySQLi, etc.

NGINX and Apache simply function as web servers. They accept an incoming request and route it to a location depending on your configuration, if possible, else they throw an error. That error can be one that is setup to be handled by NGINX or Apache using configuration, or your application can handle it by using native PHP functions.

NGINX, in my experience, uses far less CPU and RAM to serve requests than Apache does, which is also one reason I prefer and use it myself, as well as on client websites that I manage or am hired to setup.

If you have any other specific questions, feel free to ask, I’m always happy to help!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.