/ 中存储网

Linux系统Nginx+Apache+PHP+MySQL环境安装配置过程记录

2013-11-08 08:10:01 来源:itjs.cn

#安装时钟同步软件

yum install -y ntpdate 

#同步时间

ntpdate us.pool.ntp.org

#设置定时同步服务器时间

crontab -e

#在文件里加入以下内容(30分钟同步一次):

0-59/30 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP

#通用安装

yum -y install make wget apr* autoconf automake gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd  kernel keyutils  patch  perl 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

#vi 常用命令:

G  #光标定位至文件末尾

1G #光标定位至文件开头

#将文件夹data复制到/var/www/html/路径下

cp -r /usr/local/data /var/www/html/

#删除文件夹及文件

rm -rf /var/www/html/data

#虚拟机复制后网络卡配置问题解决:

#1)修改机器名

vi /etc/sysconfig/network

#2)修改网卡设置,注释掉eth0第一行,将修改eth1为eth0

vi /etc/udev/rules.d/70-persistent-net.rules

#打开防火墙80端口

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

/etc/rc.d/init.d/iptables save

#设置服务自动启动

chkconfig httpd on

#约定:

#1、将所有源码下载到/usr/local/src/目录下

#2、将所有源码编译安装在/usr/local/目录下

一、安装Apache、PHP、eAccelerator、php-memcache

#1、安装Apache

cd /usr/local/src

#1.1、安装Apache的依赖库Apr

wget http://mirror.bit.edu.cn/apache/apr/apr-1.4.6.tar.gz

tar -zxvf apr-1.4.6.tar.gz

cd apr-1.4.6

./configure --prefix=/usr/local/apr

#编译遇到的错误:

rm: cannot remove `libtoolT': No such file or directory

#解决办法:直接打开 configure,把 $RM "$cfgfile" 这行删除掉,重新运行 ./configure 就可以了

make

make install

#1.2、安装Apache的依赖库Apr-util

wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.2.tar.gz

tar -zxvf apr-util-1.5.2.tar.gz

cd apr-util-1.5.2

./configure --prefix=/usr/local/apr-util -–with-apr=/usr/local/apr

make

make install

#1.3、编译安装Apache

wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.2.24.tar.gz

tar -zxvf httpd-2.2.24.tar.gz

cd httpd-2.2.24

./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl --enable-ssl --enable-module=so --enable-rewrite --enable-cgid --enable-cgi

make

make install

#1.4、验证安装

#修改配置文件

vi /usr/local/apache/conf/httpd.conf

#启动httpd服务

/usr/local/apache/bin/apachectl -k start

#打开防火墙端口80,使用浏览器访问,看到输出:“It works!”,安装成功!

#1.5、设置httpd服务自动启动

#1.5.1、配置环境变量

vi /etc/profile

#在文件末尾添加下面内容:

export PATH=$PATH:/usr/local/apache/bin

#1.5.2、把apache加入为系统启动项

cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd

#1.5.3、编辑启动配置文件

vi /etc/init.d/httpd

#在#!/bin/sh下面加入以下内容(注意前面的#号也要是文件内容):

#chkconfig:345 85 15

#description:start and stop the apache http server

:x

#加入到服务表列,并设为自动启动

chmod +x /etc/init.d/httpd #赋可执行权限

chkconfig --add httpd #添加到服务列表

chkconfig httpd on   #设置开机启动

#安装好后,进行并发测试:

ab -n 1000 -c 10 http://localhost/

#2、安装PHP

#编译前先安装依赖:

yum install libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

tar -zxvf libmcrypt-2.5.7.tar.gz

cd libmcrypt-2.5.7

./configure

make && make install

wget http://www.php.net/get/php-5.4.16.tar.gz/from/hk2.php.net/mirror

tar -zvxf php-5.4.16.tar.gz

cd php-5.4.16

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache/bin/apxs  --with-gd  --with-iconv --with-freetype --with-jpeg --with-png --with-zlib --with-libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt

make

make install

#复制php配置文件到安装目录

mkdir /usr/local/php5/etc

cp php.ini-production /usr/local/php5/etc/php.ini

#删除系统自带的配置文件

rm -rf /etc/php.ini

#创建配置文件软链接

ln -s  /usr/local/php5/etc/php.ini   /etc/php.ini

#编辑

vi /usr/local/php5/etc/php.ini

找到:;open_basedir =

修改为:open_basedir = .:/tmp/   #防止php木马跨站

找到:;date.timezone =

修改为:date.timezone = PRC

配置apache支持php

vi /usr/local/apache/conf/httpd.conf  #编辑apache配置文件

在LoadModule php5_module modules/libphp5.so这一行下面添加:

AddType application/x-httpd-php .php  (注意:php .php这个点前面有一个空格)

#重启Apache

service httpd restart

二、安装MYSQL、Memcached

三、安装Nginx

wget http://nginx.org/download/nginx-1.4.1.tar.gz

tar -zxvf nginx-1.4.1.tar.gz

cd nginx-1.4.1

./configure --prefix=/usr/local/nginx

make

make install

#3.1、Nginx配置

vi /usr/local/nginx/conf/nginx.conf

修改以下部份内容:

server {

  listen 80;

  server_name localhost;

  upstream clusterApache {    

#服务器列表 weight参数是权重,权重越高被分配到的几率越大

server 192.168.1.214:80 weight=1;    

server 192.168.1.215:80 weight=1;  

  }

  location / {

 root /var/www/;

 index index.php index.html;

# Nginx找不到文件时,转发请求给后端Apache

 error_page 404 @proxy;

# css, js 静态文件设置有效期7天

 location ~ .*.(js|css)$ {

access_log off;

expires  7d;

 }

# 图片设置有效期3天

 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {

access_log off;

expires  3d;

 }

  }

# 动态文件.php请求转发给后端Apache

  location ~ .php$ {

#proxy_redirect off;

#proxy_pass_header Set-Cookie;

#proxy_set_header Cookie $http_cookie;

# 传递真实IP到后端

 proxy_set_header Host $http_host;

 proxy_set_header X-Real-IP $remote_addr;

 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 proxy_pass http://clusterApache;

  }

location @proxy {

 proxy_set_header Host $http_host;

 proxy_set_header X-Real-IP $remote_addr;

 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 proxy_pass http://clusterApache;

  }

}

#3.2、启动、关闭与重启

#3.2.1、检查Nginx配置文件的正确性

/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

#3.2.2、查看Nginx版本

/usr/local/nginx/sbin/nginx -v

#3.2.3、设置自动启动

#创建启动文件

vi /etc/init.d/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=/var/run/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 /var/run/nginx.pid

}

# reload nginx service functions.

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

#设置文件权限

chmod +x /etc/init.d/nginx

#添加到有服务列表

chkconfig --add nginx

#查看服务

chkconfig nginx --list

#设服务自动启动

chkconfig nginx on

#使用以下命令控制服务

service nginx start 

service nginx stop 

service nginx restart 

service nginx reload