search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP security configuration under lnmp (disabling unsafe PHP functions and separating PHP uploads, etc.)

This article introduces the method of PHP security configuration under the lnmp architecture, including disabling unsafe PHP functions, turning off PHP error logs, PHP upload separation, turning off PHP information, disabling dynamic loading of link libraries, disabling opening remote URLs and other security measures.

Several aspects of php security configuration:

1. Use open_basedir to limit virtual host cross-directory access

[HOST=www.xingzuo51.com] open_basedir=/data/site/www.xingzuo51.com/:/tmp/ [HOST=test.xingzuo51.com] open_basedir=/data/site/test.xingzuo51.com/:/tmp/

Instructions: The PHP program under www.xingzuo51.com is limited to the two directories configured by open_basedir, and cannot access other directories. If the above configuration is not done, the programs of test.xingzuo51.com and www.xingzuo51.com can access each other. If one of the sites has a vulnerability and a hacker has implanted a webshell, you can use this site to take down other sites on the same server and finally install a Trojan horse.

Note: The directory must be appended with / at the end. For example, if you write /tmp, the site also has /tmp123 and other directories starting with /tmp, then hackers can also access these directories. In addition, php5.3 and above support this writing method, but 5.2 does not.

2. Disable unsafe PHP functions disable_functions = show_source,system,shell_exec,passthru,exec,popen,proc_open,proc_get_status,phpinfo PHP is prohibited from executing the above PHP functions. The above PHP programs can execute Linux commands, such as ping, netstat, mysql, etc. If your system has a privilege escalation bug, you will know the consequences.

3. Pay attention to software security information Actively pay attention to the Linux kernel, PHP security and other information and take timely action on errors

4. PHP users can read only This method is my most recommended method, but you must discuss it with the PHP engineer before executing it. Why? For example, the user and group in the root directory of the site www.xingzuo51.com are nobody, and the user and group running php are phpuser. The directory permissions are 755 and the file permissions are 644. In this way, php is read-only and cannot write any files to the site directory. In other words, users cannot upload files. Even if there are loopholes, hackers cannot pass the backdoor, let alone install Trojans. Before doing this, tell the programmer to change the file cache to NoSQL memory cache (such as memcached, redis, etc.), and the uploaded files will pass through The interface is passed to other servers (static servers). Note: It is a very bad habit to program to generate local cache. Using file cache is slow and wastes disk space. The most important thing is that the server cannot scale horizontally under normal circumstances

5. Close php error log

display_errors = On Change to display_errors = Off

Once an error occurs in the program, detailed error information will be immediately displayed to the user, including the path, and sometimes even the database account password. Injection and penetration passwords are basically guessed through this error report. Strongly turn it off in production environments

6. PHP upload separation Upload files to remote server such as nfs etc. Of course, you can also call the PHP interface you wrote. Even if there is an upload vulnerability, the file will be transferred to the static server. Trojan horses and other files cannot be executed at all.

Example: php site www.xingzuo51.com, directory/data/site/www.xingzuo51.com Static file site static.xingzuo51.com, directory/data/site/static.xingzuo51.com The file was directly transferred to /data/site/static.xingzuo51.com. The uploaded file cannot be accessed through www.xingzuo51.com. It can only be accessed through static.xingzuo51.com, but static.xingzuo51.com does not support php.

7. Close php information

expose_php = On Change to expose_php = Off

Do not disclose your PHP version information easily to prevent hackers from launching attacks against this version of PHP.

8. Disable dynamic loading of link libraries

disable_dl = On; Change to enable_dl = Off;

9. Disable opening remote url

allow_url_fopen = On Change to allow_url_fopen = Off

In fact, this is not really safe and will not lead to problems such as web intrusion. However, this greatly affects performance. The author believes that it is a security issue in a narrow sense.

The following methods will not be able to obtain the remote url content

$data = file_get_contents("http://www.baidu.com/");

Get local file content:

$data = file_get_contents("1.txt");

If the site traffic is not large and the database is running well, but the web server load is surprisingly high, please check directly to see if this method is available.

php obtains the content of the remote web, it is recommended to use curl.



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment