1、安装Mysql(参照LAMP)
2、安装PHP
下载PHP
官网 http://www.php.net/downloads.php
cd /usr/local/src
wget http://am1.php.net/distributions/php-5.3.27.tar.gz
解压PHP
tar zxf php-5.3.27.tar.gz
创建相关账户
useradd -s /sbin/nologin php-fpm
配置编译参数
./configure \
–prefix=/usr/local/php-fpm \
–with-config-file-path=/usr/local/php-fpm/etc \
–enable-fpm \
–with-fpm-user=php-fpm \
–with-fpm-group=php-fpm \
–with-mysql=/usr/local/mysql \
–with-mysql-sock=/tmp/mysql.sock \
–with-libxml-dir \
–with-gd \
–with-jpeg-dir \
–with-png-dir \
–with-freetype-dir \
–with-iconv-dir \
–with-zlib-dir \
–with-mcrypt \
–enable-soap \
–enable-gd-native-ttf \
–enable-ftp \
–enable-mbstring \
–enable-exif \
–disable-ipv6 \
–with-pear \
–with-curl \
–with-openssl
错误信息:
configure: error: Please reinstall the libcurl distribution -
easy.h should be in /include/curl/
解决办法:
yum install -y libcurl-devel
make遇到错误/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: * [sapi/fpm/php-fpm] 错误 1
解决办法yum install -y libtool-ltdl-devel
安装PHP
make install
修改配置文件
复制php的全局配置文件
cp php.ini-production /usr/local/php-fpm/etc/php.ini
修改PHP-FPM的配置文件
vim /usr/local/php-fpm/etc/php-fpm.conf
加入如下内容
/usr/local/php-fpm/sbin/php-fpm -t检测是否正确
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
user = php-fpm
group = php-fpm
listen.owner=nobody
listen.group=nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
保存后,
启动PHP-fpm
cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
service php-fpm start
如果想让它开机启动,执行:
chkconfig php-fpm on
ps aux |grep php-fpm是否启动
3、下载Nginx
官网http://nginx.org
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.4.4.tar.gz
解压tar zxvf nginx-1.4.4.tar.gz
配置编译参数
cd nginx-1.4.4
./configure \
–prefix=/usr/local/nginx \
–with-http_realip_module \
–with-http_sub_module \
–with-http_gzip_static_module \
–with-http_stub_status_module \
–with-pcre
make&&make install
编写nginx启动脚本,并加入系统服务
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
保存后,更改权限:
chmod 755 /etc/init.d/nginx
chkconfig –add nginx
如果想开机启动,请执行:chkconfig nginx on
更改nginx的配置文件
首先把原来的配置文件清空:
/usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
给改完后保存,检测是否有问题
/usr/local/nginx/sbin/nginx -t
如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx:service nginx start
如果不能启动,请查看 “/usr/local/nginx/logs/error.log” 文件,检查nginx是否启动:
ps aux |grep nginx
测试是否解析PHP文件
创建测试文件:
vim /usr/local/nginx/html/2.php
内容如下:
echo "测试php是否解析";
?>
测试:
测试php是否解析[root@localhost nginx]#
显示成这样,才说明php解析正确。
Nginx配置
1、默认虚拟主机
修改主配置文件nginx.conf,在结束符号}加上 include vhost/*.conf;,意思是。/usr.local/nginx/conf/vhost下的所有以.conf结尾的都会被加载。这样我们就把所有虚拟主机的配置放到/usr.local/nginx/conf/vhost下
mkdir /usr.local/nginx/conf/vhost
cd /usr.local/nginx/conf/vhost
vim default.conf
server
{
listen 80 default_server; //有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/nginx/default;
}
检测/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo “”123123“” > /data/nginx/default/index.html(创建索引页)
测试curl -x127.0.0.0:80 aaa.com
2.用户认证
新创建一个虚拟主机
/usr.local/nginx/conf/vhost vim test.com
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
htpasswd -c /usr/local/nginx/conf/htpasswd aming
检测/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
mkdir /data/nginx/test.com
echo “”1231234“” > /data/nginx/test.com/index.html
测试curl -x127.0.0.0:80 aaa.com
3、域名重定向
在虚拟主机中配置
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
server_name后边可以跟多个域名, permanent为永久,
检测/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.0.1:80 test1.com/123.txt -I
4.访问日志
日志格式定义在nginx.conf之中,log_format
到虚拟主机配置文件中指定访问日志的路径
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/1.log combined_realip;
}
access_log (指定路径) /tmp/1.log combined_realip;(日志格式的名字)
没有自带日志切割工具
配置使系统自动切割日志
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/data/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
##add cron
#0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
5.配置静态文件不记录日志并添加过期时间
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/1.log combined_realip;
}
使用location~可以指定对应的静态文件,expires配置过期时间, access_log off就可以不记录访问日志了。
6、Nginx防盗链
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}
access_log off;
}
7、访问控制
location /admin/
{
allow 192.168.188.1;
allow 127.0.0.1;
deny all;
}
location /admin/
{
deny 192.168.188.1;
deny 127.0.0.1;
}
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
只要有匹配的就结束
8.Nginx解析PHP
test.conf里边配置如下
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
}
access_log /tmp/1.log combined_realip;
}
fastcgi_pass来指定php-fpm的地址,也可以写成tcp:port的形式,例如127.0.0.1:9000
fastcgi_pass要与php-fpm的地址保持一致。
fastcgi_param SCRIPT_FILENAME后边跟的是该站点的根目录,和前面定义的root路径应该一致。否则php访问会出现404
9、Nginx代理
cd /usr/local/nginx/conf/vhost
vim proxy.conf
“`
server
{
listen 80;
server_name ask.apelearn.com;
location /
{
proxy_pass http://121.201.9.155/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
检测/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.0.1:80 ask.apelearn.com -I
nginx配置负载均衡
cd /usr/local/nginx/conf/vhost
vim load.conf
upstream qq_com
{
ip_hash;
server 61.135.157.156:80;
server 125.39.240.113:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://qq_com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
php-fpm配置
1php-fpm的pool
php-fpm支持多个pool,每一个pool监听一个端口,也可以监听一个socket,php-fpm做一个更改,[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
include=etc/php-fpm.d/*.conf
mkdir /usr/local/php-fpm/etc/php-fpm.d
vim aming.conf
[aming]
listen = /tmp/aming.sock
listen.mode=666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
php_admin_value[open_basedir]=/data/www/:/tmp/
每一个pool监听一个端口,在Nginx不同的端口中调用不同的pool,从而达到相互隔离的目的,两个pool互不影响。
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm restart
ls /etc/*sock
2.php-fpm的慢执行日志
vi /usr/local/php-fpm/php-fpm.d/aming.conf
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
3、php-fpm中的open_basedir
在pool中添加php_admin_value[open_basedir]=/data/www/:/tmp/
3、php-fpm进程管理