Configure virtual hosts in Nginx and support for PHP

Source: Internet
Author: User

Create the vhosts directory under the conf directory and create the site configuration file. Here, examples.com. conf is used as an example.

The code is as follows: Copy code

[Root @ localhost ~] # Cd/usr/local/nginx/
[Root @ localhost nginx] # mkdir-p conf/vhosts
[Root @ localhost nginx] # vim conf/vhosts/examples.com. conf

The configuration file examples.com. conf created is as follows:

The code is as follows: Copy code

# Define a VM as a server
Server {
# Listening port. Multiple ports are separated by spaces.
Listen 80;
# Defines the IP address or domain name used for access. Separate multiple domain names with spaces
Server_name examples.com www.examples.com;
# Used to specify the default encoding format of the website webpage
Charset UTF-8;
 
# This parameter is used to specify the path for storing access logs of the site. The following main is used to set the log format.
# Access_log logs/examples.com. access. log main;
 
# It is used to specify the website's webpage root directory, which can be a relative path (relative to the nginx installation directory) or an absolute path
# Root/www/examples.com
# Specify the default homepage address for access
# Index index.html index. php
 
# It is used to specify the default site access settings. The root and index usage and effect are the same as those above.
# Either of the two methods is acceptable. The location/{} method is used here.
Location /{
Root/www/examples.com;
Index index.html index. php;
     }
 
# Use the error_page command to set the returned page of various error messages
# If The Returned page size of the error message is smaller than k, it will be replaced by ie's default error page
# Error_page 404/404 .html;
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
     }
 
# The location command of nginx is used to set url address matching. It supports regular expression matching and condition judgment matching.
# Use the location command to filter dynamic and static web pages.
# Set cache for all images for 30 days
Location ~ . *. (Gif | jpg | jpeg | png | bmp | swf) $ {
Expires 30d;
     }
# Cache js and css files for 1 hour
Location ~ . *. (Js | css) $ {
Expires 1 h;
     }
 
# The following lists two methods for parsing php:
# The first is to simply send all requests ending with php to port 8080 of the local machine for processing.
# Location ~ . Php $ {
# Proxy_pass http: // 127.0.0.1: 8080;
#}
# The second method is to send the php request to the IP address and port listened by the FastCGI process, and forward it to PHP_FPM.
Location ~ . Php $ {
# Set the root directory for parsing php, usually the website root directory
Root/www/examples.com;
# The address and port are consistent with those set in php_fpm.
Fastcgi_pass 127.0.0.1: 9000;
# Default homepage
Fastcgi_index index. php;
# Specify the main directory to prevent php dynamic programs, that is, the path specified earlier than $ fastcgi_script_name. We recommend that you use $ document_root in the same directory as the root directory of the website.
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
     }
 
# Prevent direct access to the. htaccess file. We recommend that you enable
Location ~ /. Ht {
Deny all;
     }
 }

Finally, create the root directory of the website and set the directory permissions:

The code is as follows: Copy code

[Root @ localhost ~] # Mkdir-p/www/examples.com
# Write permission
[Root @ localhost ~] # Chmod + w/www/examples.com
[Root @ localhost ~] # Chown-R www: www/examples.com
# Create a php file for testing
[Root @ localhost ~] # Echo "<? Phpinfo ();?> ">/Www/examples.com/phpinfo.php


After starting php-fpm and nginx, you can directly access http://www.examples.com/phpinfo.php.

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.