LNMP環境搭建
前一篇文章已經介紹了LAMP環境搭建,而LNMP環境的配置如LAMP相類似。
一、安裝 MySQL
平時安裝MySQL都是原始碼包安裝,但是由於它的編譯需要很長的時間,所以安裝二進位制免編譯包。
MySQL官方網站下載:http://dev.mysql.com/downloads/
sohu映象站下載:http://mirrors.sohu.com/
1、下載 mysql 包到 /usr/local/src/
[[email protected]~]#cd/usr/local/src/
[[email protected]src]#wgethttp://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-i686-glibc23.tar.gz
2、解壓
[[email protected]src]#tar-zxvf/usr/local/src/mysql-5.1.73-linux-i686-glibc23.tar.gz
3、把解壓後的資料移動到 /usr/local/mysql
[[email protected]src]#mvmysql-5.1.73-linux-i686-glibc23/usr/local/mysql
4、建立 MySQL 使用者
[[email protected]src]#useradd-s/sbin/nologinmysql
5、初始化資料庫
[[email protected]src]#cd/usr/local/mysql
[[email protected]mysql]#mkdir-p/data/mysql//建立datadir,資料庫檔案會放在這裡
[[email protected]mysql]#chown-Rmysql/data/mysql//更改許可權
[[email protected]mysql]#./scripts/mysql_install_db--user=mysql--datadir=/data/mysql//--user定義資料庫所屬主,--datadir定義資料庫安裝目錄
注意:若報如下錯
./scripts/mysql_install_db: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directoryNeither host 'tpp' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option
這是因為64位系統安裝了32位的程式引起的,執行下面安裝命令:
[[email protected] mysql]# yum install libgcc.i686 --setopt=protected_multilib=false
6、拷貝配置檔案
[[email protected]mysql]#cpsupport-files/my-large.cnf/etc/my.cnf
7、拷貝啟動指令碼並修改其屬性
[[email protected]mysql]#cpsupport-files/mysql.server/etc/init.d/mysqld
[[email protected]mysql]#chmod755/etc/init.d/mysqld
8、修改啟動指令碼
[[email protected]mysql]#vim/etc/init.d/mysqld//進入後修改以下兩個地方
basedir=/usr/local/mysql
datadir=/data/mysql
9、把啟動指令碼加入系統服務項,並設定開機啟動,啟動mysql
[[email protected]mysql]#chkconfig--addmysqld
[[email protected]mysql]#chkconfigmysqldon
[[email protected]mysql]#servicemysqldstart
若不能啟動,可以到 /data/mysql/ 目錄下檢視錯誤日誌,日誌通常是主機名.err。檢查MySQL是否啟動的命令為:ps aux |gerp mysqld 。
二、安裝 PHP
PHP官方下載地址:http://www.php.net/downloads.php
sohu映象站下載地址:http://mirrors.sohu.com/php/php-5.3.27.tar.gz
1、下載 php 包到 /usr/local/src/
[[email protected]httpd-2.2.31]#cd/usr/local/src/
[[email protected]src]#wgethttp://mirrors.sohu.com/php/php-5.3.27.tar.gz
2、解壓
[[email protected]src]#tar-zxvfphp-5.3.27.tar.gz
3、為了防止後面編譯後不能正常解析PHP,故先安裝需要的軟體或者庫檔案(親試99%都會成功)
[[email protected]src]#yuminstall-ygccgcc-c++makezlibzlib-develpcrepcre-devellibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibxml2libxml2-develglibcglibc-develglib2glib2-develbzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-developensslopenssl-developenldapopenldap-develnss_ldapopenldap-clientsopenldap-serverslibtoollibtool-ltdl-devel [[email protected]src]#rpm-ivhhttp://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [[email protected]src]#yuminstall-ylibmcrypt-devel
4、建立相關賬戶
[[email protected]src]#useradd-s/sbin/nologinphp-fpm
5、配置編譯引數
[[email protected]src]#cdphp-5.3.27
[[email protected]php-5.3.27]#./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\
--enable-zend-multibyte\
--disable-ipv6\
--with-pear\
--with-curl\
--with-openssl
6、編譯
[[email protected]php-5.3.27]#make
7、安裝
[[email protected]php-5.3.27]#makeinstall
8、修改配置檔案
[[email protected]php-5.3.27]#cpphp.ini-production/usr/local/php/etc/php.ini
[[email protected]php-5.3.27]#vim/usr/local/php/etc/php-fpm.conf
[global]
pid=/usr/local/php/var/run/php-fpm.pid
error_log=/usr/local/php/var/log/php-fpm.log
[www]
listen=/tmp/php-fcgi.sock
user=php-fpm
group=php-fpm
pm=dynamic
pm.max_children=50
pm.start_servers=20
pm.min_spare_servers=5
pm.max_spare_servers=35
pm.max_requests=500
rlimit_files=1024
儲存配置檔案後,檢驗配置是否正確:
[[email protected]php-5.3.27]#/usr/local/php/sbin/php-fpm-t
如果出現諸如 “test is successful” 字樣,說明配置沒有問題。
9、啟動php-fpm
[[email protected]php-5.3.27]#cpsapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
[[email protected]php-5.3.27]#chmod755/etc/init.d/php-fpm
[[email protected]php-5.3.27]#servicephp-fpmstart
如果想讓它開機啟動,執行:
chkconfigphp-fpmon
檢測是否啟動:
psaux|grepphp-fpm
看看是不是有很多個程序(大概20多個)
如果重啟的時候報錯:
Starting php-fpm [14-Aug-2015 11:29:31] ERROR: An another FPM instance seems to already listen on /tmp/php-fcgi.sock
[14-Aug-2015 11:29:31] ERROR: FPM initialization failed
failed
檢測是否啟動:psaux|grepphp-fpm
如果有程序,用命令killall php-fpm 殺掉,如果沒有程序,就直接刪除/tmp/php-fcgi.sock 然後再啟動。
三、安裝 Nginx
Nginx官方網站:http://nginx.org
1、下載nginx
[[email protected] php-5.3.27]# cd /usr/local/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.4.4.tar.gz
2、解壓nginx
[[email protected] src]# tar -zxvf nginx-1.4.4.tar.gz
3、配置編譯引數
[[email protected] src]# cd nginx-1.4.4
[[email protected] nginx-1.4.4]# ./configure\
--prefix=/usr/local/nginx\
--with-http_realip_module\
--with-http_sub_module\
--with-http_gzip_static_module\
--with-http_stub_status_module\
--with-pcre
4、編譯nginx
[[email protected] nginx-1.4.4]# make
5、安裝nginx
[[email protected] nginx-1.4.4]# makeinstall
6、編寫nginx啟動指令碼,並加入系統服務
[[email protected]nginx-1.4.4]#vim/etc/init.d/nginx//寫入如下內容
#!/bin/bash
#chkconfig:-3021
#description:httpservice.
#SourceFunctionLibrary
./etc/init.d/functions
#NginxSettings
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
return0
}
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
儲存後,更改許可權:
[[email protected] nginx-1.4.4]# chmod 755 /etc/init.d/nginx
[[email protected] nginx-1.4.4]# chkconfig --add nginx
如果想開機啟動,請執行:
[[email protected] nginx-1.4.4]# chkconfig nginx on
更改nginx配置
首先把原來的配置檔案清空:
>/usr/local/nginx/conf/nginx.conf
“>” 這個符號為重定向的意思,單獨用它,可以把一個文字文件快速清空。
vim/usr/local/nginx/conf/nginx.conf
寫入如下內容:
usernobodynobody;
worker_processes2;
error_log/usr/local/nginx/logs/nginx_error.logcrit;
pid/usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile51200;
events
{
useepoll;
worker_connections6000;
}
http
{
includemime.types;
default_typeapplication/octet-stream;
server_names_hash_bucket_size3526;
server_names_hash_max_size4096;
log_formatcombined_realip'$remote_addr$http_x_forwarded_for[$time_local]'
'$host"$request_uri"$status'
'"$http_referer""$http_user_agent"';
sendfileon;
tcp_nopushon;
keepalive_timeout30;
client_header_timeout3m;
client_body_timeout3m;
send_timeout3m;
connection_pool_size256;
client_header_buffer_size1k;
large_client_header_buffers84k;
request_pool_size4k;
output_buffers432k;
postpone_output1460;
client_max_body_size10m;
client_body_buffer_size256k;
client_body_temp_path/usr/local/nginx/client_body_temp;
proxy_temp_path/usr/local/nginx/proxy_temp;
fastcgi_temp_path/usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errorson;
tcp_nodelayon;
gzipon;
gzip_min_length1k;
gzip_buffers48k;
gzip_comp_level5;
gzip_http_version1.1;
gzip_typestext/plainapplication/x-javascripttext/csstext/htmapplication/xml;
server
{
listen80;
server_namelocalhost;
indexindex.htmlindex.htmindex.php;
root/usr/local/nginx/html;
location~\.php${
includefastcgi_params;
fastcgi_passunix:/tmp/php-fcgi.sock;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;
}
}
}
儲存配置後,先檢驗一下配置檔案是否有錯誤存在:
/usr/local/nginx/sbin/nginx -t
如果顯示內容如下,則配置正確,否則需要根據錯誤提示修改配置檔案:
nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok
nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful
啟動nginx:
service nginx start
如果不能啟動,請檢視 “/usr/local/nginx/logs/error.log” 檔案,檢查nginx是否啟動:
psaux|grepnginx
看是否有程序。
測試是否解析php檔案
建立測試檔案:
vim/usr/local/nginx/html/2.php
內容如下:
<?php
echo"測試php是否解析";
?>
測試:
[[email protected]nginx]#curllocalhost/2.php
測試php是否解析
說明php解析正確。
或者我們建立檔案:
vim/usr/local/nginx/html/info.php
內容如下:
<?php phpinfo(); ?>
我們在網頁上輸入192.168.0.109/info.php會顯示下圖所示的網頁(要關機防火牆:iptables -F):
若報502錯誤,檢視日誌
# tail /usr/local/nginx/logs/nginx_error.log
2016/05/23 14:05:18 [crit] 7998#0: *22 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.18.103, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "192.168.18.120"
解決方法:
chmod 777 /tmp/php-fcgi.sock
service php-fpm restart
service nginx restart
轉載於:https://blog.51cto.com/msiyuetian/1688048