Lnmp 搭建(nginx,mysql,php,memcache,tomcat)
实验环境:rhel6.5seliunx and iptables disablednginx 1.6.2
mysql 5.5.12
php 5.4.36
memcached 1.4.4
server1172.25.12.1 tomcat nginx mysql php memcached
server2 172.25.12.2 tomcat memcached
server3 172.25.12.3 tomcat
server4 172.25.12.4 tomcat
一.nginx:
1.1安装
安装依赖性:yum install gcc openssl-devel pcre-devel-y
解压:tarzxf nginx-1.6.2.tar.gz
编译前稍作改动:
掩盖版本号,为了安全:curl-I IP/hostname 可查看服务器信息
root@server1core]# pwd
/pags/nginx-1.6.2/src/core
# vim nginx.h
#definenginx_version 1006002
#defineNGINX_VERSION "1.6.2"
#defineNGINX_VER "nginx/" NGINX_VERSION
改为
#definenginx_version 1006002
#defineNGINX_VERSION "1.6.2"
#defineNGINX_VER "nginx/"
关闭gcc的debug功能,这样编译出来的内容会更小
# pwd
/pags/nginx-1.6.2/auto/cc
# vimgcc
# debug
CFLAGS="$CFLAGS-g"
改为:
# debug
#CFLAGS="$CFLAGS -g"
编译安装:
./configure--prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module
make&& makeinstall
1.2修改配置文件,加以优化
# useradd -M -s /sbin/nologin nginx#增加nginx用户,以此身份运行
# pwd
/usr/local/lnmp/nginx
# vim conf/nginx.conf
usernginx; #以此身份运行
worker_processes2; #默认启动几个进程
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid logs/nginx.pid;
events {
use epoll; #高效方法,用于Linux内核2.6以后版本;
worker_connections1024;
}
软连命令到/usr/sbin 方便使用;或者写在~/.bash_profile下;source
# pwd
/usr/local/lnmp/nginx
ln -s/usr/local/lnmp/nginx/sbin/nginx/usr/sbin/#一定要写绝对路径
1.3检测语法;启动nginx;
# nginx-t
nginx: theconfiguration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configurationfile /usr/local/lnmp/nginx/conf/nginx.conf test is successful
# nginx
# netstat -anltp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3595/nginx
测试:
# pwd
/usr/local/lnmp/nginx/html
# echo "test nginx" > index.html
# hostname
foundation12.ilt.example.com
# curl 172.25.12.1
test nginx 成功
1.4查看nginx处理状态:
# vim nginx.conf
# redirect server error pages to thestatic page /50x.html
#
error_page 500 502 503 504/50x.html;
location = /50x.html {
root html;
}
location/status {
stub_statuson;
}
# proxy the PHP scripts to Apachelistening on 127.0.0.1:80
# nginx -s> 在浏览器中输入http://172.25.12.1/status
可以看到:
Active connections: 1
server accepts handled requests
5 5 32
Reading: 0 Writing: 1 Waiting: 0
1.5配置https
生成证书: cd /etc/pki/tls/certs
# make wxl_nginx.pem
umask 77 ; \
...................
Country Name (2 letter code) :cn
State or Province Name (full name) []:shaxi
Locality Name (eg, city) :xi'an
Organization Name (eg, company) :Linux-lover
Organizational Unit Name (eg, section) []:Linux-lovers
Common Name (eg, your name or your server's hostname)[]:server1.example.com
Email Address []:1138122262@qq.com
# mv wxl_nginx.pem /usr/local/lnmp/nginx/conf/
# vim nginx.conf
server {
listen 443 ssl;
server_nameserver1.example.com;
ssl_certificate wxl_nginx.pem;
ssl_certificate_keywxl_nginx.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout5m;
ssl_ciphersHIGH:!aNULL:!MD5;
ssl_prefer_server_cipherson;
location / {
root html;
indexindex.html index.htm;
}
}
nginx -s> 浏览器输入https://172.25.12.1/验证
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
1.6搭建虚拟主机:
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
server {
listen80;
server_name www.linux-lover.com; #在主机中做好解析
location / {
index index.html;
root/usr/local/lnmp/nginx/vhost/linux-lover;
}
}
#HTTPS server
#
# ls-Rvhost/
vhost/:
linux-lover
vhost/linux-lover:
index.html
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
1.7用nginx实现负载均衡
定义http {
include mime.types;
default_typeapplication/octet-stream;
log_format main'$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream blance {
server172.25.12.2:80 weight=2;#添加权重。
server172.25.12.3:80;
}
#access_log logs/access.logmain;
#使用上面定义的负载均衡
server {
listen 80;
server_name www.loadblance.com;
location / {
proxy_pass http://blance;
}
}
1.8使用sticky模块实现session粘滞
重新编译 ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_modul --with-http_stub_status_module --add-module=/usr/local/lnmp/nginx/nginx-sticky-module-1.0#添加sticky模块,直接加绝对路径即可
make && make install
注意此字段表示成功加进sticky 模板
adding module in /usr/local/lnmp/nginx/nginx-sticky-module-1.0/
+ ngx_http_sticky_module wasconfigured
可以发现之前的配置文件并未改变;
注意加sticky模板就不能加权重了;
nginx: invalid parameter"weight=2" in /usr/local/lnmp/nginx/conf/nginx.conf:27
upstream blance {
sticky;
server172.25.12.2:80;
server172.25.12.3:80;
}
效果就是,对于同一ip来源,让其访问它之前一直访问的主机;而当其中一节点down之后nginx会调度其访问online的节点;说明nginx具备健康检查功能;
二mysql安装配置
2.1安装:
安装之前移除系统已经安装的数据库;
解决依赖性: yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel cmake
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql\
>-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \#数据库存放目录
>-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock\#套接字文件位置
>-DWITH_MYISAM_STORAGE_ENGINE=1 \
>-DWITH_INNOBASE_STORAGE_ENGINE=1 \
>-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
>-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
>-DWITH_PARTITION_STORAGE_ENGINE=1 \
>-DDEFAULT_CHARSET=utf8\ #使用 utf8 字符
>-DDEFAULT_COLLATION=utf8_general_ci \#校验字符
>-DEXTRA_CHARSETS=all
make&& makeinstall
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
重新编译时,需要清除旧的对象文件和缓存信息
make clean
rm -f CmakeCache.txt
2.2初始化数据库
为了方便使用命令:在~/.bash_profile声明mysql路径;
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
source ~/.bash_profile #使之生效
建立mysql用户:useradd-M -s /sbin/nologin mysql
根据主机内存复制 mysql 配置文件;
# pwd
/usr/local/lnmp/mysql/support-files
cp my-medium.cnf/etc/my.cnf #之前的最好备份下
初始化数据库
chown -Rmysql.mysql .
scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
chown -R root .
chown -Rmysql.mysql data
cpsupport-files/mysql.server /etc/init.d/mysqld
2.3启动;设秘密
# /etc/init.d/mysqldstart
Starting MySQL.. SUCCESS
#mysql_secure_installation
三.php安装配置
3.1安装mcrypt(加密扩展)
3.1.1安装libmycrypt
tarjxf libmcrypt-2.5.8.tar.bz2
cdlibmcrypt-2.5.8
./configure--prefix=/usr/local/lnmp/php/modules/libmcrypt
make&& make install
cd libltdl/
./configure--prefix=/usr/local/lnmp/php/modules/libmcrypt/ --enable-ltdl-install
添加系统动态连接库:
vim/etc/ld.so.conf
includeld.so.conf.d/*.conf
/usr/local/lnmp/php/module/libmcrypt/lib/
刷新;查看
ldconfig -v | grep libmcrypt
3.1.2安装mhash
tar jxf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure--prefix=/usr/local/lnmp/php/modules/mhash
make && make install
vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lnmp/php/module/libmcrypt/lib/
/usr/local/lnmp/php/modules/mhash/lib/
ldconfig -v | grep mhash
软链到系统能查找的库里
ln -s /usr/local/lnmp/php/module/mhash/lib/* /usr/local/lib/
ln -s /usr/local/lnmp/php/module/mhash/include/* /usr/local/include/
3.1.3安装mcrypt
./configure --prefix=/usr/local/lnmp/php/modules/mcrypt --with-libmcrypt-prefix=/usr/local/lnmp/php/modules/libmcrypt/
也可以直接安装在系统的库里面方便查找
3.2安装php
./configure--prefix=/usr/local/lnmp/php/ --with-config-file-path=/usr/local/lnmp/php/etc--with-mysql=/usr/local/lnmp/mysql/--with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config --with-openssl-with-gd --with-zlib --with-curl --enable-bcmath --with-libxml-dir --with-png-dir--with-jpeg-dir--with-freetype-dir--without-pear--with-gettext--with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets--enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx--with-mcrypt=/usr/local/lnmp/php/modules/libmcrypt/--with-mhash
make &&make install
3.3PHP和nginx整合
创建三个文件:
# pwd
/pags/php-5.4.36
配置文件:cp php.ini-production/usr/local/lnmp/php/etc/php.ini
启动脚本: cpsapi/fpm/init.d.php-fpm/etc/init.d/fpm
chmod +x /etc/init.d/fpm
fpm配置文件: cpphp-fpm.conf.default php-fpm.conf
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
启动php/etc/init.d/fpm start
修改nginx配置,测试php
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
include fastcgi.conf;
}
写入测试文件cat htmls/index.php
nginx -s> 修改php的时间;不然nginx日志会有警告:
vim/usr/local/lnmp/php/etc/php.ini
; Defines thedefault timezone used by the date functions
;http://php.net/date.timezone
date.timezone =Asia/Shanghai
四.lnmp平台简单测试:搭建论坛
unzipDiscuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
只有upload有用;改名bbs mv upload/ bbs/
chmod 777config/-R
chmod 777data/uc_*-R
浏览器输入http://172.25.12.1/bbs/install进行安装;
mysql要开启
修改nginx配置文件:
location / {
root html;
indexindex.html index.htm index.php;
}
五.添加memcache模块
PHP 扩展的 Memcache 实际上是连接 Memcache 的方式
为了方便使用php有关命令:
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
source~/.bash_profile
安装php扩展memcache:tar zxfmemcache-2.2.5.tgz;
生成configure文件# phpize
编译安装 ./configure--prefix=/usr/local/lnmp/php/modules/memcache --with-php-config=/usr/local/lnmp/php/bin/php-config
#make && make install
Installing shared extensions: /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/
检测:ll /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/
-rwxr-xr-x 1 root root 259040 Apr 3 08:03 memcache.so
使php加载该模块
# pwd
/usr/local/lnmp/php/etc
# vim php.ini
; ... or under UNIX:
;
; extension=msql.so
extension=memcache.so
重载生效:/etc/init.d/fpm> 检测是否加入:
浏览器输入 http://172.25.12.1/index.php 查看是否有memcache模板
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
而数据真正缓存在memcached服务端当中,上面所做的只是让php连接进memcache,使用它。所以下面真正安装memcache。
安装memcached软件:yum installmemcached-y
按照生产环境修改配置文件:vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="
启动服务:/etc/init.d/memcached restart
pwd/root/memcache-2.2.5
cpmemcache.php example.php/usr/local/lnmp/nginx/html/
vim/usr/local/lnmp/nginx/html/memcache.php
$VERSION='$Id: memcache.php,v 1.2 2008/09/11 19:21:06 mikl Exp $';
define('ADMIN_USERNAME','wxl'); // Admin Username
define('ADMIN_PASSWORD','redhat'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = '172.25.12.1:11211';// add more as an array #memcache缓存服务器地址
$MEMCACHE_SERVERS[] = '172.25.12.2:11211';// add more as an array
用telnet测试下memcache
telnet localhost 11211
stats
使用测试文件测试:
# cat /usr/local/lnmp/nginx/html/test.php
浏览器访问 http://172.25.12.1/test.php不断刷新
访问http://172.25.12.1/memcache.php 检测么么擦车命中率
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
六.搭建jsp平台
6.1搭建java运行平台:
下载安装;运行chmod +x jdk-6u32-linux-x64.bin
sh jdk-6u32-linux-x64.bin
之后会生成一个包 mv jdk1.6.0_32//usr/local/lnmp/jdk
修改环境变量 : vim/etc/profile
unset -f pathmunge
export JAVA_HOME=/usr/local/lnmp/jdk
export> export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
检测:
# cat test.java
public> {
public staticvoidmain(String[] args)
{
System.out.println("testjava");
}
}
编译:javactest.java
运行:# java test#后面不接什么。自动补齐
test java
6.2安装tomcat:
tarzxf apache-tomcat-7.0.37.tar.gz
mvapache-tomcat-7.0.37 /usr/local/lnmp/tomcat
pwd /usr/local/lnmp/tomcat/
启动: cd bin/ ;./startup.sh
检测:ss -antpl | grep 8080
浏览访问 http://172.25.12.1:8080/
6.3把tomact部署在nginx的后端
tomcat的端口是8080 nginx的端口是80用户自己是不会输入端口号的。所以请求先到 达nginx,nginx再交给tomacat
创建测试文件:# cattest.jsp
Timeis :
页:
[1]