Ten Rules for large and high-performance Web Sites

Source: Internet
Author: User
Summarizes the practical experience and lessons learned by domestic network and game companies in running backend servers. These rules apply to large systems with daily access volumes ranging from 10 thousand to millions. In our company ChinaNetCloud, we have seen many different types of websites and systems, which are both good and bad. Some of these systems have a good server/network architecture and have been properly adjusted and monitored. However, general systems have security and performance problems and cannot run well, and cannot become more popular. In China, open-source LAMP stack is the most popular network architecture.

Summarizes the practical experience and lessons learned by domestic network and game companies in running backend servers. These rules apply to large systems with daily access volumes ranging from 10 thousand to millions.

In our company ChinaNetCloud, we have seen many different types of websites and systems, which are both good and bad. Some of these systems have a good server/network architecture and have been properly adjusted and monitored. However, general systems have security and performance problems and cannot run well, and cannot become more popular.

In China, the open-source LAMP stack is the most popular network architecture. It is developed using PHP and runs on an Apache server. It uses MySQL as a database and runs on Linux. It is a reliable platform that runs well and is now the world's most popular Internet system architecture. However, it is difficult for us to correctly scale and maintain security because each application layer has its own problems, defects, and best practices. Our job is to help enterprises create and run high-performance, scalable, and secure systems at the lowest operating costs. Therefore, we have a wealth of experience with such problems.

The actual situation is that many websites are created quickly and cheaply by developers. Generally, there are no IT staff or managers, but programmers only manage the system. The result is that, although the website can start to run at a very low cost, it usually faces real problems when there are a large number of users who need to scale up. After all, there are 0.3 billion million Internet users in China. If 80 million of them access this site, it will easily lead to 0.01% ~ 0.5 million of page traffic. These problems are generated at all levels. The rules summarized below are an overview of the most common problems, and explain why these rules are so important and the best way to fix them. Websites that follow these suggestions will improve scalability, security, and operational stability.

1. Use appropriate session management

The first way to expand the system is to add more hardware. For example, use two servers instead of one. This sounds reasonable, but may cause potential problems: session management. This is a serious problem for Java programs, and may also cause scalability problems in PHP, especially for database load.

A session is defined as a separate end user login or connection for a period of time. It usually contains multiple TCP/IP HTTP connections and several Web pages, it usually includes dozens or even hundreds of page elements, such as frameworks, menus, and Ajax updates. All these HTTP requests need to know who the user is to meet the security requirements and send appropriate content to the user, because these are part of the session. Generally, each session includes associated session data, such as user name, user ID, history, shopping cart, and statistics.

The problem is that when there are two Web servers and multiple HTTP connections, user traffic will be allocated and moved between the two servers. It is difficult for the server to know who the user is, and track all the data, because each page or page component may come from different servers. In PHP, a session ID is created and put in the Cookie during the first connection or login. Then, the Cookie is sent together with each HTTP request.

This poses a problem. Next, each PHP script needs to search for session data based on the ID. Since PHP cannot maintain state during execution (different from Java), the session data needs to be stored somewhere, usually in the database. However, if a complex page needs to be searched ten times during each page loading process (this is often done), it means that each page needs to perform ten SQL queries, this will cause a large load on the database.

In the preceding example of 0.01% Internet users in China, it may be easy to generate hundreds of queries per second for the sake of management sessions. The solution is to always use the session ID in the Cookie and use services such as Memcached to cache session data for high performance.

Pay attention to the security issues, because hackers can forge the session ID of another user, which is easily found or seen, especially in public Wi-Fi. The solution is to properly encrypt or sign the session ID and bind it with the time interval, IP address, and other key information such as the browser or other details. There are many good examples of good session management on the Internet, you can find the most suitable as needed.

2. Always consider security

Although writing code like preventing SQL injection and login security involves many security issues, unfortunately, almost no one has considered security, those who consider it do not have a good understanding of it. This article focuses on operational system security. For this type of security, we focus on three security areas: Firewall, operating users, and file access permissions.

In addition to configuring dedicated hardware firewalls (such as Cisco's ASA), all servers should also run firewalls such as Iptables, which protect servers from other threats and attacks. These threats and attacks may come from the public Internet, other servers, or local servers. They also include developers and operators who use VPN or SSH channels. We only open the required port to the specified IP address. Iptables may be complex, but there are many good templates that we can usually use to help customers create Iptables. For example, the default RedHat or CentOS firewall configuration is only 10 lines, which is obviously not practical. Our best practice of Iptables configuration has about five pages, which includes the most advanced security protection provided by Linux.

All public services should run under special users, such as Apache. Never run with the Root user, because this will allow any user that breaks into Apache to take over the entire server. If Apache runs only under the Apache user or the Nobody, it is not easy to break into Apache.

The files (.php and .html files) of the Web server running the Web server should not be writable. This means that Apache or Nginx users should not have the write permission for the Web directory. There are many ways to do this, and the simplest is to use these files as owned by other users, then, users such as Apache and Nginx are assigned to a group that can read files with 640 permissions. This prevents almost all hackers and page attacks.

In addition, never use Ftp to upload files, especially in public Wi-Fi environments, because hackers can easily steal user names and passwords. Instead, Sftp is more secure. In addition, each employee should have his/her own user ID and random password.

3. Use the standard path and installation Configuration

An annoying deployment problem is that developers seldom consider where their software will be deployed to production Web servers and how to deploy it. We have seen many large systems deploy their PHP code in the/home/xiaofeng or/web/code path. In fact, these two paths are very nonstandard and may cause operational and security problems. When these systems are transferred from the development environment to the test environment and then to the production environment, because each installation configuration is non-standard, problems often occur. In this case, developers need to adjust the configuration to work properly.

You should always use standard installation packages and binary files to install servers such as Apache. Do not compile or install Tarball from the source code, because it may cause long-term stability and management problems. Installing multiple versions on the server may also cause confusion.

The Web site should always be tested and deployed under the specified platform and standard path released by Linux, such as the/var/www/html path under RedHat or CentOS. This facilitates effective permission management, backup, configuration, monitoring, and other operations on the system.

Web server logs should be stored in/var/logs or/var/logs/app_name, rather than in the main code area. The reason for doing so is not only because these standard paths are very important, but also because the server is properly configured to configure/var as a separate file system. If the application suddenly writes a large amount of logs and occupies all disk space, the system will not crash or other serious problems because of the above configuration. If the log is located elsewhere, problems may occur.

4. Always use logs

The number of logs made in the Web system cannot exceed. All systems should write important data into logs, whether their own logs or system Syslog. Cron jobs and other Shell scripts or C language programs have corresponding standards and simple functions for logs. In Shell scripts, you only need to use the Logger command to write logs. Write logs when the script starts/stops, important script execution, and real-time data are generated. In this case, you can view the main system logs to easily see what happened.

Large systems often use specialized tools such as Local5 to record logs, and configure Syslog or Syslog-ng to store logs in separate files, which makes it easier to use. Note that the Syslog tool and Logger (and any Syslog call) Give priority to user. notice by default. you can adjust it if necessary.

A good system will configure the program to enable or disable logs, and you can choose to apply different levels of logs at each module or function level. This allows us to record very detailed and powerful logs for analyzing and debugging problems occurring in production operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.