温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在centos下安装nginx和php

发布时间:2021-08-09 18:17:23 来源:亿速云 阅读:114 作者:chen 栏目:云计算

这篇文章主要讲解了“怎么在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 -t
sudo 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-devel
sudo 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-dir
sudo make all install

进行php-fpm的用户设置

sudo groupadd www
sudo useradd www -g www -s /sbin/nologin
cd /usr/local/php
sudo cp etc/php-fpm.conf.default etc/php-fpm.conf
sudo vi etc/php-fpm.conf   #修改以下两个参数user = www
group = 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<?phpphpinfo();?>

重启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=<path> options.

解决办法:

yum -y install openssl openssl-devel

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI