前言:
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP以其开发源代码,免费,快捷,跨平台,高效,面向对象,强大的动态图像创建等功能深受广大开发者的喜爱。本文描述基于CentOS 7.2下编译安装PHP 5.6.35。
1.安装编译工具、依赖包
[root@Mike-VM-Node-172_31_225_214 ~]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel -y
2.下载解压PHP
[root@Mike-VM-Node-172_31_225_214 ~]# cd /usr/local/ [root@Mike-VM-Node-172_31_225_214 /usr/local]# wget https://www.php.net/distributions/php-5.6.35.tar.gz [root@Mike-VM-Node-172_31_225_214 /usr/local]# tar zxvf php-5.6.35.tar.gz && rm -rf php-5.6.35.tar.gz
3.编译安装
[root@Mike-VM-Node-172_31_225_214 /usr/local]# cd php-5.6.35/ [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-fpm-group=nginx --with-fpm-user=nginx --enable-pcntl --with-mysql [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# make && make install [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# cp php.ini-production /etc/php.ini [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# chmod +x /etc/init.d/php-fpm [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]#
备注:如果是安装 PHP7 以上的话会多这一步操作,cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
4.配置环境变量
[root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# vim /etc/profile PATH=$PATH:/usr/local/php/bin export PATH [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# source /etc/profile [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# php -v PHP 5.6.35 (cli) (built: Nov 11 2020 11:18:52) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]#
5.启动PHP
[root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# /etc/init.d/php-fpm start Starting php-fpm done [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]#
6.PHP进程数的设置
根据配置的解析,在php-fpm.conf文件中添加如下配置:
[root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# cd /usr/local/php/etc/ [root@Mike-VM-Node-172_31_225_214 /usr/local/php/etc]# ll total 52 -rw-r--r-- 1 root root 1233 Nov 11 11:19 pear.conf -rw-r--r-- 1 root root 23054 Nov 11 11:21 php-fpm.conf -rw-r--r-- 1 root root 23054 Nov 11 11:19 php-fpm.conf.default [root@Mike-VM-Node-172_31_225_214 /usr/local/php/etc]# vim php-fpm.conf pm.max_children = 100 pm.start_servers = 30 pm.min_spare_servers = 20 pm.max_spare_servers = 100 pm.max_requests = 500 [root@Mike-VM-Node-172_31_225_214 /usr/local/php-5.6.35]# /etc/init.d/php-fpm restart
7.安装扩展Redis模块
[root@Mike-VM-Node-172_31_225_214 ~]# cd /usr/local/ [root@Mike-VM-Node-172_31_225_214 /usr/local]# [root@Mike-VM-Node-172_31_225_214 /usr/local]# wget http://pecl.php.net/get/redis-3.1.6.tgz --2020-11-11 13:16:39-- http://pecl.php.net/get/redis-3.1.6.tgz Resolving pecl.php.net (pecl.php.net)... 104.236.228.160 Connecting to pecl.php.net (pecl.php.net)|104.236.228.160|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 199920 (195K) [application/octet-stream] Saving to: ‘redis-3.1.6.tgz’ 100%[=========================================================================================>] 199,920 334KB/s in 0.6s 2020-11-11 13:16:40 (334 KB/s) - ‘redis-3.1.6.tgz’ saved [199920/199920] [root@Mike-VM-Node-172_31_225_214 /usr/local]# tar zxvf redis-3.1.6.tgz && rm -rf redis-3.1.6.tgz package.xml [root@Mike-VM-Node-172_31_225_214 /usr/local]# cd redis-3.1.6/ [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# make && make install [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# vim /etc/php.ini
######添加#########
extension = redis.so
[root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]# php -m redis [root@Mike-VM-Node-172_31_225_214 /usr/local/redis-3.1.6]#
这里输入 php -m 看到有redis就证明安装扩展成功了~
8.安装扩展 Swoole 模块
[root@Mike-VM-Node-172_31_225_214 /usr/local]# wget https://github.com/swoole/swoole-src/archive/v1.9.3-stable.tar.gz --2020-11-11 13:25:06-- https://github.com/swoole/swoole-src/archive/v1.9.3-stable.tar.gz Resolving github.com (github.com)... 192.30.255.112 Connecting to github.com (github.com)|192.30.255.112|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/swoole/swoole-src/tar.gz/v1.9.3-stable [following] --2020-11-11 13:25:06-- https://codeload.github.com/swoole/swoole-src/tar.gz/v1.9.3-stable Resolving codeload.github.com (codeload.github.com)... 192.30.255.121 Connecting to codeload.github.com (codeload.github.com)|192.30.255.121|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/x-gzip] Saving to: ‘v1.9.3-stable.tar.gz’ [ <=> ] 609,837 875KB/s in 0.7s 2020-11-11 13:25:08 (875 KB/s) - ‘v1.9.3-stable.tar.gz’ saved [609837] [root@Mike-VM-Node-172_31_225_214 /usr/local]# tar zxvf v1.9.3-stable.tar.gz && rm -rf v1.9.3-stable.tar.gz [root@Mike-VM-Node-172_31_225_214 /usr/local]# cd swoole-src-1.9.3-stable/ [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# make && make install [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# vim /etc/php.ini ####添加#### extension=swoole.so [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]# php -m swoole [root@Mike-VM-Node-172_31_225_214 /usr/local/swoole-src-1.9.3-stable]#
这里输入 php -m 看到有 swoole 就证明安装扩展成功了~
9.PHP禁用危险函数
在 /etc/php.ini 下添加
disable_functions = phpinfo,passthru,chroot,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,symlink,popen,stream_socket_server
10.Nginx 配置连接PHP
Nginx 安装详见 https://www.cnblogs.com/mike666/p/13926545.html
主 nginx.conf 配置
user nginx nginx; worker_processes auto; error_log logs/error.log ; worker_rlimit_nofile 65535; pid logs/nginx.pid; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; log_format upstream2 '$proxy_add_x_forwarded_for $remote_user [$time_local] "$request" $http_host' '$body_bytes_sent "$http_referer" "$http_user_agent" $ssl_protocol $ssl_cipher' '$request_time [$status] [$upstream_status] [$upstream_response_time] "$upstream_addr"'; access_log logs/access.log; server_names_hash_bucket_size 128; server_names_hash_max_size 1024; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; server_tokens off; tcp_nodelay on; keepalive_timeout 120; proxy_connect_timeout 1000s; proxy_read_timeout 2000; proxy_send_timeout 2000; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Host $host; proxy_set_header Referer $http_referer; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon image/jpeg image/gif image/png; gzip_disable "msie6"; #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. #open_file_cache max=1000 inactive=20s; #open_file_cache_valid 30s; #open_file_cache_min_uses 2; #open_file_cache_errors on; server { listen 80; server_name localhost; index.php index.html index.htm; root /www/wwwroot; 跨域问题################################## location ~* .(eot|ttf|woff|svg|otf)$ { add_header Access-Control-Allow-Origin *; } ####PHP虚拟配置############################### 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.conf; } } ####默认配置SSL证书############################# #server { # # listen 443 default_server; # server_name _; # ssl on; # ssl_certificate ssl/test.com/chao.crt; # ssl_certificate_key ssl/test.com/chao.key; # ssl_session_timeout 1d; # ssl_session_cache shared:SSL:50m; # ssl_session_tickets off; # ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # rewrite ^(.*) https://www.baidu.com/$1 permanent; #} ##########################vhost##################################### include conf.d/*.conf; }
虚拟主机配置
server { listen 80; server_name php.com; index index.php index.html index.htm; root /www/wwwroot; error_log /data/logs/error.log; access_log /data/logs/access.log; location ~ [^/].php(/|$) { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; } location ~* .(eot|otf|ttf|woff|woff2|svg)$ { add_header Access-Control-Allow-Origin *; } location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } root html; index index.html index.htm; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /.well-known { allow all; } location ~ /. { deny all; } }
以上是 Nginx 连接 PHP 的配置~
本文分享完毕,感谢支持点赞~~