LNMP是linux、nginx、mysql、php的簡寫;LNMP與LAMP環境同樣也是用來作web網站後臺的,nginx是輕量級的,進程間的通信使用php-fpm獨立使用;apache則是比較臃腫的,調用php的模塊來完成的,須要加載不少模塊,運行起來相對較慢。php
1、安裝MySQLhtml
咱們平時安裝MySQL都是源碼包安裝的,可是因爲它的編譯須要很長的時間,咱們這裏選擇安裝二進制免編譯包。你能夠到MySQL官方網站去下載 http://dev.mysql.com/downloads/ 具體版本根據你的平臺和需求而定,目前比較經常使用的爲mysql-5.0/mysql-5.1, 5.5版本雖然已經發布有段日子了,可是貌似用在線上跑服務的仍是少數。
mysql
具體的步驟以下:linux
一、下載mysql安裝文件到/usr/local/src 目錄下nginx
[root@yong ~]# cd /usr/local/src
[root@yong src]# wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz --2015-04-27 09:23:01-- http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.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/octet-stream]Saving to: 「mysql-5.1.40-linux-i686-icc-glibc23.tar.gz」
二、解壓下載的文件web
[root@yong src]# tar -zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
三、把解壓完的數據移動到/usr/local/mysqlsql
提醒注意:不用本身手動添加/mysql目錄,移動的同時會把原目錄更名的。數據庫
[root@yong src]# mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql
四、創建mysql用戶,禁止登陸系統;apache
[root@yong src]# useradd -s /sbin/nologin mysql
五、初始化數據庫,新建/data目錄及/data/mysql子目錄,並設置/data/mysql目錄的所屬主所屬組爲mysql用戶;
vim
[root@yong src]# cd /usr/local/mysql/ [root@yong mysql]# mkdir -p /data/mysql [root@yong mysql]# chown -R mysql:mysql /data/mysql/ [root@yong mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ Installing MySQL system tables... OK Filling help tables... OK
--user定義數據庫的所屬主,--datadir 定義數據庫的安裝路徑,建議放到大存儲空間。看到兩個"OK"說明執行正確,不然請仔細查看錯誤信息,查詢哪裏出了問題;
六、拷貝配置文件
[root@yong mysql]# cp support-files/my-large.cnf /etc/my.cnf
不少模板配置文件在/support-files/目錄下;
根據內存大小選擇:
my-small.cnf (內存 <= 64M)
my-medium.cnf (內存 128M )
my-large.cnf (內存 512M)
my-huge.cnf (內存 1G-2G)
my-innodb-heavy-4G.cnf (內存 4GB)
七、拷貝啓動腳本文件並修改屬性
[root@yong mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@yong mysql]# chmod 755 /etc/init.d/mysqld
八、修改啓動腳本
[root@yong mysql]# vim /etc/init.d/mysqld
須要修改的地方有"basedir=/usr/local/mysql"(mysql的安裝目錄)
」datadir=/data/mysql「 (前面初始化數據庫定義的目錄)
九、把啓動腳本加入系統服務項,並設定開機啓動,啓動mysql服務;
[root@yong mysql]# chkconfig --add mysqld [root@yong mysql]# chkconfig mysqld on [root@yong mysql]# service mysqld start Starting MySQL.. [ OK ]
十、查看mysqld是否啓動
[root@localhost mysql]# ps aux |grep mysqld root 1341 0.0 0.1 6680 1328 pts/0 S 20:22 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid mysql 1456 0.3 4.2 381608 43504 pts/0 Sl 20:22 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
2、安裝php
一、下載php5.4.37壓縮包到/usr/local/src目錄下;解壓縮包,而後進入解壓以後的目錄;
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2 [root@localhost src]# tar -jxvf php-5.4.37.tar.bz2 [root@localhost src]# cd php-5.4.37
二、建立php-fpm用戶,並禁止登陸;
[root@localhost php-5.4.37]# useradd -s /sbin/nologin php-fpm
三、配置php的各項參數;
[root@localhost php-5.4.37]#./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
配置遇到的各項錯誤,根據錯誤提示進行安裝所須要的包;
提示錯誤,沒有可接受的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
[root@localhost php-5.4.37]# yum install -y gcc
再次執行./configure,提示錯誤xml2-config沒有找到,安裝libxml2-devel包;
configure: error: xml2-config not found. Please check your libxml2 installation
[root@localhost php-5.4.37]# yum install -y libxml2-devel
再次執行./configure,提示錯誤須要從新安裝libcurl,安裝libcurl-devel包;
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
[root@localhost php-5.4.37]# yum install -y libcurl-devel
再次執行./configure,提示錯誤jpeglib.h沒有找到,安裝libjpeg-devel包;
configure: error: jpeglib.h not found.
[root@localhost php-5.4.37]# yum install -y libjpeg-devel
再次執行./configure,提示錯誤png.h沒有找到,安裝libpng-devel包;
configure: error: png.h not found.
[root@localhost php-5.4.37]# yum install -y libpng-devel
再次執行./configure,提示錯誤freetype-config沒有找到,安裝freetype-devel包;
configure: error: freetype-config not found.
[root@localhost php-5.4.37]# yum install -y freetype-devel
再次執行./configure,提示錯誤mcrypt.h沒有找到,安裝libmcrypt-devel包,默認的yum源,沒有這個包,須要安裝epel擴展源後,才能夠安裝。
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
[root@localhost php-5.4.37]# yum install -y epel-release [root@localhost php-5.4.37]# yum install -y libmcrypt-devel
再次執行./configure,沒有錯誤提示,出現Thank you for using PHP,配置OK。
四、安裝php,安裝完成後使用echo $?查看是否安裝正確;
[root@localhost php-5.4.37]# make && make install [root@localhost php-5.4.37]# echo $? 0
五、拷貝php配置文件
[root@localhost php-5.4.37]# cp php.ini-production /usr/local/php/etc/php.ini
六、拷貝php啓動腳本,php-fpm配置文件,更改php-fpm權限爲755;添加php-fpm開機啓動;
[root@localhost php-5.4.37]# cp /usr/local/src/php-5.4.37/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost php-5.4.37]# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
另外一種更名方法,使用花括號{},同一個目錄下;-v 爲可視化顯示;
mv -v /usr/local/php/etc/{php-fpm.conf.default,php-fpm.conf}
七、更改php-fpm的權限爲755;添加php-fpm到系統啓動項,並設置開機啓動;啓動php-fpm;
[root@localhost php-5.4.37]# chmod 755 /etc/init.d/php-fpm [root@localhost php-5.4.37]# chkconfig --add php-fpm [root@localhost php-5.4.37]# service php-fpm start Starting php-fpm done [root@localhost php-5.4.37]# chkconfig php-fpm on
3、安裝nginx
一、下載nginx1.6.2壓縮包到/usr/local/src目錄下,解壓縮包,以後進入解壓縮後的目錄;
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz [root@localhost src]# tar zxvf nginx-1.6.2.tar.gz [root@localhost src]# cd nginx-1.6.2
二、配置nginx
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-pcre
提示錯誤,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=<path> option.
[root@localhost nginx-1.6.2]# yum install -y pcre-devel
再次執行./configure,沒有錯誤;
三、安裝nginx,安裝完成後echo $?查看是否安裝正確;
[root@localhost nginx-1.6.2]# make && make install [root@localhost nginx-1.6.2]# echo $? 0
四、啓動nginx,ps查看nginx的進程,nginx的用戶爲nobody;
[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx [root@localhost ~]# ps aux |grep nginx root 986 0.0 0.0 3552 528 ? Ss 08:56 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 988 0.0 0.0 3752 884 ? S 08:56 0:00 nginx: worker process
編寫nginx啓動腳本,加入##號內的內容;
[root@localhost nginx-1.6.2]# vi /etc/init.d/nginx
###########################################################################
#!/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
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
###########################################################################
五、設置nginx權限755,加入開機啓動;
[root@localhost nginx-1.6.2]# chmod a+x /etc/init.d/nginx [root@localhost nginx-1.6.2]# ls -l /etc/init.d/nginx -rwxr-xr-x. 1 root root 1211 5月 11 17:37 /etc/init.d/nginx [root@localhost nginx-1.6.2]# chkconfig --add nginx [root@localhost nginx-1.6.2]# chkconfig nginx on
4、配置解析php
編輯nginx配置文件,找到下面的代碼,刪除前面的#號,更改 fastcgi_param這一行,加入nginx存放路徑;
[root@localhost nginx-1.6.2]# vi /usr/local/nginx/conf/nginx location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; }
保存退出,從新加載nginx
[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload
在nginx的目錄下新建一個phpinfo.php文件,進行測試;
[root@localhost nginx-1.6.2]# vi /usr/local/nginx/html/phpinfo.php <?php phpinfo(); ?>
[root@localhost nginx-1.6.2]# curl localhost/phpinfo.php -I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Mon, 11 May 2015 09:49:36 GMT Content-Type: text/html Connection: keep-alive X-Powered-By: PHP/5.4.37
在瀏覽器輸入ip地址也能夠進行測試,顯示welcome to nginx!phpinfo.php也正常解析,說明環境已經搭建OK;