php编译安装:
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2
[[email protected] src]# rm -rf /usr/local/php/ //删除上次LAMP安装时做的PHP
[[email protected] src]# tar jxvf php-5.4.37.tar.bz2
[[email protected] src]# cd php-5.4.37
[[email protected] php-5.4.37]# ./configure \
> --prefix=/usr/local/php \
> --with-config-file-path=/usr/local/php/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 \
> --enable-zend-multibyte \
> --disable-ipv6 \
> --with-pear \
> --with-curl \
> --with-openssl
如有报错,试试先执行如下命令:
[[email protected] php-5.4.37]#yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
[[email protected] php-5.4.37]# make
[[email protected] php-5.4.37]# echo $?
0
[[email protected] php-5.4.37]# echo $?
0
[[email protected] php-5.4.37]# cp php.ini-production /usr/local/php/etc/php.ini
//拷贝一份配置文件
[[email protected] php-5.4.37]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
//拷贝启动脚本
[[email protected] php-5.4.37]# vim !$ //查看脚本
vim /etc/init.d/php-fpm
[[email protected] php-5.4.37]# chmod 755 /etc/init.d/php-fpm //授执行的权限
[[email protected] php-5.4.37]# chkconfig --add php-fpm //加入系统服务项
[[email protected] php-5.4.37]# chkconfig php-fpm on //开机自启动
[[email protected] php-5.4.37]# service php-fpm start
failed //启动失败,还少相关配置
[[email protected] php-5.4.37]# cd /usr/local/php/etc/
[[email protected] etc]# mv php-fpm.conf.default php-fpm.conf //重命名成php-fpm.conf
[[email protected] etc]# /usr/local/php/sbin/php-fpm -t //检查配置文件是否有错
[[email protected] etc]# !ser //重启还是失败,无用户
service php-fpm start
[[email protected] etc]# useradd -s /sbin/nologin php-fpm //增加用户
[[email protected] etc]# !ser
service php-fpm start
[[email protected] etc]# ps aux|grep php-fpm //查看进程是否启动
[[email protected] etc]# netstat -lnp //查看监听的端口
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19600/php-fpm
[[email protected] etc]# ls
pear.conf php-fpm.conf php.ini
Nginx编译安装:
[[email protected] etc]# cd /usr/local/src/
[[email protected] src]# ls
[[email protected] src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[[email protected] src]# tar zxvf nginx-1.6.2.tar.gz
[[email protected] src]# cd nginx-1.6.2
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-pcre
[[email protected] nginx-1.6.2]# echo $?
0
[[email protected] nginx-1.6.2]# make
[[email protected] nginx-1.6.2]# echo $?
0
[[email protected] nginx-1.6.2]# make install
[[email protected] nginx-1.6.2]# echo $?
0
[[email protected] nginx-1.6.2]# cd /usr/local/nginx/
[[email protected] nginx]# ls
conf html logs sbin
[[email protected] nginx]# ls sbin/nginx //nginx的可执行文件
sbin/nginx
[[email protected] nginx]# /usr/local/nginx/sbin/nginx //启动nginx,80端口被占用
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
[[email protected] nginx]# apachectl stop
[[email protected] nginx]# /usr/local/nginx/sbin/nginx //启动nginx
[[email protected] nginx]# ps aux |grep nginx //查看进程有一主一次
root 21901 0.0 0.0 5380 636 ? Ss 05:18 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 21902 0.0 0.0 5564 988 ? S 05:18 0:00 nginx: worker process
[[email protected] nginx]# netstat -lnp //查看监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21901/nginx
nginx无自己的启动脚本(可以自己写一份)
解析php,使nginx、php联系在一起
测试php解析
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
//将目录改到web网站根目录下
客户端访问,到nginx默认页面
[[email protected] ~]# cat /usr/local/nginx/html/index.html
//查看文件,刚访问的页面配置为该文件
[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# vim info.php
<?php
phpinfo();
?>
[[email protected] html]# /usr/local/nginx/sbin/nginx -t //查看配置文件有无错误
[[email protected] html]# /usr/local/nginx/sbin/nginx -s reload //重新加载
客户端访问,正常解析
[[email protected] html]# curl localhost
[[email protected] html]# curl localhost/info.php
//使用curl测试
nginx启动脚本和配置文件
nginx无apachect 文件工具重启,需手动写一个
[[email protected] html]# 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
[[email protected] html]# chmod 755 !$
chmod 755 /etc/init.d/nginx
[[email protected] html]# chkconfig --add nginx
[[email protected] html]# chkconfig nginx on
[[email protected] html]# service nginx start
[[email protected] html]# service nginx stop
[[email protected] html]# service nginx restart
[[email protected] html]# vim /usr/local/nginx/conf/nginx.conf //打开默认配置文件,使用gg调整光标到首行,然后使用dG命令清空原先配置,写入:
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 wang ‘$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;
include vhosts/*.conf;
}
[[email protected] html]# pwd
/usr/local/nginx/html
[email protected] html]# cd /usr/local/nginx/conf/
[[email protected] conf]# mkdir vhosts
[[email protected] conf]# cd vhosts/
[[email protected] conf]# vim default.conf
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/1233;
deny all;
}
//使默认为错误403
[email protected] conf]# /usr/local/nginx/sbin/nginx -t
[[email protected] conf]# /etc/init.d/nginx reload
[[email protected] vhosts]# curl -x127.0.0.1:80 111.com
<head><title>403 Forbidden</title></head>
//curl测试出现403错误
[[email protected] vhosts]# vim 111.conf
server
{
listen 80;
server_name 111.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
# fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
[email protected] conf]# /usr/local/nginx/sbin/nginx -t
[[email protected] conf]# /etc/init.d/nginx reload
[[email protected] vhosts]# /etc/init.d/nginx reload
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19600/php-fpm
[[email protected] vhosts]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 301 Moved Permanently
[[email protected] vhosts]# curl -x127.0.0.1:80 111.com/forum.php -I
HTTP/1.1 200 OK
php-fpm配置文件
[[email protected] ~]# ls /usr/local/php/etc/php-fpm.conf
/usr/local/php/etc/php-fpm.conf
// php-fpm.conf服务相关配置文件
[[email protected] ~]# ls /usr/local/php/etc/php.ini
/usr/local/php/etc/php.ini
//全局服务配置
[[email protected] ~]# vim /usr/local/php/etc/php-fpm.conf //查看配置
[[email protected] ~]# > /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/www.sock
user = php-fpm
group = php-fpm
listen.owner = nobody //和后面的nginx的一致
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
[[email protected] ~]# /usr/local/php/sbin/php-fpm -t
[[email protected] ~]# /etc/init.d/php-fpm restart
[[email protected] ~]# ps aux |grep php-fpm //(查看pool、www)
[[email protected] ~]# ls /usr/local/nginx/conf/vhosts/
111.conf default.conf
[[email protected] ~]# cat /usr/local/nginx/conf/vhosts/111.conf
[[email protected] ~]# vim /usr/local/php/etc/php-fpm.conf
[www]
slowlog = /tmp/www_slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir]=/data/www/:/tmp/
//慢反应日志,反应慢时记录日志
配置文件参考
[[email protected] html]# vim /usr/local/nginx/conf/nginx.con
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 wang ‘$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;
include vhosts/*.conf;
}