天天看點

linux下LNMP環境搭建

LNMP是linux、nginx、mysql、php的簡寫;LNMP與LAMP環境一樣也是用來做web網站背景的,nginx是輕量級的,程序間的通訊使用php-fpm獨立使用;apache則是比較臃腫的,調用php的子產品來完成的,需要加載很多子產品,運作起來相對較慢。

一、安裝MySQL

我們平時安裝MySQL都是源碼包安裝的,但是由于它的編譯需要很長的時間,我們這裡選擇安裝二進制免編譯包。你可以到MySQL官方網站去下載下傳 http://dev.mysql.com/downloads/ 具體版本根據你的平台和需求而定,目前比較常用的為mysql-5.0/mysql-5.1, 5.5版本雖然已經釋出有段日子了,但是貌似用線上上跑服務的還是少數。

具體的步驟如下:

1、下載下傳mysql安裝檔案到/usr/local/src 目錄下

1

<code>[root@yong ~]</code><code># cd /usr/local/src</code>

2

<code>[root@yong src]</code><code># wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz</code>

<code>--2015-04-27 09:23:01--  http:</code><code>//syslab</code><code>.comsenz.com</code><code>/downloads/linux/mysql-5</code><code>.1.40-linux-i686-icc-glibc23.</code><code>tar</code><code>.gzResolving syslab.comsenz.com... 101.227.130.115Connecting to syslab.comsenz.com|101.227.130.115|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 123633020 (118M) [application</code><code>/octet-stream</code><code>]Saving to: “mysql-5.1.40-linux-i686-icc-glibc23.</code><code>tar</code><code>.gz”</code>

2、解壓下載下傳的檔案

<code>[root@yong src]</code><code># tar -zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz</code>

3、把解壓完的資料移動到/usr/local/mysql

提醒注意:不用自己手動添加/mysql目錄,移動的同時會把原目錄改名的。

<code>[root@yong src]</code><code># mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql</code>

4、建立mysql使用者,禁止登入系統;

<code>[root@yong src]</code><code># useradd -s /sbin/nologin mysql</code>

5、初始化資料庫,建立/data目錄及/data/mysql子目錄,并設定/data/mysql目錄的所屬主所屬組為mysql使用者;

3

4

5

6

7

8

<code>[root@yong src]</code><code># cd /usr/local/mysql/</code>

<code>[root@yong mysql]</code><code># mkdir -p /data/mysql</code>

<code>[root@yong mysql]</code><code># chown -R mysql:mysql /data/mysql/</code>

<code>[root@yong mysql]</code><code># ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/</code>

<code>Installing MySQL system tables...</code>

<code>OK</code>

<code>Filling help tables...</code>

--user定義資料庫的所屬主,--datadir 定義資料庫的安裝路徑,建議放到大存儲空間。看到兩個"OK"說明執行正确,否則請仔細檢視錯誤資訊,查詢哪裡出了問題;

6、拷貝配置檔案

<code>[root@yong mysql]</code><code># cp support-files/my-large.cnf /etc/my.cnf</code>

很多模闆配置檔案在/support-files/目錄下;

根據記憶體大小選擇: 

my-small.cnf (記憶體 &lt;= 64M)

my-medium.cnf (記憶體 128M )

my-large.cnf (記憶體 512M)

my-huge.cnf (記憶體 1G-2G)

my-innodb-heavy-4G.cnf (記憶體 4GB)

7、拷貝啟動腳本檔案并修改屬性

<code>[root@yong mysql]</code><code># cp support-files/mysql.server /etc/init.d/mysqld </code>

<code>[root@yong mysql]</code><code># chmod 755 /etc/init.d/mysqld</code>

8、修改啟動腳本

<code>[root@yong mysql]</code><code># vim /etc/init.d/mysqld</code>

需要修改的地方有"basedir=/usr/local/mysql"(mysql的安裝目錄)

”datadir=/data/mysql“ (前面初始化資料庫定義的目錄)

9、把啟動腳本加入系統服務項,并設定開機啟動,啟動mysql服務;

<code>[root@yong mysql]</code><code># chkconfig --add mysqld</code>

