Heim Backend-Entwicklung PHP-Tutorial Nginx and PHP-FastCGI on CentOS 5

Nginx and PHP-FastCGI on CentOS 5

Jun 23, 2016 pm 02:32 PM

Nginx and PHP-FastCGI on CentOS 5

Published: Monday, December 14th, 2009 by Phil Paradis

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP and FastCGI on your CentOS 5 Linux VPS.

It is assumed that you've already followed the steps outlined in our getting started guide. These steps should be performed via a root login to your Linode VPS over SSH.

Contents

Basic System Configuration Install Required Packages Configure Your Site Configure spawn-fcgi Test PHP with FastCGI More Information

Basic System Configuration

Issue the following commands to set your system hostname, substituting a unique value for "plato."

echo "HOSTNAME=plato" >> /etc/sysconfig/networkhostname "plato"
Nach dem Login kopieren

Edit your /etc/hosts file to resemble the following, substituting your Linode's public IP address for 12.34.56.78, your hostname for "plato," and your primary domain name for "example.com." :

File:/etc/hosts

127.0.0.1   localhost.localdomain   localhost12.34.56.78 plato.example.com   plato
Nach dem Login kopieren

Install Required Packages

CentOS doesn't include nginx in their repositories, so we'll need to add support for EPEL (Extra Packages for Enterprise Linux) from the Fedora project. Issue the following command:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
Nach dem Login kopieren

Issue the following commands to update your system and install the nginx web server, PHP, and compiler tools:

yum updateyum install nginx php-cli php make automake gcc gcc-c++ spawn-fcgi wgetchkconfig --add nginxchkconfig --level 35 nginx onservice nginx start
Nach dem Login kopieren

You'll be asked to accept the key for EPEL, as it gets imported the first time you install an EPEL package. Various additional dependency packages will be installed along with the ones we requested. Once the installation process finishes, you may wish to make sure nginx is running by browsing to your Linode's IP address (found on the "Remote Access" tab in the Linode Manager). You should get the default ngnix page.

Configure Your Site

In this guide, we'll be using the domain "example.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:

mkdir -p /srv/www/www.example.com/public_htmlmkdir /srv/www/www.example.com/logschown -R nginx:nginx /srv/www/www.example.com
Nach dem Login kopieren

Issue the following commands to create virtual hosting directories:

mkdir /etc/nginx/sites-availablemkdir /etc/nginx/sites-enabled
Nach dem Login kopieren

Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for "include /etc/nginx/conf.d/*.conf":

File excerpt:/etc/nginx/nginx.conf

# Load virtual host configuration files.include /etc/nginx/sites-enabled/*;
Nach dem Login kopieren

Next, define your site's virtual host file:

File:/etc/nginx/sites-available/www.example.com

server {    server_name www.example.com example.com;    access_log /srv/www/www.example.com/logs/access.log;    error_log /srv/www/www.example.com/logs/error.log;    root /srv/www/www.example.com/public_html;    location / {        index index.html index.htm index.php;    }    location ~ \.php$ {        include /etc/nginx/fastcgi_params;        fastcgi_pass  127.0.0.1:9000;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;    }}
Nach dem Login kopieren

Important security note: If you're planning to run applications that support file uploads (images, for example), the above configuration may expose you to a security risk by allowing arbitrary code execution. The short explanation for this behavior is that a properly crafted URI which ends in ".php", in combination with a malicious image file that actually contains valid PHP, can result in the image being processed as PHP. For more information on the specifics of this behavior, you may wish to review the information provided on Neal Poole's blog.

To mitigate this issue, you may wish to modify your configuration to include a try_files directive. Please note that this fix requires nginx and the php-fcgi workers to reside on the same server.

location ~ \.php$ {    try_files $uri =404;    include /etc/nginx/fastcgi_params;    fastcgi_pass 127.0.0.1:9000;    fastcgi_index index.php;    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;}
Nach dem Login kopieren

Additionally, it's a good idea to secure any upload directories your applications may use. The following configuration excerpt demonstrates securing an "/images" directory.

location ~ \.php$ {    include /etc/nginx/fastcgi_params;    if ($uri !~ "^/images/") {    fastcgi_pass 127.0.0.1:9000;    }    fastcgi_index index.php;    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;}
Nach dem Login kopieren

