溫馨提示×

溫馨提示×

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

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

Linux系統(tǒng)如何配置LNMP

發(fā)布時間:2022-01-24 10:05:06 來源:億速云 閱讀:97 作者:小新 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關Linux系統(tǒng)如何配置LNMP,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

LNMP簡介:

LNMP代表的就是:Linux系統(tǒng)下Nginx+MySQL+PHP這種網站服務器架構。Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Mysql是一個小型關系型數據庫管理系統(tǒng)。PHP是一種在服務器端執(zhí)行的嵌入HTML文檔的腳本語言。這四種軟件均為免費開源軟件,組合到一起,成為一個免費、高效、擴展性強的網站服務系統(tǒng)。

LNMP安裝教程:

一,安裝nginx

*nginx的官方網站:*

*http://nginx.org/en/download.html*

*Mainline version* *主線版本*

*Stable version* *穩(wěn)定版本*

*Legacy versions* *遺產版本 /歷史版本*

1.下載

安裝前確認安裝擴展 沒有的直接 yum install wget gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel

 [root@localhost ~]# cd /usr/local/src/
 
 [root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

2.解壓

  [root@localhost src]# tar zxvf nginx-1.12.2.tar.gz

/** 取消Debug編譯模式  START***/

cd nginx-1.12.2

vi auto/cc/gcc  #將這句注釋掉 取消Debug編譯模式 大概在172行 #CFLAGS=”$CFLAGS -g”

Linux系統(tǒng)如何配置LNMP /**取消Debug編譯模式  END******/

\3. 預編譯

  cd nginx-1.12.2
 
 
 
 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre --with-http_gzip_static_module --with-http_dav_module   --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

解釋

–with-http_gzip_static_module :支持壓縮

–with-http_stub_status_module :支持nginx狀態(tài)查詢

–with-http_ssl_module :支持https

–with-pcre :為了支持rewrite重寫功能,必須制定pcre

–with-http_dav_module        #啟用支持(增加PUT,DELETE,MKCOL:創(chuàng)建集合,COPY和MOVE方法)             –with-http_addition_module      #啟用支持(作為一個輸出過濾器,支持不完全緩沖,分部分相應請求) –with-http_sub_module        #啟用支持(允許一些其他文本替換Nginx相應中的一些文本) –with-http_flv_module        #啟用支持(提供支持flv視頻文件支持) –with-http_mp4_module        #啟用支持(提供支持mp4視頻文件支持,提供偽流媒體服務端支持)

*make -j 4 && make install 4核編譯*

\4. [root@localhost src]# make && make install

5.添加系統(tǒng)變量(方便啟停服務)

[root@localhost nginx-1.12.2]# vim /etc/profile

我一般是在56行添加   export PATH=/usr/local/nginx/sbin:$PATH

Linux系統(tǒng)如何配置LNMP

重啟配置 source /etc/profile

[root@localhost nginx-1.12.2]# nginx -V

Linux系統(tǒng)如何配置LNMP*添加軟連 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/*

*生成服務啟動腳本*

*vim /etc/init.d/nginx*

 #!/bin/bash
 
 # chkconfig: - 99 2
 
 # description: Nginx Service Control Script
 
 PROG="/usr/local/nginx/sbin/nginx"
 
 PIDF="/usr/local/nginx/logs/nginx.pid"
 
 case "$1" in
 
        start)
 
        $PROG
 
        ;;
 
        stop)
 
        kill -3 $(cat $PIDF)
 
        ;;
 
        restart)
 
        $0 stop &> /dev/null
 
        if [ $? -ne 0 ] ; then continue ; fi
 
        $0 start
 
        ;;
 
        reload)
 
        kill -1 $(cat $PIDF)
 
        ;;
 
        *)
 
        echo "Userage: $0 { start | stop | restart | reload }"
 
        exit 1
 
 esac
 
 exit 0

配置服務開機自動啟動 [root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# chkconfig –add nginx [root@localhost ~]# chkconfig nginx on

首次啟動  /usr/local/nginx/sbin/nginx

Linux系統(tǒng)如何配置LNMP

二、安裝mysql 5.7

用的是rpm 好處是不用配置那么多東西 。 配置不用管。

 [root@localhost ~]# cd /usr/local/src/  [root@localhost src]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

 [root@localhost src]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm

 [root@localhost src]# yum -y install mysql-server

(**也可以指定安裝目錄**   yum –installroot=/usr/local/mysql –releasever=/ -y install mysql-server 可以自己研究**)**

根據步驟安裝就可以了,

默認配置文件路徑: 配置文件:/etc/my.cnf 日志文件:/var/log/var/log/mysqld.log 服務啟動腳本:/usr/lib/systemd/system/mysqld.service

socket文件:/var/run/mysqld/mysqld.pid

啟動mysql服務

service mysqld restart

重置密碼

[root@localhost ~]# grep “password” /var/log/mysqld.log

Linux系統(tǒng)如何配置LNMP

可以看到  輸入 mysql -u root -p  密碼 進入    第一次登陸 ,需要重置密碼 要不什么也不能操作

接下來重置密碼:5.7.20 為了安全密碼      必須包含 數字字母符號

alter user ‘root’@’localhost’ identified by ‘Root!!2018’;

也可以 直接再添加新用戶

grant all on . to ‘rootadmin’@’%’ identified by ‘Root@@’  with grant option;

增加root用戶指定可以任意IP登錄,如果想限制只能讓指定IP登錄請把%替換成IP地址

最后記得刷新權限;

flush privileges ;

三、安裝php

需要的插件 包

 yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel epel-release libmcrypt-devel autoconf

1.下載

  [root@localhost ~]# cd /usr/local/src/

  [root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.32.tar.gz

2.解壓

  [root@localhost src]# tar zxvf php-5.6.32.tar.gz

\3. 預編譯

 進入目錄 [root@localhost src]``# cd php-5.6.32

創(chuàng)建php-fpm用戶,并禁止登錄; [root@localhost php-5.6.32]# useradd -s /sbin/nologin php-fpm

 ./configure --prefix=/usr/local/php --sysconfdir=/usr/local/php/etc --with-config-file-path=/usr/local/php/etc/   --with-fpm-user=php-fpm --with-fpm-group=php-fpm --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mhash --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib --enable-mbstring --with-mcrypt --enable-sockets --with-iconv-dir   --enable-zip --with-pcre-dir --with-pear --enable-session --enable-gd-native-ttf --enable-xml --with-freetype-dir --enable-gd-jis-conv --enable-inline-optimization --enable-shared --enable-soap --enable-bcmath --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-xmlrpc --with-gettext --enable-exif --with-readline   --enable-ftp   --enable-redis

提示錯誤mcrypt.h沒有找到,安裝libmcrypt-devel包,默認的yum源,沒有這個包,需要安裝epel擴展源后,才可以安裝。

[root@localhost php-5.6.32]# yum install -y epel-release

[root@localhost php-5.6.32]# yum install -y libmcrypt

[root@localhost php-5.6.32]# yum install -y libmcrypt-devel

再次執(zhí)行./configure,沒有錯誤提示,出現(xiàn)Thank you for using PHP,配置OK。

Linux系統(tǒng)如何配置LNMP

完成后使用echo $?查看是否安裝正確; [root@localhost  php-5.6.32]# make && make install

[root@localhost  php-5.6.32]# echo $?

0 0表示上一步的結果成功。

配置文件

需要將當前目錄下的php.ini文件拷貝到 php的安裝目錄etc下

[root@localhost php-5.6.32]# cp php.ini-production /usr/local/php/etc/php.ini

php.ini 文件是在包目錄下的 php.ini-development(開發(fā)), php.ini-production(生產)

拷貝php啟動腳本,php-fpm配置文件,更改php-fpm權限為755;添加php-fpm開機啟動;

 [root@ php-5.6.32]# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm   /etc/init.d/php-fpm
 
 (啟動腳本)
 
 [root@ php-5.6.32]# mv /usr/local/php/etc/php-fpm.conf.default   /usr/local/php/etc/php-fpm.conf (就是去掉了末尾的.default )
 
 [root@ php-5.6.32]# chmod 755 /etc/init.d/php-fpm
 
 [root@lphp-5.6.32]# chkconfig --add php-fpm
 
 [root@lphp-5.6.32]# service php-fpm start
 
 Starting php-fpm done
 
 [root@php-5.6.32]# chkconfig php-fpm on

將php的安裝目錄也加入到系統(tǒng)的環(huán)境變量  在最后一行加入

vim /etc/profile

export PATH=/usr/local/php/bin:$PATH

source /etc/profile 重新加載

[root@localhost ~]# php -v PHP 5.6.32 (cli) (built: Mar 12 2018 17:43:15) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies完成 接下來就是測試

—–php—安裝成功

三、測試  在地址欄輸入你的ip。然后測試PHP安裝是否成功。確保nginx 和PHP都是運行的。

1.寫測試頁面 Linux系統(tǒng)如何配置LNMP進入nginx的html  cd /usr/local/nginx/html/

編輯  vim index.php

 

\2. 配置nginx

核心配置的兩個 加入到nginx.conf

vim /usr/local/nginx/conf/nginx.conf

找到 location  添加  index.php

Linux系統(tǒng)如何配置LNMP

將請求轉給php的9000端口  確保nginx 和PHP都是運行的哈。

location ~ .php$ {

     root      html;      fastcgi_pass  127.0.0.1:9000;      fastcgi_index  index.php;      fastcgi_param  SCRIPT_FILENAME  fastcgi_script_name;      include     fastcgi_params;    }

安裝上面的應該沒問題,有問題的留言,大家一塊解決。

******************************

參考

這個是添加pathinfo的 除了首頁能訪問別的頁面都是404的問題

    try_files uri/ /index.php?request_filename){               rewrite ^(.*)1 last;  break;           }

這是 server{} 里面的完整配置

Linux系統(tǒng)如何配置LNMP

 server {
        listen       80;
        server_name localhost;
        root   /www/yiqi/public/;
 
 
        location / {
            root   /www/yiqi/public/;
            index index.php index.html index.htm;
                        try_files $uri $uri/ /index.php?$query_string;
                                  if (!-e $request_filename) {
                                    rewrite ^(.*)$ /index.php?s=$1 last; break;
                                  }
 
        }
 
 
 
          location ~ .*\.(php|php5)?$ {
            root   /www/yiqi/public/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include       fastcgi_params;
            fastcgi_connect_timeout 75;
 fastcgi_read_timeout 600;
 fastcgi_send_timeout 600;
        }
 
          error_page 404 /404.html;
          #access_log logs/80.access.log main;
          #error_log   logs/80.error.log info;
 
        }

關于“Linux系統(tǒng)如何配置LNMP”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI