记录一次编译安装php与nginx笔记
以下所有操作均在root 权限下执行,如非root用户,有些命令需要sudo
安装编译所需相关软件
- yum install gcc gcc++ libxml2-devel
安装php
下载源代码
解压
- tar -zxvf mirror
编译安装php
cd php-7.1.12
./configure –prefix=/usr/local/php7 –enable-fpm (–enable-fpm 添加 php-fpm,nginx与之实现php的通信)
make
undefined reference to `libiconv'
解决办法:
1. wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
2. tar -zxvf libiconv-1.13.1.tar.gz
3. cd libiconv-1.13.1
4. ./configure --prefix=/usr/local/libiconv
5. make
6. make install
cd ~/php-7.1.12
./configure –prefix=/usr/local/php7 –with-iconv=/usr/local/libiconv –enable-fpm
make
make install
测试php
- cd ~
- vi test.php
<?php
echo 'hello world!';
- /usr/local/php7/bin/php test.php
hello world
安装nginx 与 pcre(用于支持正则)
下载pcre源代码
- cd ~
- wget https://ftp.pcre.org/pub/pcre/pcre-8.36.tar.gz (pcre源代码,用于支持正则,尽可能下载8.36及之前版本,太高版本, 编译nginx会出问题)
解压
- tar -zxvf pcre-8.36.tar.gz
编译安装
- cd pcre-8.36/
- ./configure –prefix=/usr/local/pcre
- make
- make install
下载nginx源代码
解压
- tar -zxvf nginx-1.12.2.tar.gz
编译安装
- cd nginx-1.12.2/
- ./configure –prefix=/usr/local/nginx –with-pcre=/root/pcre-8.36
- make
出现以下问题,说明pcre版本过高,需要更换版本
No rule to make target `libpcre.la'. Stop
- make install
测试nginx
- /usr/local/nginx/sbin
- ps aux | grep nginx (出现nginx进程)
- 浏览器输入IP地址查看,能看到nginx默认页面
配置nginx与php(php-fpm)通信
启动php-fpm
- cd /usr/local/php7/sbin
- ./php-fpm
ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf'
ERROR: FPM initialization failed
以上说明php-fpm的配置文件不存在,因为默认php-fpm的配置文件有一个默认模板文件,只需要复制一份修改后缀即可
- cd /usr/local/php7/etc/
- cp php-fpm.conf.default php-fpm.conf
- cd /usr/local/php7/sbin/
- ./php-fpm
WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125.
ERROR: No pool defined. at least one pool section must be specified in config file
ERROR: failed to post process the configuration
ERROR: FPM initialization failed
此问题与上述一致
- cd /usr/local/php7/etc/php-fpm.d/
- cp www.conf.default www.conf
- cd /usr/local/php7/sbin/
- ./php-fpm
- ps aux | grep php-fpm (查看php-fpm进程是否存在)
nginx配置
- cd /usr/local/nginx/conf
- vi nginx.conf
-修改成以下内容(主要是打开了 FastCGI 配置,默认是注释掉的,还有修改了fastcgi_param)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- /usr/local/nginx/sbin/nginx -s reload (修改配置文件后,重启nginx)
测试
- cd /usr/local/nginx/html (默认nginx 网站根目录)
- vi test.php
<?php
echo 'hello world!';
- 浏览器访问 ip/test.php
以上,就是本次实录所有过程。