Home Operation and Maintenance Nginx How to deploy nginx, php and virtual host in CentOS environment

How to deploy nginx, php and virtual host in CentOS environment

May 13, 2023 pm 12:40 PM
php centos nginx

os environment: centos 6.1
nginx: nginx-1.2.2
php:php5.3.14
0. Install dependency packages

Copy code The code is as follows:

yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make

1. Add www user to execute nginx

Copy the code The code is as follows:

useradd -m -r -s /sbin/nologin -d /opt/web/ www

2. Create a temporary directory

Copy the code The code is as follows:

mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx /fcgi/

3. Download the latest stable version of nginx source code

Copy the code. The code is as follows:

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz

4. Unzip, compile, and install

Copy code The code is as follows:

tar vxzf nginx-1.2.2.tar.gz
cd nginx-1.2.2/
./configure \
--prefix=/opt/web/nginx \
--error -log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/ nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
- -http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi -temp-path=/var/tmp/nginx/uwsgi/
make
make install

5. Configure nginx

Copy code The code is as follows:

vim /opt/web/nginx/conf/nginx.conf
# Specify the startup user:
user www www;
# The number of processes, the nginx author believes that one is enough, based on your own traffic Modify
worker_processes 1;
# Set error log:
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx/ error.default.log;
pid /opt/web/nginx/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
charset utf-8;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$ status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
application/atom xml text/javascript;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs /host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html

#error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the php scripts to apache listening on 127.0.0.1:80

##location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the php scripts to fastcgi server listening on 127.0.0.1:9000

#location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param script_filename /scripts$fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
}
# deny access to .htaccess files, if apache's document root
# concurs with nginx's one

#location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of ip-, name -, and port-based configuration

##server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# https server

##server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols sslv2 sslv3 tlsv1 ;
# ssl_ciphers high:!anull:!md5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
proxy_read_timeout 200;
# only retry if there was a communication error, not a timeout
# on the tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
proxy_set_header x-scheme $scheme;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header host $host;
proxy_set_header x-forwarded-for $ proxy_add_x_forwarded_for;
#Introduce the virtual host file
include /opt/web/nginx/conf/sites/*.conf;
}

6. Create the directory where the virtual machine configuration file is stored

Copy code The code is as follows:

mkdir /opt/web/nginx/conf/sites

After this configuration, if you need to add a new virtual host, add the configuration file directly to the nginx/conf/sites/ directory.
For example: Now there is a www.jb51.net domain name
Create: /opt/ The content of web/nginx/conf/sites/www.jb51.net.conf file
is as follows:

Copy code The code is as follows:

server {
listen 80;
client_max_body_size 10m;
#Multiple domain names are separated by spaces, the first one is the default
server_name www.jb51.net jb51.net;
charset utf-8;
index index.html index.htm index.php;
# Define the root directory
set $root /var/webroot/www.jb51.net/;
# Set the site path
root $root;
# Prevent directory browsing
autoindex off;
if ($host != 'www.jb51.net') {
rewrite ^/(.*)$ //www.jb51.net/$1 permanent;
}
# Prevent .htaccess files from being requested
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.jb51.net/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# Forward other requests to uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/360ito_uwsgi.sock;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header host $host;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
#proxy_pass http://localhost:5000;
}
# Forward php type requests to fastcgi
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# Access log:
access_log /var /log/nginx/access.www.jb51.net.log;
# Load the .htaccess rewrite file. Note that variable paths are not supported here.
# cannot be written as include $root/www.jb51.net/. htaccess;
# include /var/webroot/www.jb51.net/.htaccess;
# If domain name jump is enabled, when an access error occurs, other domain names will automatically jump to www.jb51.net
# Note that what I am saying here is that the jump will only occur when an access error occurs, so 301 redirection cannot be implemented here!
server_name_in_redirect on;
}

7. Install the latest version of php (php5.3.14)

Copy the code The code is as follows:

cd /usr /local/src/
wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror
tar xjvf php-5.3.14.tar.bz2
cd php-5.3.14

Execution:

Copy code The code is as follows:

./buildconf --force

if If the error is reported, it may be that your autoconf is not version 2.13. For php5.3 series bugs, you need to install autoconf version 2.13:

Copy code The code is as follows:

centos: # yum install autoconf213
debian: # apt-get install autoconf2.13

Set environment variables

Copy code The code is as follows:

# centos:
export php_autoconf="/usr/bin/autoconf-2.13"
# debian:
export php_autoconf="/usr/bin/autoconf2.13"

Run again: ./buildconf - -force, if buildconf: autoconf version 2.13 (ok)
appears, it means success.
Compile and install php

Copy the code The code is as follows:

./configure \
--prefix=/opt/web/php \
--with -config-file-path=/opt/web/php/etc \
--with-config-file-scan-dir=/opt/web/php/etc/conf.d \
--enable -fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=/opt/db/percona-server-5.5.14 -rel20.5 \
--with-mysqli=/opt/db/percona-server-5.5.14-rel20.5/bin/mysql_config \
--with-iconv-dir \
- -with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--enable-inline-optimization
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
cd /opt/web/php/etc
cp php-fpm.conf.default php-fpm.conf

Modify php-fpm.conf to enable the following lines, that is, remove the preceding semicolon (;)

Copy Code The code is as follows:

pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
env[hostname] = $hostname
env[path] = /usr/local/bin:/usr/bin:/bin
env[tmp] = /tmp
env[tmpdir] = /tmp
env[temp] = / tmp

8. Start php-fpm

Copy the code The code is as follows:

/opt/web/php/sbin/php-fpm

Start nginx

Copy code The code is as follows:

/opt/web/nginx/sbin/nginx

9. Test it

Copy code The code is as follows:

vim /var/webroot/www.jb51.net/tz.php

Enter and save

Copy code The code is as follows :

phpinfo();
?>

10. Enter: http://php.jb51.net/tz.php
If successful, you can see the information output by phpinfo()                                                                                                        

The above is the detailed content of How to deploy nginx, php and virtual host in CentOS environment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? Apr 01, 2025 pm 03:15 PM

Running multiple PHP versions simultaneously in the same system is a common requirement, especially when different projects depend on different versions of PHP. How to be on the same...

See all articles