<code>[root@yong mysql]</code><code># chkconfig mysqld on</code>

<code>[root@yong mysql]</code><code># service mysqld start</code>

<code>Starting MySQL..                                           [  OK  ]</code>

10、檢視mysqld是否啟動

<code>[root@localhost mysql]</code><code># ps aux |grep mysqld</code>

<code>root      1341  0.0  0.1   6680  1328 pts</code><code>/0</code>    <code>S    20:22   0:00 </code><code>/bin/sh</code> <code>/usr/local/mysql/bin/mysqld_safe</code> <code>--datadir=</code><code>/data/mysql</code> <code>--pid-</code><code>file</code><code>=</code><code>/data/mysql/localhost</code><code>.localdomain.pid</code>

<code>mysql     1456  0.3  4.2 381608 43504 pts</code><code>/0</code>    <code>Sl   20:22   0:01 </code><code>/usr/local/mysql/bin/mysqld</code> <code>--basedir=</code><code>/usr/local/mysql</code> <code>--datadir=</code><code>/data/mysql</code> <code>--user=mysql --log-error=</code><code>/data/mysql/localhost</code><code>.localdomain.err --pid-</code><code>file</code><code>=</code><code>/data/mysql/localhost</code><code>.localdomain.pid --socket=</code><code>/tmp/mysql</code><code>.sock --port=3306</code>

二、安裝php

1、下載下傳php5.4.37壓縮包到/usr/local/src目錄下;解壓縮包,然後進入解壓之後的目錄;

<code>[root@localhost src]</code><code># wget  http://cn2.php.net/distributions/php-5.4.37.tar.bz2</code>

<code>[root@localhost src]</code><code># tar -jxvf php-5.4.37.tar.bz2 </code>

<code>[root@localhost src]</code><code># cd php-5.4.37</code>

2、建立php-fpm使用者,并禁止登入;

<code>[root@localhost php-5.4.37]</code><code># useradd -s /sbin/nologin php-fpm</code>

3、配置php的各項參數;

<code>[root@localhost php-5.4.37]</code><code>#./configure --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --enable-fpm   --with-fpm-user=php-fpm  --with-fpm-group=php-fpm   --with-mysql=/usr/local/mysql  --with-mysql-sock=/tmp/mysql.sock  --with-libxml-dir  --with-gd   --with-jpeg-dir   --with-png-dir   --with-freetype-dir  --with-iconv-dir   --with-zlib-dir   --with-mcrypt   --enable-soap   --enable-gd-native-ttf   --enable-ftp  --enable-mbstring  --enable-exif    --disable-ipv6     --with-curl</code>

配置遇到的各項錯誤,根據錯誤提示進行安裝所需要的包;

提示錯誤,沒有可接受的C編譯器,安裝gcc包;

checking for cc... no

checking for gcc... no

