CentOS快速安装PHP7.4与Nginx指南

4 下载量 70 浏览量 更新于2024-08-30 收藏 61KB PDF 举报
本文将详细介绍在CentOS操作系统上安装PHP7.4和Nginx的步骤,包括必要的准备工作,安装扩展,以及配置和启动服务。 首先,为了在CentOS上安装PHP7.4和Nginx,我们需要进行以下准备工作: 1. 下载所需的安装包: 使用`wget`命令分别下载PHP7.4和Nginx的源码包: ``` wget https://www.php.net/distributions/php-7.4.0.tar.gz wget http://nginx.org/download/nginx-1.17.6.tar.gz ``` 2. 安装必备的开发工具和扩展库: 使用`yum`命令安装一系列依赖项,包括编译工具、库和开发头文件,这些对于构建和配置PHP和Nginx是必要的: ``` yum install -y gcc gcc-c++ make cmake bison autoconf wget lrzsz libtool libtool-ltdl-devel freetype-devel libjpeg.x86_64 libjpeg-devel libpng-devel gd-devel python-devel patch sudo openssl* openssl-devel ncurses-devel bzip2 bzip2-devel unzip zlib-devel libevent* libxml* libxml2-devel libcurl* curl-devel readline-devel sqlite-devel libsodium-devel oniguruma-5.9.5-3.el7.x86_64.rpm oniguruma-devel-5.9.5-3.el7.x86_64.rpm ``` 接下来,我们将逐步安装PHP: 1. 解压PHP源码包并进入目录: ``` tar -zxvf php-7.4.0.tar.gz cd php-7.4.0 ``` 2. 配置编译选项: 运行`./configure`命令,指定安装路径和其他必要的选项,以确保PHP与MySQL、OpenSSL、mysqli等库兼容,并开启优化功能,禁用调试和运行时路径: ``` ./configure --prefix=/usr/local/php \ --with-config-file-scan-dir=/usr/local/php/etc/ \ --with-mhash --with-pdo-mysql \ --with-openssl --with-mysqli \ --with-iconv --with-zlib \ --enable-inline-optimization \ --disable-debug --disable-rpath \ --enable-shared --enable-xml \ --enable-bcmath ... ``` 注意,这里的省略号表示配置选项可能还有其他项,具体根据实际需求添加。 3. 编译和安装: ``` make && make install ``` 然后,我们需要配置PHP-FPM(PHP FastCGI Process Manager): 1. 创建php-fpm配置文件目录并拷贝默认配置: ``` mkdir /usr/local/php/etc cp php.ini-development /usr/local/php/etc/php.ini ``` 2. 启动和设置开机启动php-fpm服务: ``` /usr/local/php/sbin/php-fpm systemctl enable php-fpm.service ``` 现在,我们转向Nginx的安装: 1. 解压Nginx源码包并进入目录: ``` tar -zxvf nginx-1.17.6.tar.gz cd nginx-1.17.6 ``` 2. 配置编译选项: 根据系统环境调整Nginx配置,包括指定PHP解析器的位置: ``` ./configure --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-cc-opt='-Wno-deprecated-declarations' \ --with-ld-opt='-Wl,-rpath,/usr/local/lib:/usr/local/php/lib' \ --add-module=/usr/local/src/ngx_http_substitutions_filter_module-0.7.1 \ --add-module=/usr/local/src/ngx_devel_kit-0.3.1 \ --add-module=/usr/local/src/nginx-module-vts \ --with-openssl \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_sub_module=dynamic \ --with-http_geoip_module=dynamic \ --with-http_perl_module=dynamic \ --with-mail \ --with-mail_ssl_module \ --with-pcre \ --with-pcre-jit \ --with-http_gzip_static_module \ --with-threads \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_split_lines_module \ --with-stream_hash_module \ --with-stream_set_var_module \ --with-stream_access_log_module \ --with-stream_limit_conn_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-cc-opt="-O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" \ --with-ld-opt="-Wl,-rpath,/usr/local/lib" \ --with-http_php_module=/usr/local/php/lib/php7_module.so ``` 3. 编译和安装Nginx: ``` make && make install ``` 4. 创建Nginx配置文件并启动服务: ``` vi /usr/local/nginx/conf/nginx.conf # 在配置文件中添加处理PHP请求的部分,例如: location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } systemctl start nginx systemctl enable nginx ``` 至此,你已经在CentOS上成功安装了PHP7.4和Nginx,并完成了基本配置。你可以通过访问服务器上的PHP测试文件来验证安装是否成功。记得在生产环境中对配置文件进行安全审查和优化,以确保最佳性能和安全性。