千家信息网

怎么在centos下安装nginx和php

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要讲解了"怎么在centos下安装nginx和php ",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么在centos下安装nginx和ph
千家信息网最后更新 2026年11月02日怎么在centos下安装nginx和php

这篇文章主要讲解了"怎么在centos下安装nginx和php ",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么在centos下安装nginx和php "吧!

sudo systemctl start php-fpm

yum 安装

sudo yum -y install nginx    启动nginx,在本机浏览器访问nginx页面,检查服务是否启动成功sudo systemctl start  nginx访问url:http://ip:80

安装shell 脚本

#!/bin/bash    echo 'start install nginx...'    wget http://nginx.org/download/nginx-1.10.2.tar.gz    tar -xvf nginx-1.10.2.tar.gz    cd nginx-1.10.2    yum -y install gcc pcre-devel openssl-devel    ./configure    make    make install    echo 'install nginx successful.'fi

安装php与php-fpm

sudo yum -y install php php-fpm

启动php-fpm服务

sudo systemctl start php-fpm

下面是nginx.conf 的配置文件

核心代码就是 location.php 这一段

# For more information on configuration, see:#   * Official English Documentation: http://nginx.org/en/docs/#   * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {    worker_connections 1024;}http {    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  /var/log/nginx/access.log  main;    sendfile            on;    tcp_nopush          on;    tcp_nodelay         on;    keepalive_timeout   65;    types_hash_max_size 2048;    include             /etc/nginx/mime.types;    default_type        application/octet-stream;    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx_core_module.html#include    # for more information.    include /etc/nginx/conf.d/*.conf;    server {        listen       8008 default_server;        #listen       [::]:80 default_server;        server_name  _;        root         /usr/share/nginx/html;        # Load configuration files for the default server block.        include /etc/nginx/default.d/*.conf;                location ~ \.php$ {                   fastcgi_pass   127.0.0.1:9000;           #fastcgi服务端口,将http请求代理到此端口                   fastcgi_index  index.php;                    #fastcgi服务默认页面                   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置请求的脚本文件路径                   include        fastcgi_params;                        }        location / {        }        error_page 404 /404.html;            location = /40x.html {        }        error_page 500 502 503 504 /50x.html;            location = /50x.html {        }    }# Settings for a TLS enabled server.##    server {#        listen       443 ssl http2 default_server;#        listen       [::]:443 ssl http2 default_server;#        server_name  _;#        root         /usr/share/nginx/html;##        ssl_certificate "/etc/pki/nginx/server.crt";#        ssl_certificate_key "/etc/pki/nginx/private/server.key";#        ssl_session_cache shared:SSL:1m;#        ssl_session_timeout  10m;#        ssl_ciphers HIGH:!aNULL:!MD5;#        ssl_prefer_server_ciphers on;##        # Load configuration files for the default server block.#        include /etc/nginx/default.d/*.conf;##        location / {#        }##        error_page 404 /404.html;#            location = /40x.html {#        }##        error_page 500 502 503 504 /50x.html;#            location = /50x.html {#        }#    }}
nginx -tsudo systemctl reload  nginx

下面几种nginx 的重启方法

在重启之前 先加入配置

判断Nginx配置是否正确命令如下:
nginx -t -c /etc/nginx/nginx.conf

nginx -s reload

------------------------------------------------------------------------------------------------------------------------------

2 编译安装

安装前准备

安装一些编译工具和依赖包等

sudo yum -y install gcc automake autoconf libtool make unzip gcc-c++ glibc gd-develsudo yum -y install libmcrypt-devel mhash-devel libxslt-devel \libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \zlib zlib-devel  glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \krb5 krb5-devel libidn libidn-devel openssl openssl-devel

安装nginx

下载官方软件包

wget http://nginx.org/download/nginx-1.4.2.tar.gz

解压

tar -xvf nginx-1.4.2.tar.gz

创建安装目录

sudo mkdir /usr/local/nginx

编译安装

cd nginx-1.4.2./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx  --conf-path=/usr/local/nginx/conf/nginx.conf --user=nginx --group=nginx sudo make && sudo make install

起服务

cd /usr/local/nginx/sbin/sudo ./nginx

出现 make[1]: Leaving directory `/home/xxx/nginx-1.4.2',不用管。

安装php

下载官方软件包

wget http://cn2.php.net/distributions/php-5.6.39.tar.gz

解压

tar -xvf php-5.6.39.tar.gz

创建安装目录

sudo mkdir /usr/local/php

编译安装

./configure --prefix=/usr/local/php  \--with-config-file-path=/usr/local/php/etc \--enable-fpm --with-mcrypt \--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \--with-gd --with-jpeg-dirsudo make all install

进行php-fpm的用户设置

sudo groupadd wwwsudo useradd www -g www -s /sbin/nologincd /usr/local/phpsudo cp etc/php-fpm.conf.default etc/php-fpm.confsudo vi etc/php-fpm.conf   #修改以下两个参数user = wwwgroup = www

起服务

sudo  /usr/local/php/sbin/php-fpm

在nginx.conf进行php的为配置

在http{}的server{}里添加

 vim /usr/local/nginx/conf/nginx.conf    index        index.html index.htm index.php;location ~ \.php$ {             fastcgi_pass   127.0.0.1:9000;           #fastcgi服务端口,将http请求代理到此端口 fastcgi_index  index.php;                    #fastcgi服务默认页面 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置请求的脚本文件路径 include        fastcgi_params;             }

添加php测试页面

vim  /usr/local/nginx/html/index.php

重启nginx服务

cd /usr/local/nginx/sbin/sudo ./nginx

测试:在本机上访问该页面

http://ip:80/index.php

可能会遇到以下问题

错误为:./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题
yum -y install pcre-devel

还有可能出现:

错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl= options.

解决办法:

yum -y install openssl openssl-devel

感谢各位的阅读,以上就是"怎么在centos下安装nginx和php "的内容了,经过本文的学习后,相信大家对怎么在centos下安装nginx和php 这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

服务 页面 端口 编译 配置 文件 脚本 问题 学习 内容 官方 就是 目录 路径 软件 软件包 错误 本机 代理 测试 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 重生之互联网科技帝国 互联网科技深圳 互联网巨头反垄断金融科技 金融科技与互联网金融 中国互联网科技发展 上海喔噻互联网科技 互联网金融 科技创新 互联网科技网站 天速互联网络科技有限公司 深圳国际航运科技和互联网 互联网科技公司实习生 亳州好赞互联网科技有限公司 互联网科技实习生岗位 广州百诚互联网科技有限公司官网 互联网高科技行业有哪些企业 唐山双旺合互联网科技产业园 绵阳真晨互联网科技有限公司 河南商务圈互联网科技有限公司 瑞安市蟠桃会互联网科技有限公司 株洲百竞科技的互联网营销顾问 西安互联网科技 互联网科技英文 衡水科技产业互联网平台 青海省互联网新科技 福州新华互联网科技学校 知乎 湖南诚度互联网科技有限公司 链环科技传统企业怎么转型互联网 小米互联网科技园入驻 互联网科技平台产品运营 米泉互联网科技专业怎么样
0