溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在centos下安裝nginx和php

發(fā)布時間:2021-08-09 18:17:23 來源:億速云 閱讀:122 作者:chen 欄目:云計算

這篇文章主要講解了“怎么在centos下安裝nginx和php ”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么在centos下安裝nginx和php ”吧!

sudo systemctl start php-fpm

yum 安裝

sudo yum -y install nginx    
啟動nginx,在本機瀏覽器訪問nginx頁面,檢查服務(wù)是否啟動成功

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服務(wù)

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服務(wù)端口,將http請求代理到此端口
		   fastcgi_index  index.php;                    #fastcgi服務(wù)默認(rèn)頁面
		   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #設(shè)置請求的腳本文件路徑
		   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 編譯安裝

安裝前準(zhǔn)備

安裝一些編譯工具和依賴包等

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

創(chuàng)建安裝目錄

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

起服務(wù)

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

出現(xiàn) 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

創(chuàng)建安裝目錄

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的用戶設(shè)置

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   #修改以下兩個參數(shù)user = www
group = www

起服務(wù)

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服務(wù)端口,將http請求代理到此端口 fastcgi_index  index.php;                    #fastcgi服務(wù)默認(rèn)頁面 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #設(shè)置請求的腳本文件路徑 include        fastcgi_params;
             }

添加php測試頁面

vim  /usr/local/nginx/html/index.php<?phpphpinfo();?>

重啟nginx服務(wù)

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

還有可能出現(xiàn):

錯誤提示:./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 ”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么在centos下安裝nginx和php 這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI