linux centos源码编译安装Nginx和PHP

来源:互联网 发布:小学网络安全教育教案 编辑:程序博客网 时间:2024/04/27 20:19


一、创建用户组及用户


groupadd www
useradd -g www -s /sbin/nologin -M www


二、安装nginx-1.10.2


1. 前提


系统已安装openssl、pcre、zlib,若未安装这些软件请先安装,可源码安装也可通过yum命令进行,源码可前往软件官网下载

2. 下载nginx源码包与解压


地址:http://nginx.org/en/download.html,版本:1.10.2

进入目录/usr/local/src/
cd /usr/local/src

下载nginx源码,解压
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2

3. 配置

./configure --user=www --group=www --prefix=/usr/local/nginx-1.10.2 --sbin-path=/usr/local/nginx-1.10.2/sbin/nginx --conf-path=/usr/local/nginx-1.10.2/conf/nginx.conf --error-log-path=/usr/local/nginx-1.10.2/logs/nginx_error.log --http-log-path=/usr/local/nginx-1.10.2/logs/nginx_access.log --pid-path=/usr/local/nginx-1.10.2/run/nginx.pid --lock-path=/usr/local/nginx-1.10.2/lock/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module
错误:./configure: error: perl module ExtUtils::Embed is required,解决:yum -y install perl-devel perl-ExtUtils-Embed

4. 安装

make && make install

5. 以service形式启动nginx

新增启动脚本
vim /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: /usr/local/nginx-1.10.2/run/nginx.pid
# config: /usr/local/nginx-1.10.2/conf/nginx.conf
#nginx path
nginxd=/usr/local/nginx-1.10.2/sbin/nginx
#nginx conf path
nginx_config=/usr/local/nginx-1.10.2/conf/nginx.conf
#nginx pid file
nginx_pid=/usr/local/nginx-1.10.2/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 on



三、安装php-5.6.27


1. 前提

系统已安装依赖包,依赖包根据系统环境按需安装,如:yum install libxml2 libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel libmcrypt-devel

2. 下载php源码包与解压

地址:http://www.php.net/downloads.php,版本:5.6.27

进入目录/usr/local/src/
cd /usr/local/src

下载nginx源码,解压
wget http://cn2.php.net/get/php-5.6.27.tar.gz/from/this/mirror
tar -zxvf php-5.6.27.tar.gz
cd php-5.6.27

3. 配置

./configure --prefix=/usr/local/php-5.6.27 --with-config-file-path=/usr/local/php-5.6.27/etc --with-mcrypt --with-gettext --with-mysql --with-mysqli --with-gd --with-jpeg-dir --with-png-dir --with-curl --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --enable-sockets --with-png-dir --with-pdo-mysql --enable-fpm --enable-fastcgi --with-zlib --enable-pcntl

4. 编译安装

make
make test
make install

5. 配置文件

设置php.ini
cp /usr/local/php-5.6.27/php.ini-dist /usr/local/php-5.2.14/etc/php.ini 

设置php-fpm,并修改php-fpm.conf
cp /usr/local/php-5.6.27/etc/php-fpm.conf.default  /usr/local/php-5.6.27/etc/php-fpm.conf
vim /usr/local/php-5.6.27/etc/php-fpm.conf
修改如下:
user = www
group = www

6. 将php-fpm作为service服务

赋值启动脚本
cp /usr/local/src/php-5.6.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

赋予执行权限
chmod +x /etc/init.d/php-fpm

开机自启动
chkconfig --add php-fpm
chkconfig php-fpm on


四、nginx+php


1. 修改nginx配置

vim /usr/local/nginx-1.10.2/conf/nginx.conf
开启前面的注释,并修改为$document_root$,如下:
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;
        }

2. 启动php-fpm、nginx

service php-fpm start
service nginx start




0 0
原创粉丝点击