CentOS7安装Nginx-1.9.9+PHP5.6
linux系统CentOS7 用 WinSCP将以下用到的源码包上传到指定的目录下,这里是 /usr/local/src shell端 先安装其他必须软件,安装gcc、gcc-c++ yum -y install gcc gcc-c++ 进入到安装包目录 cd /usr/local/src 解压缩PCRE到当前文件夹 tar -zxvf pcre-8.37.tar.gz 进入解压缩目录 cd pcre-8.37 配置安装目录 ./configure --prefix=/usr/local/pcre 编译 make 安装 make install 返回上级目录 cd ../ 解压缩zlib到当前文件夹 tar -zxvf zlib-1.2.8.tar.gz 进入解压缩目录 cd zlib-1.2.8 配置安装目录 ./configure --prefix=/usr/local/zlib 编译 make 安装 make install 返回上级目录 cd ../ 解压缩jpeg到当前文件夹 tar -zxvf jpegsrc.v9f.tar.gz 进入解压缩目录 cd jpeg-9f 配置安装目录 ./configure --prefix=/usr/local/jpeg --enable-shared --enable-static 编译 make 安装 make install 返回上级目录 cd ../ 解压缩libpng到当前文件夹 tar -zxvf libpng-1.6.45.tar.gz 进入解压缩目录 cd libpng-1.6.45 配置安装目录 ./configure --prefix=/usr/local/png 编译 make 安装 make install 返回上级目录 cd ../ 解压缩freetype到当前文件夹 tar -zxvf freetype-2.5.4.tar.gz 进入解压缩目录 cd freetype-2.5.4 配置安装目录 ./configure --prefix=/usr/local/freetype 编译 make 安装 make install 返回上级目录 cd ../ 解压缩gd2到当前文件夹 tar -zxvf libgd-2.1.1.tar.gz 进入解压缩目录 cd libgd-2.1.1 配置安装目录 ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg/ --with-png=/usr/local/png/ --with-zlib=/usr/local/zlib/ --with-freetype=/usr/local/freetype/ 编译 make 安装 make install 返回上级目录 cd ../ 解压缩openssl到当前文件夹 tar -zxvf openssl-1.0.2e.tar.gz 进入解压缩目录 cd openssl-1.0.2e 配置安装目录 ./config --prefix=/usr/local/openssl 编译 make 安装 make install 返回上级目录 cd ../ 解压缩Nginx到当前文件夹 tar -zxvf nginx-1.9.9.tar.gz 进入Nginx的解压目录 cd nginx-1.9.9 配置 --with-pcre 指的是pcre解压缩后的源码路径。 --with-zlib 指的是zlib解压缩后的源码路径。 --with-openssl指的是openssl解压缩后的源码路径。 ./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/logs/nginx.pid \ --sbin-path=/usr/local/nginx/sbin/nginx \ --lock-path=/usr/local/nginx/logs/nginx.lock \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.37 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.2e 编译 make 安装 make install 返回上级目录 cd ../ 编辑配置文件 找到 #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} 将前面的#全部去掉 并将所有 root html; 变成 root /usr/local/web; 将 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 变成 fastcgi_param SCRIPT_FILENAME /usr/local/web$fastcgi_script_name; location对应匹配不同请求方式或请求路径 root该请求访问的页面存放目录 nginx要调用fastcgi解析PHP文件,/usr/local/web是fastcgi要读取PHP文件的具体位置 nginx默认用9000端口调用php-fpm的 如果保存报错则 :wq! 强制保存(前提root管理权限) vim /usr/local/nginx/conf/nginx.conf /* 启动nginx /usr/local/nginx/sbin/nginx 重启 /usr/local/nginx/sbin/nginx -s reload */ 将nginx放到service控制中将下面代码新建保存到/etc/init.d/nginx中(该方法对于其他服务也同样适用,比如Mysql,php-fpm等等) #!/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/logs/nginx.pid # config: /usr/local/nginx/conf/nginx.conf #nginx程序--sbin-path路径 nginxd=/usr/local/nginx/sbin/nginx #nginx配置文件--conf-path路径 nginx_config=/usr/local/nginx/conf/nginx.conf #nginx pid文件--sbin-path的路径 nginx_pid=/usr/local/nginx/logs/nginx.pid #nginx lock文件--lock-path的路径 nginx_lock=/usr/local/nginx/logs/nginx.lock RETVAL=0 #使service可控制的名称 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 $nginx_lock return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f $nginx_lock $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 :wq 保存并退出 vim /etc/init.d/nginx 设置文件的访问权限 chmod 755 /etc/init.d/nginx 可以使用service控制nginx启用关闭了 service nginx start 让nginx服务加入到开机启动指令管理的服务列表中 chkconfig --add nginx 设置开机启动 chkconfig nginx on 开放防火墙80端口访问 firewall-cmd --zone=public --add-port=80/tcp --permanent 重启防火墙 firewall-cmd --reload 安装php前需要安装必要程序 yum install -y libxml2 libxml2-devel yum install bzip2 bzip2-devel yum install libcurl libcurl-devel 该openssl之前已安装这里可以不用执行 #yum install openssl openssl-devel yum install libmcrypt libmcrypt-devel yum install libxslt-devel yum install -y libXpm-devel 解压缩PHP到当前文件夹 tar -zxvf php-5.6.15.tar.gz 进入解压缩目录 cd php-5.6.15 配置PHP安装,Nginx+PHP整合 #./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fastCGI --enable-fpm 使用最新配置 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-config-file-scan-dir=/usr/local/php/etc/conf.d --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-gd=/usr/local/gd2 --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --with-freetype-dir=/usr/local/freetype --with-zlib-dir=/usr/local/zlib --with-xpm-dir=/usr/lib64 --with-pear --with-curl --with-iconv --with-mcrypt --with-mhash --with-zlib --with-xmlrpc --with-xsl --with-openssl --with-mysql --with-mysqli --with-pdo-mysql --with-bz2 --with-gettext --enable-fpm 编译 make 安装 make install 如果编译报错 undefined reference to `XpmLibraryVersion' 执行 rpm -ql libXpm 查找libXpm目录,这里发现在/usr/lib64/ 下,重新编译php,./configure 中增加 --with-xpm-dir=/usr/lib64,这里./configure已经添加 编译错误可执行清理后重新编译 make clean 将安装包里的开发版php.ini复制到配置php安装时指定存放php.ini的位置即--with-config-file-path指向的位置 cp php.ini-development /usr/local/php/php.ini 将/usr/local/php/etc/php-fpm.conf.default同目录下复制一份命名php-fpm.conf cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 使php-fpm.pid保存在指定目录/usr/local/php/var/run/php-fpm.pid(设定开机启动时用到) 找到 ;pid = run/php-fpm.pid 去掉前面的 ; vim /usr/local/php/etc/php-fpm.conf /* 启动php-fpm /usr/local/php/sbin/php-fpm */ 编辑php.ini php5默认<?php echo date("Y-m-d H:i:s");?>时间与北京时间相差八小时(少八小时) 为什么呢?PHP5系列版本新增了时区设置,默认为格林威治时间,与中国所在的东8区正好相差8个小时 查找 ;date.timezone = 将;去掉,并修改成 date.timezone = PRC 除了E_NOTICE类型的错误(Notice)不报告,其他的都报告,查找 error_reporting = E_ALL 改成 error_reporting = E_ALL & ~E_NOTICE 使 include 等可以包含域外网页如 include_once "http://www.126.com"; 找到 allow_url_include = Off 改成 allow_url_include = On vim /usr/local/php/php.ini 将php-fpm放到service控制中将下面代码新建保存到/etc/init.d/php-fpm中 #!/bin/bash # php-fpm Startup script for the php-fpm # it is v.5.5.0 version. # chkconfig: - 85 15 # description: php-fpm is very good # processname: php-fpm # pidfile: /usr/local/php/var/run/php-fpm.pid # config: /usr/local/php/etc/php-fpm.conf php_command=/usr/local/php/sbin/php-fpm php_config=/usr/local/php/etc/php-fpm.conf php_pid=/usr/local/php/var/run/php-fpm.pid RETVAL=0 #使service可控制的名称 prog="php-fpm" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network #start function start(){ if [ -e $php_pid ] then echo "php-fpm already start..." exit 1 fi $php_command } stop(){ if [ -e $php_pid ] then parent_pid=`cat $php_pid` all_pid=`ps -ef | grep php-fpm | awk '{if('$parent_pid' == $3){print $2}}'` for pid in $all_pid do kill $pid done kill $parent_pid fi exit 1 } restart(){ stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|status}" exit 1 esac exit $RETVAL :wq 保存并退出 vim /etc/init.d/php-fpm 设置文件的访问权限 chmod 755 /etc/init.d/php-fpm 可以使用service控制nginx启用关闭了 service php-fpm start 让nginx服务加入到开机启动指令管理的服务列表中 chkconfig --add php-fpm 设置开机启动 chkconfig php-fpm on 单独安装pdo_mysql 先安装 autoconf yum install autoconf 下载mysqlpdo包,这里是 PDO_MYSQL-1.0.2.tgz,放到 /usr/local/src 进入到下载目录 cd /usr/local/src 解压缩 tar -zxvf PDO_MYSQL-1.0.2.tgz 进入源码目录 cd PDO_MYSQL-1.0.2 在此目录使用php扩展编译环境 /usr/local/php/bin/phpize 执行加载 ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql 编译安装 make && make install 最后得到类似一个目录 /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ 为方便,把pdo_mysql.so文件移动到extensions目录下 mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so /usr/local/php/lib/php/extensions/ 在make之前要做一个mysql的header文件的软连接。因为mysql安装的时候指定了目录,不做软连接找不到header文件。 ln -s /usr/local/mysql/include/* /usr/local/include/ 在/usr/local/php/php.ini 中编辑 找到; extension_dir = "./",去掉注释,并修改路径 extension_dir = "/usr/local/php/lib/php/extensions/" 将 extension=php_pdo_mysql.dll 改成 extension=pdo_mysql.so 附录 nginx configure 配置选项 --prefix=<path> : Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。 --sbin-path=<path> : Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。 --conf-path=<path> : 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。 --pid-path=<path> : 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。 --lock-path=<path> : nginx.lock文件的路径,默认为<prefix>/logs/nginx.lock --error-log-path=<path> : 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。 --http-log-path=<path> : 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。 --user=<user> : 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。 --group=<group> : 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。 --builddir=<DIR> : 指定编译的目录 nginx configure 配置选项模块部分 --with-rtsig_module : 启用 rtsig 模块 --with-select_module --without-select_module : 允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式 --with-poll_module --without-poll_module : Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure. --with-http_ssl_module : 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl --with-http_realip_module : 启用 ngx_http_realip_module --with-http_addition_module : 启用 ngx_http_addition_module --with-http_sub_module : 启用 ngx_http_sub_module --with-http_dav_module : 启用 ngx_http_dav_module --with-http_flv_module : 启用 ngx_http_flv_module --with-http_stub_status_module : 启用 "server status" 页 --without-http_charset_module : 禁用 ngx_http_charset_module --without-http_gzip_module : 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。 --without-http_ssi_module : 禁用 ngx_http_ssi_module --without-http_userid_module : 禁用 ngx_http_userid_module --without-http_access_module : 禁用 ngx_http_access_module --without-http_auth_basic_module : 禁用 ngx_http_auth_basic_module --without-http_autoindex_module : 禁用 ngx_http_autoindex_module --without-http_geo_module : 禁用 ngx_http_geo_module --without-http_map_module : 禁用 ngx_http_map_module --without-http_referer_module : 禁用 ngx_http_referer_module --without-http_rewrite_module : 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。 --without-http_proxy_module : 禁用 ngx_http_proxy_module --without-http_fastcgi_module : 禁用 ngx_http_fastcgi_module --without-http_memcached_module : 禁用 ngx_http_memcached_module --without-http_limit_zone_module : 禁用 ngx_http_limit_zone_module --without-http_empty_gif_module : 禁用 ngx_http_empty_gif_module --without-http_browser_module : 禁用 ngx_http_browser_module --without-http_upstream_ip_hash_module : 禁用 ngx_http_upstream_ip_hash_module --with-http_perl_module : 启用 ngx_http_perl_module --with-perl_modules_path=<PATH> : 指定 perl 模块的路径 --with-perl=<PATH> : 指定 perl 执行文件的路径 --http-log-path=<PATH> : Set path to the http access log --http-client-body-temp-path=<PATH> : Set path to the http client request body temporary files --http-proxy-temp-path=<PATH> : Set path to the http proxy temporary files --http-fastcgi-temp-path=<PATH> : Set path to the http fastcgi temporary files --without-http : 禁用 HTTP server --with-mail : 启用 IMAP4/POP3/SMTP 代理模块 --with-mail_ssl_module : 启用 ngx_mail_ssl_module --with-cc=<PATH> : 指定 C 编译器的路径 --with-cpp=<PATH> : 指定 C 预处理器的路径 --with-cc-opt=<OPTIONS> : Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048". --with-ld-opt=<OPTIONS> : Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib". --with-cpu-opt=<CPU> : 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 --without-pcre : 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。 --with-pcre=<DIR> : 指定 PCRE 库的源代码的路径。 --with-pcre-opt=<OPTIONS> : Set additional options for PCRE building. --with-md5=<DIR> : Set path to md5 library sources. --with-md5-opt=<OPTIONS> : Set additional options for md5 building. --with-md5-asm : Use md5 assembler sources. --with-sha1=<DIR> : Set path to sha1 library sources. --with-sha1-opt=<OPTIONS> : Set additional options for sha1 building. --with-sha1-asm : Use sha1 assembler sources. --with-zlib=<DIR> : Set path to zlib library sources. --with-zlib-opt=<OPTIONS> : Set additional options for zlib building. --with-zlib-asm=<CPU> : Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro --with-openssl=<DIR> : Set path to OpenSSL library sources --with-openssl-opt=<OPTIONS> : Set additional options for OpenSSL building --with-debug : 启用调试日志 --add-module=<PATH> : Add in a third-party module found in directory PAT