After reviewing your configuration for potential security issues, issue the following commands to enable the site:

cd /etc/nginx/sites-enabled/ln -s /etc/nginx/sites-available/www.example.comservice nginx restart
Nach dem Login kopieren

You may wish to create a test HTML page under /srv/www/www.example.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (PHP will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode's IP address.

Configure spawn-fcgi

Issue the following command sequence to download scripts to control spawn-fcgi and php-fastcgi, set privileges, make the init script run at startup, and launch it for the first time:

cd /optwget -O php-fastcgi-rpm.sh http://library.linode.com/assets/696-php-fastcgi-rpm.shmv php-fastcgi-rpm.sh /usr/bin/php-fastcgichmod +x /usr/bin/php-fastcgiwget -O php-fastcgi-init-rpm.sh http://library.linode.com/assets/697-php-fastcgi-init-rpm.shmv php-fastcgi-init-rpm.sh /etc/rc.d/init.d/php-fastcgichmod +x /etc/rc.d/init.d/php-fastcgichkconfig --add php-fastcgichkconfig php-fastcgi on/etc/init.d/php-fastcgi start
Nach dem Login kopieren

Test PHP with FastCGI

Create a file called "test.php" in your site's "public_html" directory with the following contents:

File:/srv/www/www.example.com/public_html/test.php

<?php echo phpinfo(); ?>
Nach dem Login kopieren

When you visit http://www.example.com/test.php in your browser, the standard "PHP info" output is shown. Congratulations, you've configured the nginx web server to use PHP-FastCGI for dynamic content!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Apr 06, 2025 am 12:02 AM

Die Hijacking der Sitzung kann in den folgenden Schritten erreicht werden: 1. Erhalten Sie die Sitzungs -ID, 2. Verwenden Sie die Sitzungs -ID, 3. Halten Sie die Sitzung aktiv. Zu den Methoden zur Verhinderung der Sitzung der Sitzung in PHP gehören: 1. Verwenden Sie die Funktion Session_regenerate_id (), um die Sitzungs -ID zu regenerieren. 2. Store -Sitzungsdaten über die Datenbank, 3. Stellen Sie sicher, dass alle Sitzungsdaten über HTTPS übertragen werden.

Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Apr 03, 2025 am 12:04 AM

Die Anwendung des soliden Prinzips in der PHP -Entwicklung umfasst: 1. Prinzip der Einzelverantwortung (SRP): Jede Klasse ist nur für eine Funktion verantwortlich. 2. Open and Close Principle (OCP): Änderungen werden eher durch Erweiterung als durch Modifikation erreicht. 3.. Lischs Substitutionsprinzip (LSP): Unterklassen können Basisklassen ersetzen, ohne die Programmgenauigkeit zu beeinträchtigen. 4. Schnittstellen-Isolationsprinzip (ISP): Verwenden Sie feinkörnige Schnittstellen, um Abhängigkeiten und nicht verwendete Methoden zu vermeiden. 5. Abhängigkeitsinversionsprinzip (DIP): Hoch- und niedrige Module beruhen auf der Abstraktion und werden durch Abhängigkeitsinjektion implementiert.

Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Mar 31, 2025 pm 11:54 PM

So setzen Sie die Berechtigungen von Unixsocket automatisch nach dem Neustart des Systems. Jedes Mal, wenn das System neu startet, müssen wir den folgenden Befehl ausführen, um die Berechtigungen von Unixsocket: sudo ...

Wie debugge ich den CLI -Modus in PhpStorm? Wie debugge ich den CLI -Modus in PhpStorm? Apr 01, 2025 pm 02:57 PM

Wie debugge ich den CLI -Modus in PhpStorm? Bei der Entwicklung mit PHPSTORM müssen wir manchmal den PHP im CLI -Modus (COMS -Zeilenschnittstellen) debuggen ...

Erklären Sie die späte statische Bindung in PHP (statisch: :). Erklären Sie die späte statische Bindung in PHP (statisch: :). Apr 03, 2025 am 12:04 AM

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Apr 01, 2025 pm 03:12 PM

Senden von JSON -Daten mithilfe der Curl -Bibliothek von PHP in der PHP -Entwicklung müssen häufig mit externen APIs interagieren. Eine der gängigen Möglichkeiten besteht darin, die Curl Library zu verwenden, um Post � ...

See all articles