LNMP環境搭建
目錄
1 環境準備... 1
1.1 克隆虛拟機... 1
1.2 配置IP. 1
1.3 準備安裝軟體包... 1
2 安裝Nginx. 1
1)更改hostname、建立所需軟體存放目錄...1
2)下載下傳或上傳安裝所需要的軟體...1
3)解壓安裝nginx.1
4) 關閉防火牆... 1
5) 編輯nginx.conf1
6)建立web服務對應的目錄...1
7)修改本地host檔案 解析設定的IP位址(注意點)...1
8)編輯nginx.conf1
3 安裝Mysql1
1)解壓編譯cmake軟體...1
2)安裝相關依賴包...1
3)建立mysql使用者及使用者組...1
4)解壓編譯mysql1
5)建立連結以及設定mysql的環境變量...1
6)建立mysql啟動配置檔案...1
7)進行mysql初始化(兩個OK)...1
8) mysql的基本相關設定... 1
9) MySQL基礎管理操作指令... 1
4 安裝PHP. 1
5 LNMP整合配置實踐操作... 1
1) 編輯nginx.conf1
2) 檢查nginx配置檔案,平滑重新開機... 1
3) 編輯index.php檔案,看是否可以解析... 1
4)編輯index.php/mysql.php檔案...1
1 環境準備
參考LAMP環境搭建;
192.168.1.67
libiconv-1.14.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.gz
nginx-1.6.2
nginx-1.6.2.tar.gz
php-5.3.27.tar.gz
2 安裝Nginx
[root@moban ~]# hostname lnmp
[root@moban ~]# logout
[root@lnmp ~]# mkdir /home/oldboy/tools -p
[root@lnmp~]# cd /home/oldboy/tools/
-rw-r--r--. 1 root root 804164 Jun 9 03:30 nginx-1.6.2.tar.gz
-rw-r--r--. 1 root root 15008639 Jun 9 04:16 php-5.3.27.tar.gz
[root@lnmptools]# yum install pcre pcre-devel -y
[root@lnmptools]# yum install openssl openssl-devel -y
[root@lnmp tools]# rpm -qa pcre* open*
openssh-clients-5.3p1-84.1.el6.x86_64
openldap-2.4.23-31.el6.x86_64
openssl098e-0.9.8e-17.el6.centos.2.x86_64
pcre-7.8-6.el6.x86_64
openssh-5.3p1-84.1.el6.x86_64
pcre-devel-7.8-6.el6.x86_64
openssl-devel-1.0.1e-30.el6.11.x86_64
openssh-server-5.3p1-84.1.el6.x86_64
openssl-1.0.1e-30.el6.11.x86_64
[root@lnmp tools]# tar xfnginx-1.6.2.tar.gz
[root@lnmp tools]# cdnginx-1.6.2
##建立使用者nginx并且進行編譯安裝
[root@lnmp nginx-1.6.2]#useradd nginx -s /sbin/nologin -M
[root@lnmp nginx-1.6.2]# idnginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@lnmp nginx-1.6.2]#./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2--with-http_stub_status_module --with-http_ssl_module
[root@lnmp nginx-1.6.2]# make&& make install
[root@lnmp nginx-1.6.2]# echo$?
##建立連結
[root@lnmp nginx-1.6.2]# ln -s /application/nginx1.6.2/ /application/nginx
[root@lnmp nginx-1.6.2]# ll /application/nginx
[root@lnmp nginx-1.6.2]# ll /application/nginx/
##檢查文法并且啟動服務:
[root@lnmp nginx-1.6.2]#/application/nginx/sbin/nginx -t
[root@lnmp nginx-1.6.2]#/application/nginx/sbin/nginx
[root@lnmp nginx-1.6.2]#netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4796/nginx
[root@lnmp nginx-1.6.2]# ps-ef | grep nginx
root 4796 1 0 15:43 ? 00:00:00 nginx: master process/application/nginx/sbin/nginx
nginx 4797 4796 0 15:43 ? 00:00:00 nginx: worker process
root 4801 2447 0 15:43 pts/0 00:00:00 grep nginx
[root@lnmp nginx-1.6.2]# service iptables stop
[root@lnmp nginx-1.6.2]# setenforce 0
[root@lnmp nginx-1.6.2]# getenforce 0
[root@lnmp nginx-1.6.2]# cd /application/nginx/conf/
[root@lnmp conf]# cpnginx.conf nginx.conf.bak
[root@lnmp conf]# egrep -v"#|^$" nginx.conf.default >nginx.conf
[root@lnmp conf]# vi nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
######################################################___1
server {
listen 80;
server_name www.etiantian.org;
root /data/html/www;
index index.html index.htm;
}
######################################################___2
server_name blog.etiantian.org;
root /data/html/blog;
######################################################___3
server_name bbs.etiantian.org;
root /data/html/bbs;
[root@lnmp conf]# mkdir/data/html/{www,blog,bbs,wiki} -p
[root@lnmp conf]# touch/data/html/{www,blog,bbs,wiki}/index.html
[root@lnmp conf]# for name in www blog bbs wiki;doecho "http://$name.etiantian.org">/data/html/$name/index.html;done
[root@lnmp conf]# for name in www blog bbs wiki;docat /data/html/$name/index.html;done
http://www.etiantian.org
http://blog.etiantian.org
http://bbs.etiantian.org
http://wiki.etiantian.org
[root@lnmp conf]# ../sbin/nginx -t
[root@lnmp conf]# ps -ef |grep nginx
root 4796 1 0 15:43 ? 00:00:00 nginx: master process/application/nginx/sbin/nginx
root 4925 2447 0 16:28 pts/0 00:00:00 grep nginx
[root@lnmp conf]# ../sbin/nginx -s reload
192.168.1.67 www.etiantian.org blog.etiantian.org bbs.etiantian.org etiantian.org status.etiantian.org
##status
listen 80;
server_name status.etiantian.org;
stub_status on;
access_log off;
檢查并重新開機nginx
3 安裝Mysql
[root@lnmp tools]# tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
gmake
gmake install
[root@lnmp cmake-2.8.8]# cd ../
[root@lnmp tools]# yum install ncurses-devel -y
[root@lnmp tools]# yum install libaio-devel -y
[root@lnmp tools]# groupadd MySQL
[root@lnmp tools]# useradd mysql -s /sbin/nologin-M -g mysql
[root@lnmp tools]# tar xf mysql-5.5.32.tar.gz
[root@lnmp tools]# cd mysql-5.5.32
[root@lnmp mysql-5.5.32]# cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/application/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock\
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0 \
[root@lnmp mysql-5.5.32]# make && make install
[root@lnmp mysql-5.5.32]# ln -s/application/mysql-5.5.32 /application/mysql
[root@lnmp mysql-5.5.32]# ll /application/mysql/
[root@lnmp mysql-5.5.32]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[root@lnmp mysql-5.5.32]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@lnmp mysql-5.5.32]# source /etc/profile
[root@lnmp mysql-5.5.32]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@lnmp mysql-5.5.32]#
[root@lnmp mysql-5.5.32]# cpsupport-files/my-small.cnf /etc/my.cnf
[root@lnmp mysql-5.5.32]# chown -R mysql.mysql/application/mysql/data
[root@lnmp mysql-5.5.32]# chmod -R 1777 /tmp/
[root@lnmp mysql-5.5.32]# cd /application/mysql/scripts/
[root@lnmp scripts]# ./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
[root@lnmp scripts]# cd ../
[root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@lnmp mysql]# chmod +x /etc/init.d/mysqld
[root@lnmp mysql]#/etc/init.d/mysqld start
Starting MySQL... SUCCESS!
[root@lnmp mysql]# mysqladmin-uroot password '888888'
[root@lnmp mysql]# mysql-uroot -p888888
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32 Source distribution
Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarks of theirrespective
owners.
Type 'help;' or '\h' for help. Type '\c' to clearthe current input statement.
mysql> GRANT ALLPRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> quit
Bye
①單執行個體資料庫mysql的啟動指令:/etc/init.d/mysqld start。
②檢查端口:ss -lntup|grep 330或者使用netstat -lntup|grep 330.
③檢查程序:ps -ef|grep mysql|grep -v grep 兩個主要的程序,一個就是mysql_safe一個是mysqld。
④mysql啟動的基本原理:/etc/init.d/mysqld start是一個shell的啟動腳本,啟動後會最終調用mysqld_safe腳本,最後調用mysqld伺服器啟動mysql。檔案vim /application/mysql/support-files/mysql.server 中的$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
⑤初始化時候啟動方法:mysql_safe --user=mysql &,當找回root密碼的時候用此方法啟動,和/etc/init.d/mysqld 的啟動實質是一樣的。
⑥常用關閉mysql指令:/etc/init.d/mysqld stop 然後檢視端口是否關閉。
⑦mysql關閉的原理:在腳本檔案/etc/init.d/mysqld檔案中内容有兩個地方需要注意:if (kill -0 $mysqld_pid 2>/dev/null)還有一個地方就是kill $mysqld_pid。
⑧強制關閉資料庫方法:
killall mysqld
pkill mysqld
killall -9 mysqld
⑨優雅關閉資料庫方法:
第一種:mysqladmin方法:mysqladmin -uroot -p123456 shutdown
##${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
第二種:自帶腳本方法:/etc/init.d/mysqld stop
第三種:kill 信号方法:kill -USR2 ‘cat path/pid’(最好不要使用)
4 安裝PHP
1)上傳所需要安裝的軟體及插件
[root@lnmp conf]# cd /home/oldboy/tools/
[root@lnmp tools]# ll
total 23000
-rw-r--r--. 1 root root 4984397 Jun 9 04:16 libiconv-1.14.tar.gz
-rw-r--r--. 1 root root 1335178 Jun 9 04:16 libmcrypt-2.5.8.tar.gz
-rw-r--r--. 1 root root 471915 Jun 9 04:16 mcrypt-2.6.8.tar.gz
-rw-r--r--. 1 root root 931437 Jun 9 04:16 mhash-0.9.9.9.tar.gz
drwxr-xr-x. 9 1001 1001 4096 Jun 17 15:41 nginx-1.6.2
[root@lnmp tools]#
2)安裝PHP依賴的插件
[root@lnmp tools]# yuminstall -y libxslt-devel
[root@lnmp php-5.3.27]# yuminstall zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devellibxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel -y
[root@lnmp tools]# rpm -qazlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devellibjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
[root@lnmp tools]# tar zxflibiconv-1.14.tar.gz
[root@lnmp tools]# cd libiconv-1.14
[root@lnmp libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
make
make install
[root@lnmp libiconv-1.14]# cd ../
[root@lnmp tools]# tar zxflibmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
sleep 2
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
cd ../../
[root@lnmp tools]# tar zxfmhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
cd ../
rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib64/libmcrypt.la/usr/lib64/libmcrypt.la
ln -s /usr/local/lib64/libmcrypt.so/usr/lib64/libmcrypt.so
ln -s /usr/local/lib64/libmcrypt.so.4/usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib64/libmcrypt.so.4.4.8/usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib64/libmhash.a/usr/lib64/libmhash.a
ln -s /usr/local/lib64/libmhash.la/usr/lib64/libmhash.la
ln -s /usr/local/lib64/libmhash.so/usr/lib64/libmhash.so
ln -s /usr/local/lib64/libmhash.so.2/usr/lib64/libmhash.so.2
ln -s /usr/local/lib64/libmhash.so.2.0.1/usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config/usr/bin/libmcrypt-config
[root@lnmp tools]# tar zxfmcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure LD_LIBRARY_PATH=/usr/local/lib
3)安裝PHP軟體
[root@lnmp tools]# yuminstall openssl-devel -y
[root@lnmp tools]# tar xfphp-5.3.27.tar.gz
[root@lnmp tools]# cd php-5.3.27
[root@lnmp php-5.3.27]# ./configure \
--prefix=/application/php5.3.27 \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-curl \
--enable-short-tags \
--enable-zend-multibyte \
--with-gd \
--with-xmlrpc \
--with-openssl \
--enable-soap \
--enable-sockets \
--with-xsl \
--enable-mbstring \
--enable-gd-native-ttf \
--with-libxml-dir=/usr \
--enable-static \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--with-mcrypt \
--with-mhash \
--enable-pcntl \
--enable-zip \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
###出現以下界面表示編譯完成
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHPLicense, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms ofthis license agreement. |
| If you do not agree with the terms ofthis license, you must abort |
| the installation process at thispoint. |
Thank you for using PHP.
[root@lnmp php-5.3.27]# ln -s/application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@lnmp php-5.3.27]# make&& make install
[root@lnmp php-5.3.27]# ln -s/application/php5.3.27/ /application/php
4) 拷貝php配置檔案
[root@lnmp bbs]# cd /home/oldboy/tools/php-5.3.27
[root@lnmp php-5.3.27]# cp php.ini-production/application/php/etc/php.ini
5) 拷貝php啟動腳本,php-fpm配置檔案,更改php-fpm權限為755;添加php-fpm開機啟動;
[root@lnmp etc]# cd /home/oldboy/tools/php-5.3.27
[root@lnmp php-5.3.27]# cp sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
[root@lnmp php-5.3.27]# mv/application/php/etc/php-fpm.conf.default /application/php/etc/php-fpm.conf
6) 更改php-fpm的權限為755;添加php-fpm到系統啟動項,并設定開機啟動;啟動php-fpm;
[root@lnmp php-5.3.27]# chmod755 /etc/init.d/php-fpm
[root@lnmp php-5.3.27]#chkconfig --add php-fpm
[root@lnmp php-5.3.27]#service php-fpm start
Starting php-fpm done
[[email protected]]# chkconfig php-fpm on
這樣也可以
[[email protected]]# /application/php/sbin/php-fpm -t
[17-Jun-2015 20:06:48] NOTICE: configuration file/application/php5.3.27/etc/php-fpm.conf test is successful
[[email protected]]# /application/php/sbin/php-fpm
#将以下三項放入到/etc/rc.local,開機啟動。檔案中開啟服務是從後往前,開啟,停止服務是從前往後停止。
[[email protected]]# vi /etc/rc.local
##nginx+php-fpm by TS at 2014.10.09
/application/nginx/sbin/nginx
/application/php/sbin/php-fpm
[root@lnmp php-5.3.27]# netstat -lntup|grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 72576/php-fpm
5 LNMP整合配置實踐操作
[root@lnmp php-5.3.27]# cd /application/nginx/conf/
sendfile on;
index index.php index.html index.htm;
access_log logs/www_access.log;
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
includefastcgi.conf;
}
index index.phpindex.html index.htm;
access_log logs/blog_access.log;
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/bbs_access.log;
[root@lnmp conf]#/application/nginx/sbin/nginx -t
[root@lnmp conf]#/application/nginx/sbin/nginx -s reload
[root@lnmp data]# cd/data/html/
[root@lnmp html]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jun 17 15:51 bbs
drwxr-xr-x. 2 root root 4096 Jun 17 15:51 blog
drwxr-xr-x. 2 root root 4096 Jun 17 15:51 wiki
drwxr-xr-x. 2 root root 4096 Jun 17 15:51 www
[root@lnmp html]# cd www
[root@lnmp www]# vi index.php
<?php
phpinfo();
?>
[root@lnmp www]# cd ../bbs/
[root@lnmp bbs]# vi index.php
$link_id=mysql_connect('192.168.1.66','root','888888') or mysql_error();
if($link_id){
echo "mysql successful by TS !";
}else{
echo mysql_error();
[root@lnmp bbs]# vi mysql.php
$link_id=mysql_connect('192.168.1.67','root','888888') or mysql_error();
這裡LNMP是免安裝mysql的PHP安裝需要将編譯參數中的指定mysql路徑”--with-mysql=/application/mysql \”參數換成下面三個編譯參數:
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
不知道是不是這個原因,php連接配接MySQL不能給localhost,要指定IP才可以連接配接成功;
6 參考資料:
http://tslove.blog.51cto.com/9115838/1592742
http://8802265.blog.51cto.com/8792265/1650624
http://tslove.blog.51cto.com/9115838/1596567
轉載至:http://blog.csdn.net/sz_bdqn/article/details/46542367