configure: error: in `/usr/local/src/php-5.4.37':

configure: error: no acceptable C compiler found in $PATH

<code>[root@localhost php-5.4.37]</code><code># yum install -y gcc</code>

再次執行./configure,提示錯誤xml2-config沒有找到,安裝libxml2-devel包;

configure: error: xml2-config not found. Please check your libxml2 installation

<code>[root@localhost php-5.4.37]</code><code># yum install -y libxml2-devel</code>

再次執行./configure,提示錯誤需要重新安裝libcurl,安裝libcurl-devel包;

checking for cURL in default path... not found

configure: error: Please reinstall the libcurl distribution -

    easy.h should be in &lt;curl-dir&gt;/include/curl/

<code>[root@localhost php-5.4.37]</code><code># yum install -y libcurl-devel</code>

再次執行./configure,提示錯誤jpeglib.h沒有找到,安裝libjpeg-devel包;

configure: error: jpeglib.h not found.

<code>[root@localhost php-5.4.37]</code><code># yum install -y libjpeg-devel</code>

再次執行./configure,提示錯誤png.h沒有找到,安裝libpng-devel包;

configure: error: png.h not found.

<code>[root@localhost php-5.4.37]</code><code># yum install -y libpng-devel</code>

再次執行./configure,提示錯誤freetype-config沒有找到,安裝freetype-devel包;

configure: error: freetype-config not found.

<code>[root@localhost php-5.4.37]</code><code># yum install -y freetype-devel</code>

再次執行./configure,提示錯誤mcrypt.h沒有找到,安裝libmcrypt-devel包,預設的yum源,沒有這個包,需要安裝epel擴充源後,才可以安裝。

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

<code>[root@localhost php-5.4.37]</code><code># yum install -y epel-release</code>

<code>[root@localhost php-5.4.37]</code><code># yum install -y libmcrypt-devel</code>

再次執行./configure,沒有錯誤提示,出現Thank you for using PHP,配置OK。

<code>[root@localhost php-5.4.37]</code><code># make &amp;&amp; make install </code>

<code>[root@localhost php-5.4.37]</code><code># echo $?</code>

<code>0</code>

5、拷貝php配置檔案

<code>[root@localhost php-5.4.37]</code><code># cp php.ini-production /usr/local/php/etc/php.ini</code>

6、拷貝php啟動腳本,php-fpm配置檔案,更改php-fpm權限為755;添加php-fpm開機啟動;

<code>[root@localhost php-5.4.37]</code><code># cp /usr/local/src/php-5.4.37/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm</code>

<code>[root@localhost php-5.4.37]</code><code># mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf</code>

另一種改名方法,使用花括号{},同一個目錄下;-v 為可視化顯示;

mv -v /usr/local/php/etc/{php-fpm.conf.default,php-fpm.conf}

7、更改php-fpm的權限為755;添加php-fpm到系統啟動項,并設定開機啟動;啟動php-fpm;

<code>[root@localhost php-5.4.37]</code><code># chmod 755 /etc/init.d/php-fpm </code>

<code>[root@localhost php-5.4.37]</code><code># chkconfig --add php-fpm</code>

<code>[root@localhost php-5.4.37]</code><code># service php-fpm start</code>

<code>Starting php-fpm  </code><code>done</code>

<code>[root@localhost php-5.4.37]</code><code># chkconfig php-fpm on</code>

三、安裝nginx

1、下載下傳nginx1.6.2壓縮包到/usr/local/src目錄下,解壓縮包,之後進入解壓縮後的目錄;

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

<code>[root@localhost src]</code><code># wget http://nginx.org/download/nginx-1.6.2.tar.gz</code>

<code>[root@localhost src]</code><code># tar zxvf nginx-1.6.2.tar.gz </code>

<code>[root@localhost src]</code><code># cd nginx-1.6.2</code>

2、配置nginx

<code>[root@localhost nginx-1.6.2]</code><code># ./configure   --prefix=/usr/local/nginx   --with-pcre</code>

提示錯誤,HTTP重寫子產品需要PCRE庫,需要安裝pcre-devel包;

checking for PCRE library ... not found

checking for PCRE library in /usr/local/ ... not found

checking for PCRE library in /usr/include/pcre/ ... not found

checking for PCRE library in /usr/pkg/ ... not found

checking for PCRE library in /opt/local/ ... not found

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

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre=&lt;path&gt; option.

<code>[root@localhost nginx-1.6.2]</code><code># yum install -y pcre-devel</code>

再次執行./configure,沒有錯誤;

3、安裝nginx,安裝完成後echo $?檢視是否安裝正确;

<code>[root@localhost nginx-1.6.2]</code><code># make &amp;&amp; make install</code>

<code>[root@localhost nginx-1.6.2]</code><code># echo $?</code>

4、啟動nginx,ps檢視nginx的程序,nginx的使用者為nobody;

<code>[root@localhost nginx-1.6.2]</code><code># /usr/local/nginx/sbin/nginx </code>

<code>[root@localhost ~]</code><code># ps aux |grep nginx</code>

<code>root       986  0.0  0.0   3552   528 ?        Ss   08:56   0:00 nginx: master process </code><code>/usr/local/nginx/sbin/nginx</code> <code>-c </code><code>/usr/local/nginx/conf/nginx</code><code>.conf</code>

<code>nobody     988  0.0  0.0   3752   884 ?        S    08:56   0:00 nginx: worker process</code>

編寫nginx啟動腳本,加入##号内的内容;

<code>[root@localhost nginx-1.6.2]</code><code># vi /etc/init.d/nginx</code>

###########################################################################

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"

start() {

        echo -n $"Starting $prog: "

        mkdir -p /dev/shm/nginx_temp

        daemon $NGINX_SBIN -c $NGINX_CONF

        RETVAL=$?

        echo

        return $RETVAL

}

stop() {

        echo -n $"Stopping $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -TERM

        rm -rf /dev/shm/nginx_temp

reload(){

        echo -n $"Reloading $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -HUP

restart(){

        stop

        start

configtest(){

    $NGINX_SBIN -c $NGINX_CONF -t

    return 0

case "$1" in

  start)

        ;;

  stop)

  reload)

        reload

  restart)

        restart

  configtest)

        configtest

  *)

        echo $"Usage: $0 {start|stop|reload|restart|configtest}"

        RETVAL=1

esac

exit $RETVAL

5、設定nginx權限755,加入開機啟動;

<code>[root@localhost nginx-1.6.2]</code><code># chmod a+x /etc/init.d/nginx </code>

<code>[root@localhost nginx-1.6.2]</code><code># ls -l /etc/init.d/nginx </code>

<code>-rwxr-xr-x. 1 root root 1211 5月  11 17:37 </code><code>/etc/init</code><code>.d</code><code>/nginx</code>

<code>[root@localhost nginx-1.6.2]</code><code># chkconfig --add nginx</code>

<code>[root@localhost nginx-1.6.2]</code><code># chkconfig nginx on</code>

四、配置解析php

編輯nginx配置檔案,找到下面的代碼,删除前面的#号,更改 fastcgi_param這一行,加入nginx存放路徑;

<a href="http://s3.51cto.com/wyfs02/M00/6C/C0/wKioL1VRnqWBDMIlAAEFHxnhyw8501.jpg" target="_blank"></a>

<code>[root@localhost nginx-1.6.2]</code><code># vi /usr/local/nginx/conf/nginx</code>

<code>        </code><code>location ~ \.php$ {</code>

<code>            </code><code>root           html;</code>

<code>            </code><code>fastcgi_pass   127.0.0.1:9000;</code>

<code>            </code><code>fastcgi_index  index.php;</code>

<code>            </code><code>fastcgi_param  SCRIPT_FILENAME </code><code>/usr/local/nginx/html</code><code>$fastcgi_script_name;</code>

<code>            </code><code>include        fastcgi_params;</code>

<code>        </code><code>}</code>

儲存退出,重新加載nginx

<code>[root@localhost nginx-1.6.2]</code><code># /usr/local/nginx/sbin/nginx -s reload</code>

在nginx的目錄下建立一個phpinfo.php檔案,進行測試;

<code>[root@localhost nginx-1.6.2]</code><code># vi /usr/local/nginx/html/phpinfo.php</code>

<code>&lt;?php</code>

<code>        </code><code>phpinfo();</code>

<code>?&gt;</code>

<code>[root@localhost nginx-1.6.2]</code><code># curl localhost/phpinfo.php -I</code>

<code>HTTP</code><code>/1</code><code>.1 200 OK</code>

<code>Server: </code>

<code>nginx</code><code>/1</code><code>.6.2</code>

<code>Date: Mon, 11 May 2015 09:49:36 GMT</code>

<code>Content-Type: text</code><code>/html</code>

<code>Connection: keep-alive</code>

<code>X-Powered-By: PHP</code><code>/5</code><code>.4.37</code>

在浏覽器輸入ip位址也可以進行測試,顯示welcome to nginx!phpinfo.php也正常解析,說明環境已經搭建OK;

<a href="http://s3.51cto.com/wyfs02/M02/6C/C0/wKioL1VRnuGDmI-WAAHF4KbE-AI829.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/6C/C0/wKioL1VRnunBmKx7AALjPhDXbW8247.jpg" target="_blank"></a>

本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1650624,如需轉載請自行聯系原作者