Nginx+Mysql+PHP安装和配置,简单优化
一)软件下载1、下载nginx
wget http://nginx.org/download/nginx-1.2.0.tar.gz
2、下载pcre
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
3、下载MySQL
wget http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.5/mysql-5.5.25.tar.gz
4、下载php
wget http://cn.php.net/distributions/php-5.3.13.tar.gz
5、下载cmake(MySQL编译工具)
wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz
6、下载libmcrypt
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
安装编译工具及库文件(使用CentOS yum命令安装)
yum install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gdkernel keyutilspatchperl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch
二)安装 cmake
# tar zxvf cmake-2.8.8.tar.gz
# ./configure
# make
# make install
三)安装 Mysql
# groupadd mysql #添加mysql组
# useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
# mkdir -p /data/mysql #创建MySQL数据库存放目录
# chown -R mysql:mysql /data/mysql #设置MySQL数据库目录权限
# mkdir -p /usr/local/mysql #创建MySQL安装目录
# tar -zxvf mysql-5.5.25.tar.gz #解压
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
# make
# make install
# cd /usr/local/mysql
# cp ./support-files/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
# vim /etc/my.cnf #编辑配置文件,在 部分增加下面一行
datadir = /data/mysql#添加MySQL数据库路径
:wq!#保存退出
# ./scripts/mysql_install_db --user=mysql #生成mysql系统数据库
# cp -fr ./support-files/mysql.server /etc/rc.d/init.d/mysqld#把Mysql加入系统启动
# chmod 755 /etc/init.d/mysqld #增加执行权限
# chkconfig mysqld on #设置开机启动
# vim /etc/rc.d/init.d/mysqld#编辑
basedir = /usr/local/mysql #MySQL程序安装路径
datadir = /data/mysql#MySQl数据库存放目录
# service mysqld start #启动
# vim /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
:wq! #保存退出
下面这两行把myslq的库文件链接到系统默认的位置,在编译类似PHP等软件时可以不用指定mysql的库文件地址。
# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql/
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
# /usr/local/mysql/bin/mysqladmin -u root -p password "123456"#修改密码
Enter password: #不用再输入密码直接按回车键。
到此mysql安装完成
四)安装pcre
# mkdir /usr/local/pcre #创建目录
# tar zxvf pcre-8.30.tar.gz #解压
# ./configure --prefix=/usr/local/pcre/
# make
# make install
五)安装nginx
# groupadd www #添加www组
# useradd -g www www -s /bin/false #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
# tar zxvf nginx-1.2.0.tar.gz #解压
# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/mnt/pcre-8.30
注意:--with-openssl=/usr/ --with-pcre=/mnt/pcre-8.30指向的是源码包解压的路径,而不是安装的路径,否则会报错
# make
# make install
# /usr/local/nginx/sbin/nginx #启动nginx
# vim /etc/rc.d/init.d/nginx #设置nginx开启启动,编辑启动文件添加下面内容.
#################################################################
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
./etc/rc.d/init.d/functions
# Source networking configuration.
./etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
#################################################################
:wq!#保存退出
# chmod 755 /etc/rc.d/init.d/nginx #赋予文件执行权限
# chkconfig nginx on#设置开机启动
# /etc/rc.d/init.d/nginx restart #重启
停止 nginx: [确定]
启动 nginx: [确定]
六)安装libmcrypt
# tar zxvf libmcrypt-2.5.7.tar.gz #解压
# ./configure
# make
# make install
七)安装PHP
# tar zxvf php-5.3.13.tar.gz #解压
# mkdir /usr/local/php5 #创建安装PHP目录
# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv--with-zlib--enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex--enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl
# make
# make install
# cp php.ini-production /usr/local/php5/etc/php.ini #复制php配置文件到安装目录下
# rm -fr /etc/php.ini #删除系统自带的php配置文件
# ln -s /usr/local/php5/etc/php.ini /etc/php.ini #创建软连接
# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
# vim /usr/local/php5/etc/php-fpm.conf
user = www #设置php-fpm运行账号为www
group = www #设置php-fpm运行组为www
pid = run/php-fpm.pid #取消前面的分号
:wq保存退出
# cp /mnt/php-5.3.13/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #设置 php-fpm开机启动,拷贝php-fpm到启动目录
# chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限
# chkconfig php-fpm on#设置开机启动
# vim /usr/local/php5/etc/php.ini #编辑
找到:;date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_php = On
修改为:expose_php = OFF#禁止显示php版本的信息
:wq保存退出
八)配置nginx支持php
# vim /usr/local/nginx/conf/nginx.conf #编辑
userwww www;#首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php5/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
worker_processes 2 ;
#error_loglogs/error.log;
error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
pid logs/nginx.pid;
events {
worker_connections3000;
}
http {
include mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
client_max_body_size 20m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
#access_loglogs/access.logmain;
sendfile on;
tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout60;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzipon;
server {
listen 80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
root html;
indexindex.php index.html index.htm;#添加index.php
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#error_page404 /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;
}
# 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_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
# /etc/rc.d/init.d/nginx restart #重启nginx
九)优化linux内核参数
#vim /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 102465535
使配置立即生效:
#/sbin/sysctl -p
十)测试
# vim /usr/local/nginx/html/index.php#建立php测试页
添加以下内容
:wq 保存退出,.重新启动机器,开机后访问:http://ip地址/index.php
出现以下图表示nginx+php+mysql安装成功
http://blog.运维网.com/attachment/201211/110344261.jpg
十一 )编写每天定时切割Nginx日志的脚本
# vim /usr/local/nginx/sbin/nginx.sh
#!/bin/bash
# The Nginx logs path
savepath_log="/home/nginx/logs" #这里指定分割后的日志存放的路径
logs_path="/usr/local/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid `
设置crontab,每天凌晨00:00切割nginx访问日志
# crontab -e
输入以下内容:
00 00 * * * /bin/bash/usr/local/nginx/sbin/nginx.sh
页:
[1]