搭建LNMP架构
Nginx的介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP/FTP 代理服务器。Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过五年半了。Igor Sysoev在建立的项目时,使用基于BSD许可。自Nginx 发布以来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。Netcraft的统计显示,Nginx(绿线)上升趋势明显,在不久的将来有可能超越Apache。
Mysql介绍
MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理。在全球排名前1万位的网站中,开源软件使用率超过75%,其中比较受欢迎的开源组合是LAMP(LNMP)。加上阿里带头发展”去IOE”运动(IMB、ORACLE和EMC),加上之前的”棱镜门事件”,使得Mysql大受欢迎,哈哈,扯远了。
PHP介绍
PHP作为网络开发的强大语言之一,现在应用非常广泛,具有开放源代码,跨平台性强,开发快捷,效率高,面向对象,并且易于上手,专业专注等诸多优点。各种PHP开发框架也让程序开发变的简单有效。
实验环境:CentOS release 6.4 (Final) IP:192.168.2.100
依赖包安装
1
2
3
4
# yum -y install ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-develpcre-devel libjpeg-devel libpng-devel freetype-devellibcurl-devel
# tar xf libmcrypt-2.5.6.tar.gz
# cd libmcrypt-2.5.6
# ./configure && make && make install
Nginx安装
1
2
3
4
5
6
7
8
9
#groupadd www
#useradd -g www www
#tar xf nginx-1.6.2.tar.gz
#cd nginx-1.6.2
#./configure --prefix=/usr/local/webserver/nginx1.6 --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
#主要参数说明:
--with-http_ssl_module #启用ttps协议模块。
--with-http_stub_status_module #用来监控Nginx当前状态。
# make && make install
Nginx第一种启动方法
1
2
3
4
5
6
7
8
# /usr/local/webserver/nginx1.6/sbin/nginx -t #检查配置文件是否错误。
nginx: the configuration file /usr/local/webserver/nginx1.6/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx1.6/conf/nginx.conf test is successful
# /usr/local/webserver/nginx1.6/sbin/nginx -c /usr/local/webserver/nginx1.6/conf/nginx.conf
# netstat -anlp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4618/nginx
unix3 [ ] STREAM CONNECTED 280764618/nginx
unix3 [ ] STREAM CONNECTED 280754618/nginx
Nginx第二种启动方法,做成服务启动(推荐),篇幅有限,脚本看附件。
1
2
3
4
5
# vim /etc/init.d/nginx #脚本内容看附件。
# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
输入IP地址若返回如下页面,说明已经安装成功了。
Mysql安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# groupadd mysql
# useradd -r -g mysql mysql
# tar xf mysql-5.6.14.tar.gz
# cd mysql-5.6.14
# mkdir -p /usr/local/webserver/mysql5.6
# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql5.6/ \#指定安装目录
> -DMYSQL_DATADIR=/usr/local/webserver/mysql5.6/data \ #指定数据存在目录
> -DWITH_MYISAM_STORAGE_ENGINE=1 \ #支持MyIASM引擎
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #支持InnoDB引擎
> -DWITH_MEMORY_STORAGE_ENGINE=1 \ #支持Memory引擎
> -DWITH_READLINE=1 \ #快捷键功能
> -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ #连接数据库socket路径
> -DMYSQL_TCP_PORT=3306 \ #指定端口
> -DENABLED_LOCAL_INFILE=1 \ #允许从本地导入数据
> -DWITH_PARTITION_STORAGE_ENGINE=1 \ #安装支持数据库分区
> -DEXTRA_CHARSETS=all \ #安装所有的字符集
> -DDEFAULT_CHARSET=utf8 \ #指定默认字符
> -DDEFAULT_COLLATION=utf8_general_ci #指定字符集的校对规则
#make && make install
# chown mysql.mysql /usr/local/webserver/mysql5.6/ -R
# cd /usr/local/webserver/mysql5.6/scripts/
# ./mysql_install_db --basedir=/usr/local/webserver/mysql5.6/ --datadir=/usr/local/webserver/mysql5.6/data/
# rm -rf /etc/my.cnf
# cp -a /usr/local/webserver/mysql5.6/support-files/mysql.server /etc/init.d/mysqld
# cp -a /usr/local/webserver/mysql5.6/support-files/my-default.cnf /usr/local/webserver/mysql5.6/my.cnf
# chkconfig --add mysqld
# chkconfig mysqld on
# vi /usr/local/webserver/mysql5.6/my.cnf#这里先指定mysql的目录和数据的存放目录,其他参数再根据需求来调整。
basedir = /usr/local/webserver/mysql5.6/
datadir = /usr/local/webserver/mysql5.6/data
# service mysqld start
Starting MySQL..........
# netstat -anlp |grep mysqld
tcp 0 0 :::3306 :::* LISTEN 17411/mysqld
unix2 [ ACC ] STREAM LISTENING 9770217411/mysqld /var/lib/mysql/mysql.sock
PHP安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# tar xf php-5.6.11.tar.gz
# cd php-5.6.11
#./configure --prefix=/usr/local/webserver/php5.6 --enable-fpm --with-mcrypt --enable-mbstring --with-mysql=/usr/local/webserver/mysql5.6/ --with-mysqli=/usr/local/webserver/mysql5.6/bin/mysql_config
#重要参数说明
--enable-fpm的作用是开启php的fastcgi功能即开启php-fpm功能。
--enable- mbstring表示启用mbstring模块mbstring模块的主要作用在于检测和转换编码,提供对应的多字节操作的字符串函数。
#make && make install
# cp -a php.ini-production /usr/local/webserver/php5.6/etc/php.ini
# cp -a /usr/local/webserver/php5.6/etc/php-fpm.conf.default /usr/local/webserver/php5.6/etc/php-fpm.conf
# cp -a sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm#做成服务启动
# chkconfig --add php-fpm
# chkconfig php-fpm on
# chmod +x /etc/init.d/php-fpm
# service php-fpm start
Starting php-fpmdone
# netstat -anlp |grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 32657/php-fpm
unix3 [ ] STREAM CONNECTED 218072 32657/php-fpm
unix3 [ ] STREAM CONNECTED 218071 32657/php-fpm
重点来了,整合LNMP。
1
2
3
4
5
6
7
8
9
10
#/usr/local/webserver/nginx1.6/conf/nginx.conf #启用下面这几行。
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
include fastcgi_params;
}
include test.conf; #同时在配置文件添加一个虚拟主机。
添加一个虚拟主机。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#vi /usr/local/webserver/nginx1.6/conf/fastcgi.conf #在后面添加一行。
fastcgi_index index.php;
#mkdir -p /data/web/www.test.com
# chown www:www /data/web/ -R
# vim/usr/local/webserver/nginx1.6/conf/test.conf
server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php default.html default.htm default.php;
root/data/web/www.test.com/;
if (!-f $request_filename){
rewrite ^/(.+)$ /index.php?$1& last;
}
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass127.0.0.1:9000;
fastcgi_param APPLICATION_ENV development;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
}
重启Nginx
# service nginx reload
nginx: the configuration file /usr/local/webserver/nginx1.6/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx1.6/conf/nginx.conf test is successful
Reloading nginx:
测试结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cat /data/web/www.test.com/phpinfo.php
<?php
phpinfo();
?>
#cat /data/web/www.test.com/index.php
<?php
$link=mysql_connect('localhost','root','redhat'); #用root连接本地的数据,密码是redhat。
if ($link)
echo "Successfuly";
else
echo "Faile";
mysql_close();
?>
查看结果,看到如下的返回结果,证明整合LNMP成功了。
LNMP架构已经安装完成,安装并不难,难在对于架构的维护!!!
页:
[1]