CentOS6.3上安装与配置nginx+php+mysql环境

时间:2023-01-11 20:34:55

1. 目前nginx采用是源码包安装的方式(yum安装失败),下载地址:http://nginx.org/en/download.html

我这里的安装包是:nginx-1.12.0.tar.gz

 

2.在安装nginx前需要安装pcre,我这里的pcre源码包是:pcre-8.35.tar.gz

文件存放路径:/home/nginx/pcre-8.35.tar.gz

tar  zxvf  pcre-8.35.tar.gz

cd  pcre-8.35

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

make

make install

 

3.源码包安装nginx;文件存放路径:/home/nginx/nginx-1.12.0.tar.gz

tar  zxvf  nginx-1.12.0.tar.gz

cd  nginx-1.12.0

./configure --prefix=/usr/local/nginx  --without-http_memcached_module --with-http_stub_status_module --with-pcre=/home/nginx/pcre-8.35

注意:此处一定要加上pcre的源码所在的路径,否则报错。

make

make install

 

4. yum安装mysql + php;在安装之前,先装yum-downloadonly,将mysql和php的安装包下载到本地后再安装,方便后日后搭建不能上网的设备。

    4.1安装yum-downloadonly

    yum install yum-downloadonly 或者 yum install yum-plugin-downloadonly

    4.2下载Mysql

    yum install mysql mysql-server mysql-devel --downloadonly --downloaddir=/home/PhpMysql

    4.3下载PHP

    yum install php php-fpm php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml --downloadonly --downloaddir=/home/PhpMysql/ 

    4.4安装mysql和php

    cd  /home/PhpMysql/ 

    rpm  -ivh  *.rpm  --force

 

5. 配置nginx: 找到nginx.conf,修改四处即可: 取消两处注释,修改一处参数,增加一处设置。

我安装的文件路径在:/usr/local/nginx/conf/nginx.conf

user  nobody;             #将此行(也就是文件首行)注释去掉。

location / {
root html;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /Autotest/index.php?$1 last;
break;
}
}            #此块是增加的,用于页面跳转;如果不加该处设置,能打开项目首页,却无法打第二个页面。

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}                              #取消此块的注释,并用修改fastcgi_param行的参数。

原本此行是:fastcgi_param  SCRIPT_FILENAME  /script$fastcgi_script_name;  须改为:fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

配置完毕。

 

6.手动启动

mysql:    service mysqld start

php-fpm:   service php-fpm start

nginx:      /usr/local/nginx/sbin/nginx

 

7. nginx开机自启动:需要编写自启动脚本,然后chkconfig,如下:

#!/bin/bash

#chkconfig: 235 90 10
#description: nginx self startup by.

PROG='nginx'
PROG_PATH='/usr/local/nginx/sbin/'

get_pid()
{
local pid=`ps aux | grep $1 | grep -v grep| awk '{print $2}'`
echo $pid
}


function start()
{
local pid=$(get_pid $PROG)
if [[ "$pid" != "" ]]; then
echo "Error $PROG is currently running" 1>&2
exit 1
else
$PROG_PATH$PROG 2>&1 &
echo "$PROG started"
fi
}


function stop()
{
echo "begin to stop $PROG"
local pid=$(get_pid $PROG)
if [[ "$pid" != "" ]]; then
kill -9 $pid
echo "$PROG stopped"
else
echo "Error,$PROG not started!" 1>&2
exit 1
fi

}

case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
*)
echo "Usage: $0 {start|stop|reload|restart|force_reload}" 1>&2
exit 1
;;

esac

此处脚本完毕,将文件命名为:atNginx,然后执行chkconfig : chkconfig --add atNginx

 

8.mysql与php-fpm开机自启动

chkconfig --level 235 mysqld on

chkconfig --level 235 php-fpm on