您的位置:首页 > 其它

LNMP环境搭建

2015-06-18 03:33 447 查看
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 环境准备

1.1 克隆虚拟机

参考LAMP环境搭建;

1.2 配置IP

192.168.1.67

1.3 准备安装软件包

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

1)更改hostname、创建所需软件存放目录

[root@moban ~]# hostname lnmp

[root@moban ~]# logout

[root@lnmp ~]# mkdir /home/oldboy/tools -p

2)下载或上传安装所需要的软件

[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

3)解压安装nginx

[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$?

0

##创建链接

[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

4) 关闭防火墙

[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

5) 编辑nginx.conf

[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 {

listen 80;

server_name blog.etiantian.org;

root /data/html/blog;

index index.html index.htm;

}

######################################################___3

server {

listen 80;

server_name bbs.etiantian.org;

root /data/html/bbs;

index index.html index.htm;

}

}

6)创建web服务对应的目录

[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

nginx 4797 4796 0 15:43 ? 00:00:00 nginx: worker process

root 4925 2447 0 16:28 pts/0 00:00:00 grep nginx

[root@lnmp conf]# ../sbin/nginx -s reload

7)修改本地host文件 解析设置的IP地址(注意点)

192.168.1.67 www.etiantian.org blog.etiantian.org bbs.etiantian.org etiantian.org status.etiantian.org

8)编辑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;

server {

listen 80;

server_name www.etiantian.org;

root /data/html/www;

index index.html index.htm;

}

server {

listen 80;

server_name blog.etiantian.org;

root /data/html/blog;

index index.html index.htm;

}

server {

listen 80;

server_name bbs.etiantian.org;

root /data/html/bbs;

index index.html index.htm;

}

##status

server {

listen 80;

server_name status.etiantian.org;

stub_status on;

access_log off;

}

}

检查并重启nginx

[root@lnmp conf]# ../sbin/nginx -t

[root@lnmp conf]# ../sbin/nginx -s reload

3 安装Mysql

1)解压编译cmake软件

[root@lnmp tools]# tar xf cmake-2.8.8.tar.gz

cd cmake-2.8.8

./configure

gmake

gmake install

2)安装相关依赖包

[root@lnmp cmake-2.8.8]# cd ../

[root@lnmp tools]# yum install ncurses-devel -y

[root@lnmp tools]# yum install libaio-devel -y

3)创建mysql用户及用户组

[root@lnmp tools]# groupadd mysql

[root@lnmp tools]# useradd mysql -s /sbin/nologin-M -g mysql

4)解压编译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

5)创建链接以及设置mysql的环境变量

[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]#

6)创建mysql启动配置文件

[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/

7)进行mysql初始化(两个OK)

[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

8) 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

9) MySQL基础管理操作命令

①单实例数据库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

-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@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

./configure

make

make install

sleep 2

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

cd ../../

[root@lnmp tools]# tar zxfmhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9/

./configure

make

make install

sleep 2

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/

/sbin/ldconfig

./configure LD_LIBRARY_PATH=/usr/local/lib

make

make install

cd ../

sleep 2

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

[root@lnmpphp-5.3.27]# chkconfig php-fpm on

这样也可以

[root@lnmpphp-5.3.27]# /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

[root@lnmpphp-5.3.27]# /application/php/sbin/php-fpm

#将以下三项放入到/etc/rc.local,开机启动。文件中开启服务是从后往前,开启,停止服务是从前往后停止。

[root@lnmpphp-5.3.27]# 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整合配置实践操作

1) 编辑nginx.conf

[root@lnmp php-5.3.27]# cd /application/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;

server {

listen 80;

server_name www.etiantian.org;

root /data/html/www;

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;

}

}

server {

listen 80;

server_name blog.etiantian.org;

root /data/html/blog;

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;

}

}

server {

listen 80;

server_name bbs.etiantian.org;

root /data/html/bbs;

index index.phpindex.html index.htm;

access_log logs/bbs_access.log;

location ~ .*\.(php|php5)?$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

##status

server {

listen 80;

server_name status.etiantian.org;

stub_status on;

access_log off;

}

}

2) 检查nginx配置文件,平滑重启

[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

3) 编辑index.php文件,看是否可以解析

[root@lnmp html]# cd www

[root@lnmp www]# vi index.php

<?php

phpinfo();

?>

4)编辑index.php/mysql.php文件

[root@lnmp www]# cd ../bbs/

[root@lnmp bbs]# vi index.php

<?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

<?php

$link_id=mysql_connect('192.168.1.67','root','888888') or mysql_error();

if($link_id){

echo "mysql successful by TS !";

}else{

echo mysql_error();

}

?>

这里LNMP是免安装mysql的PHP安装需要将编译参数中的指定mysql路径”--with-mysql=/application/mysql \”参数换成下面三个编译参数:

--with-mysql=mysqlnd \

--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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: