Home > Article > Operation and Maintenance > How to deploy nginx, php and virtual host configuration in CentOS environment
Deployment time: 2012-07-24
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 for Execute nginx
Copy code The code is as follows:
useradd -m -r -s /sbin/nologin -d /opt/web/ www
2. Create Temporary directory
Copy 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;
# Number of processes, nginx The author thinks that one is enough, and modify
worker_processes 1;
# according to the number of visits. Set the 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 virtual host file
include /opt/web/nginx/conf/sites/*.conf;
}
6. Create the directory where the virtual machine configuration file is stored
Copy the 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 configuration in CentOS environment. For more information, please follow other related articles on the PHP Chinese website!