Practice 1 of Nginx: replace CentOS Apache with nginx

Time:2020-6-19

Background introduction: Alibaba cloud, 512M memory (the most loser configuration), set up a lamp environment, except that MySQL has been allocated about 100m (this can’t be less), HTTP has occupied more than 200m, which is too large, so we decided to replace it with a lighter, high concurrency nginx.

Background data

As shown in the figure below: the system is only 500m, which takes up 100m of MySQL. Httpd accounts for more than 1 / 2 (often up to a dozen processes). The remaining 50m, sometimes even less, can’t bear it. It often causes database crash and writes an automatic restart script, but it’s not a cure

#Count the number of Apache processes
ps aux|grep httpd | wc –l

Practice 1 of Nginx: replace CentOS Apache with nginx

Solutions

  • 1: Optimized for Apache. Including optimizing the operation mode of workers and so on. You can refer to Apache optimization

  • 2: replace the lightweight server. Adopt lighter servers such as nginx or lighthttpd. According to the legend, nginx is a better way to achieve load balancing and high concurrency, and decides to practice it.

Replace Apache with nginx

  • 1: Stop Apache
    sudo service httpd stop

    Note: in case, it’s better not to unload it in advance.

  • 2: Install nginx
    yum install nginx

  • 3: Start nginx
    sudo nginx

    After successful installation, the startup is as followsPractice 1 of Nginx: replace CentOS Apache with nginx

  • 4: Simple configuration nginx
    It is mainly to modify the log [easy to trace PROBLEMS] and the web_ Root file [quick start website]

  • 5: Restart nginx
    [[email protected] nginx]# nginx -s quit
    [[email protected] nginx]# nginx

    As shown in the figure below, the web directory configuration succeeded!Practice 1 of Nginx: replace CentOS Apache with nginx

  • 6: Add PHP support
    Install PHP FPM
    yum install php-fpm

    nginx.conf set up
    location ~ \.php$ {

       root /var/www/html;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME         /var/www/html$fastcgi_script_name;
       include fastcgi_params;

    }

  • 7: Restart the service, website reply.
    Practice 1 of Nginx: replace CentOS Apache with nginx

  • 8: A simple comparison of consumption and consumption is shown as follows: 200m is basically saved, although this may be the data at the initial stage of operation; however, it is still a lot lighter, with each service accounting for about 1 / 4 of the total storage and many fewer threads. Performance in terms of memory usage. I think it’s OK. Next, it depends on performancePractice 1 of Nginx: replace CentOS Apache with nginx

follow-up

The first time I contacted nginx, I felt it was pretty good overall. In the future, basic anti attack, multi port setting and performance configuration will be carried out.

Original link of personal website