淺談搭建LNMP環境

前言

前置條件
首先要有一臺linux 服務器,阿里雲、騰訊雲等等都行
由於我之前買的是騰訊雲的服務器,本文就以騰訊雲服務器爲例

操作環境

LNMP(Linux+Nginx+Mysql+PHP)

  1. linux 版本
    在這裏插入圖片描述

  2. nginx 版本
    在這裏插入圖片描述

  3. Mysql版本
    在這裏插入圖片描述

  4. Xshell版本
    在這裏插入圖片描述

  5. php版本

 php-7.3.4

安裝步驟

Mysql 以及Nginx的安裝請參考博主的另一篇文章
https://blog.csdn.net/gao36951/article/details/73321345

PHP 環境準備(php-7.3.4.tar.gz)

下載地址:https://www.php.net/downloads.php

cd /root/php/
tar -zxvf php-7.3.4.tar.gz
cd php-7.3.4
./configure   --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --with-png-dir=/usr/local/libpng   --with-jpeg-dir=/usr/local/jpeg   --with-freetype-dir=/usr/local/freetype   --with-zlib-dir=/usr/local/zlib   --with-libxml-dir=/usr/local/libxml2/   --with-iconv-dir=/usr/local/libiconv   --enable-libxml   --enable-xml   --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization   --enable-opcache   --enable-mbregex   --enable-fpm   --enable-mbstring=all   --with-openssl   --enable-pcntl   --enable-sockets   --with-xmlrpc   --enable-zip   --enable-soap   --without-pear   --with-gettext   --enable-session   --with-curl   --enable-ctype   --enable-shared   --with-gd

  • 執行上面的可能報一下錯誤,解決方案已經附上
執行報錯:
configure: error: in `/root/php/php-7.3.4':
configure: error: no acceptable C compiler found in $PATH
解決方案:
yum install gcc
執行報錯:
checking whether to use system default cipher list instead of hardcoded value... no
checking for RAND_egd... no
checking for pkg-config... /usr/bin/pkg-config
configure: error: Cannot find OpenSSL's <evp.h>
解決方案:
yum install openssl openssl-devel
ln -s /usr/lib64/libssl.so /usr/lib/

執行報錯:
checking pcre install prefix... no
checking libzip... yes
checking for the location of zlib... /usr
checking for pkg-config... (cached) /usr/bin/pkg-config
checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
解決方案:
yum remove libzip -y
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install
  • 解決完上述問題後繼續
./configure   --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --with-png-dir=/usr/local/libpng   --with-jpeg-dir=/usr/local/jpeg   --with-freetype-dir=/usr/local/freetype   --with-zlib-dir=/usr/local/zlib   --with-libxml-dir=/usr/local/libxml2/   --with-iconv-dir=/usr/local/libiconv   --enable-libxml   --enable-xml   --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization   --enable-opcache   --enable-mbregex   --enable-fpm   --enable-mbstring=all   --with-openssl   --enable-pcntl   --enable-sockets   --with-xmlrpc   --enable-zip   --enable-soap   --without-pear   --with-gettext   --enable-session   --with-curl   --enable-ctype   --enable-shared   --with-gd --with-fpm-user=nginx  --with-fpm-group=nginx 
  • 成功如下圖所示

在這裏插入圖片描述

  • 編譯安裝
make && make install
執行報錯:
In file included from /root/php/php-7.3.4/ext/zip/php_zip.h:31:0,
                 from /root/php/php-7.3.4/ext/zip/php_zip.c:36:
/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory
 #include <zipconf.h>
                     ^
compilation terminated.
make: *** [ext/zip/php_zip.lo] Error 1
解決方案:
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
  • 解決完錯誤後繼續編譯安裝
make && make install
  • 編譯安裝成功後如下圖:

在這裏插入圖片描述

  • 配置與啓動PHP
cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
  • 配置php-fpm.conf文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf 
  • 在php.sh中添加環境變量
vim /etc/profile.d/php.sh
export PATH=$PATH:/usr/local/php/bin/:/usr/local/php/sbin/
  • 使環境變量生效
source /etc/profile.d/php.sh
  • 啓動PHP服務php-fpm
service php-fpm start
  • 檢查啓動進程與偵聽端口號
ps -ef | grep php-fpm

在這裏插入圖片描述

netstat -lntup | grep 9000

在這裏插入圖片描述
LNMP環境測試

  • 配置nginx
vim /etc/nginx/nginx.conf
    server {
        listen       80;
        server_name  localhost;
        include /etc/nginx/default.d/*.conf;
        location / {
           root           /project/php;
        }
        location  /favicon.ico {
           root           /project/php;
        }
        location ~ \.php$ {
           root           /project/php;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

  • 重新加載配置文件啓動nginx
nginx -s reload
  • 測試LNMP環境是否生效
cd /project/php
touch test.php
echo "<?php phpinfo(); ?>" > test.php
  • 在瀏覽器輸入地址http://{IP或者域名}/test.php 出現下面的頁面說明成功
    在這裏插入圖片描述

參考:
https://blog.csdn.net/mangojo/article/details/86475554
http://www.kwx.gd/PHPEnvironment/CetnOS-libzip.html
https://blog.csdn.net/alexdream/article/details/7408561
https://blog.csdn.net/duguduchong/article/details/8699774
https://blog.csdn.net/weixin_42579642/article/details/85290670
http://www.kuitao8.com/20180429/4689.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章