设为首页 收藏本站
查看: 1363|回复: 0

[经验分享] Lnmp 搭建(nginx,mysql,php,memcache,tomcat)

[复制链接]
发表于 2018-10-1 09:48:45 | 显示全部楼层 |阅读模式
  实验环境:rhel6.5  seliunx and iptables disabled
  nginx                  1.6.2
  mysql                 5.5.12
  php                   5.4.36
  memcached           1.4.4
  server1  172.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
  解压:tar  zxf nginx-1.6.2.tar.gz
  编译前稍作改动:
  掩盖版本号,为了安全:curl  -I IP/hostname 可查看服务器信息
  root@server1core]# pwd
  /pags/nginx-1.6.2/src/core
  [root@server1core]# 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功能,这样编译出来的内容会更小
  [root@server1cc]# pwd
  /pags/nginx-1.6.2/auto/cc
  [root@server1cc]# vim  gcc
  # debug
  CFLAGS="$CFLAGS-g"
  改为:
  # debug
  #CFLAGS="$CFLAGS -g"
  编译安装:
  ./configure--prefix=/usr/local/lnmp/nginx --with-http_ssl_module                                  --with-http_stub_status_module
  make  && make  install
  1.2修改配置文件,加以优化
  [root@server1nginx]# useradd -M -s /sbin/nologin nginx  #增加nginx用户,以此身份运行
  [root@server1nginx]# pwd
  /usr/local/lnmp/nginx
  [root@server1nginx]# vim conf/nginx.conf
  user  nginx;                 #以此身份运行
  worker_processes  2;   #默认启动几个进程
  #error_log  logs/error.log;
  #error_log  logs/error.log  notice;
  #error_log  logs/error.log  info;
  #pid        logs/nginx.pid;
  events {
  use epoll;               #高效方法,用于Linux内核2.6以后版本;
  worker_connections  1024;
  }
  软连命令到/usr/sbin 方便使用;或者写在~/.bash_profile下;source
  [root@server1nginx]# pwd
  /usr/local/lnmp/nginx
  ln -s  /usr/local/lnmp/nginx/sbin/nginx  /usr/sbin/  #一定要写绝对路径
  1.3检测语法;启动nginx;
  [root@server1lnmp]# 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
  [root@server1lnmp]# nginx
  [root@server1lnmp]# netstat   -anltp |  grep 80
  tcp        0     0 0.0.0.0:80                 0.0.0.0:*                    LISTEN     3595/nginx
  测试:
  [root@server1html]# pwd
  /usr/local/lnmp/nginx/html
  [root@server1html]# echo "test nginx" > index.html
  [root@foundation12~]# hostname
  foundation12.ilt.example.com
  [root@foundation12~]# curl 172.25.12.1
  test nginx 成功
  1.4查看nginx处理状态:
  [root@server1conf]# 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_status  on;
  }
  # proxy the PHP scripts to Apachelistening on 127.0.0.1:80

  [root@server1conf]# 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
  [root@server1 certs]# make wxl_nginx.pem
  umask 77 ; \
  ...................
  Country Name (2 letter code) [XX]:cn
  State or Province Name (full name) []:shaxi
  Locality Name (eg, city) [Default City]:xi'an
  Organization Name (eg, company) [Default Company Ltd]: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
  [root@server1 certs]# mv wxl_nginx.pem /usr/local/lnmp/nginx/conf/
  [root@server1 conf]# vim nginx.conf
  server {
  listen       443 ssl;
  server_name  server1.example.com;
  ssl_certificate      wxl_nginx.pem;
  ssl_certificate_key  wxl_nginx.pem;
  ssl_session_cache    shared:SSL:1m;
  ssl_session_timeout  5m;
  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers  on;
  location / {
  root   html;
  index  index.html index.htm;
  }
  }

  nginx -s>  浏览器输入https://172.25.12.1/  验证

  1.6搭建虚拟主机:
  #   location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
  server {
  listen  80;
  server_name www.linux-lover.com;             #在主机中做好解析
  location / {
  index index.html;
  root/usr/local/lnmp/nginx/vhost/linux-lover;
  }
  }
  #HTTPS server
  #
  [root@server1 nginx]# ls  -Rvhost/
  vhost/:
  linux-lover
  vhost/linux-lover:
  index.html

  1.7用nginx实现负载均衡
  定义http {
  include       mime.types;
  default_type  application/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.log  main;
  #使用上面定义的负载均衡
  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: [emerg] 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
  [root@server1mysql-5.5.12]# 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  && make  install

  重新编译时,需要清除旧的对象文件和缓存信息
  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 配置文件;
  [root@server1 support-files]# 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启动;设秘密
  [root@server1 mysql]# /etc/init.d/mysqld  start
  Starting MySQL.. SUCCESS
  [root@server1 support-files]#mysql_secure_installation
  三.php安装配置
  3.1安装mcrypt(加密扩展)
  3.1.1安装libmycrypt
  tar  jxf 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整合
  创建三个文件:
  [root@server1 php-5.4.36]# 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

  启动php  /etc/init.d/fpm start
  修改nginx配置,测试php
  location ~ \.php$ {
  root           html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_index  index.php;
  #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  include        fastcgi.conf;
  }
  写入测试文件cat htmls/index.php
  

  nginx -s>  修改php的时间;不然nginx日志会有警告:
  vim/usr/local/lnmp/php/etc/php.ini
  [Date]
  ; Defines thedefault timezone used by the date functions
  ;http://php.net/date.timezone
  date.timezone =Asia/Shanghai
  四.lnmp平台简单测试:搭建论坛
  unzip  Discuz_X3.2_SC_UTF8.zip   -d /usr/local/lnmp/nginx/html/
  只有upload有用;改名bbs   mv upload/ bbs/
  chmod   777  config/  -R
  chmod   777  data/  uc_*  -R
  浏览器输入http://172.25.12.1/bbs/install进行安装;
  mysql要开启
  修改nginx配置文件:
  location / {
  root   html;
  index  index.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 zxf  memcache-2.2.5.tgz;
  生成configure文件  [root@server1 memcache-2.2.5]# phpize
  编译安装 ./configure  --prefix=/usr/local/lnmp/php/modules/memcache --with-php-config=/usr/local/lnmp/php/bin/php-config
  [root@server1 memcache-2.2.5]#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加载该模块
  [root@server1etc]# pwd
  /usr/local/lnmp/php/etc
  [root@server1etc]# vim php.ini
  ; ... or under UNIX:
  ;
  ;   extension=msql.so
  extension=memcache.so

  重载生效:/etc/init.d/fpm>  检测是否加入:
  浏览器输入 http://172.25.12.1/index.php 查看是否有memcache模板

  而数据真正缓存在memcached服务端当中,上面所做的只是让php连接进memcache,使用它。所以下面真正安装memcache。
  安装memcached软件:yum install  memcached  -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
  cp  memcache.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
  使用测试文件测试:
  [root@server1 memcache-2.2.5]# cat /usr/local/lnmp/nginx/html/test.php
  
  浏览器访问 http://172.25.12.1/test.php不断刷新
  访问http://172.25.12.1/memcache.php 检测么么擦车命中率

  六.搭建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
  检测:
  [root@server1 ~]# cat test.java

  public>  {
  public staticvoid  main(String[] args)
  {
  System.out.println("testjava");
  }
  }
  编译:javactest.java
  运行:[root@server1 ~]# java test  #后面不接什么。自动补齐
  test java
  6.2安装tomcat:
  tar  zxf 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
  创建测试文件:[root@server1 ROOT]# cattest.jsp
  Time  is :

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-607000-1-1.html 上篇帖子: MySQl的意外断电后无法启动 下篇帖子: MySQL学习之路:多实例无法启动排错
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表