centos搭建LNMP

  1. 云栖社区>
  2. 博客>
  3. 正文

centos搭建LNMP

技术小阿哥 2017-11-28 14:09:00 浏览644
展开阅读全文

LNMP是一个基于CentOS/Debian编写的Nginx、PHP、MySQLphpMyAdmineAccelerator

可以在VPS、独立主机上轻松的安装LNMP生产环境

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debiancentosubuntufedoragentoo等。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

Mysql是一个小型关系型数据库管理系统

PHP是一种在服务器端执行的嵌入HTML文档的脚本语言

这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

特点

Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。

Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。

优点

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。

作为负载均衡服务器:Nginx 既可以在内部直接支持RailsPHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。

作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。


实验环境

centos_5.8(x64)


实验软件

cmake-2.8.6.tar.gz

mysql-5.5.17.tar.gz

pcre-8.13.tar.gz

nginx-1.0.9.tar.gz

libiconv-1.14.tar.gz

libmcrypt-2.5.8.tar.gz

mhash-0.9.9.9.tar.gz

mcrypt-2.6.8.tar.gz  

php-5.4.5.tar.gz



软件安装

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers


yum -y install pcre-devel zlib-devel gd-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel curl-devel freetype-devel bison gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel* mysql-devel


tar zxvf cmake-2.8.6.tar.gz

cd cmake-2.8.6

./configure

gmake && gmake install


/usr/sbin/groupadd mysql

/usr/sbin/useradd -g mysql mysql

tar zxvf mysql-5.5.17.tar.gz

cd mysql-5.5.17

cmake \

> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

> -DMYSQL_DATADIR=/usr/local/mysql/data \

> -DSYSCONFDIR=/etc \

> -DWITH_MYISAM_STORAGE_ENGINE=1 \

> -DWITH_INNOBASE_STORAGE_ENGINE=1 \

> -DWITH_MEMORY_STORAGE_ENGINE=1 \

> -DWITH_READLINE=1 \

> -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \

> -DMYSQL_TCP_PORT=3306 \

> -DENABLED_LOCAL_INFILE=1 \

> -DWITH_PARTITION_STORAGE_ENGINE=1 \

> -DEXTRA_CHARSETS=all \

> -DDEFAULT_CHARSET=utf8 \

> -DDEFAULT_COLLATION=utf8_general_ci \

> -DMYSQL_USER=mysql \

> -DWITH_DEBUG=0 \

> -DWITH_SSL=system

make && make install

chmod +w /usr/local/mysql/

chown -R mysql:mysql /usr/local/mysql

cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 

chmod +x /etc/init.d/mysqld

/etc/init.d/mysqld start


vim /etc/profile

PATH=/usr/local/mysql/bin:$PATH

export PATH          尾行添加两行配置

source /etc/profile


vim /etc/my.cnf

在[mysqld]添加

datadir = /data/mysql


找到

max_allowed_packet = 1M

改为

max_allowed_packet = 16M



netstat -tuplna | grep mysql

tcp  0  0 0.0.0.0:3306    0.0.0.0:*   LISTEN  31341/mysqld


ps -e | grep mysqld

31054 pts/0 00:00:00 mysqld_safe

31341 pts/0 00:00:00 mysqld


/usr/local/mysql/bin/mysqladmin -uroot password   数据库密码

/usr/local/mysql/bin/mysql -uroot -p          数据库密码

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>



tar zxvf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local

make && make install


tar zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make && make install


tar zxvf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9

./configure

make && make install

ln -s /usr/local/lib/libmcrypt.la      /usr/lib/libmcrypt.la

ln -s /usr/local/lib/libmcrypt.so      /usr/lib/libmcrypt.so

ln -s /usr/local/lib/libmcrypt.so.4     /usr/lib/libmcrypt.so.4

ln -s /usr/local/lib/libmcrypt.so.4.4.8  /usr/lib/libmcrypt.so.4.4.8

ln -s /usr/local/lib/libmhash.a       /usr/lib/libmhash.a

ln -s /usr/local/lib/libmhash.la      /usr/lib/libmhash.la

ln -s /usr/local/lib/libmhash.so      /usr/lib/libmhash.so

ln -s /usr/local/lib/libmhash.so.2    /usr/lib/libmhash.so.2

ln -s /usr/local/lib/libmhash.so.2.0.1  /usr/lib/libmhash.so.2.0.1

ln -s /usr/local/bin/libmcrypt-config  /usr/bin/libmcrypt-config


tar zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8

/sbin/ldconfig

./configure

make && make install


tar zxvf php-5.4.5.tar.gz

cd php-5.4.5

./configure --prefix=/usr/local/php \

--with-config-file-path=/usr/local/php/etc \

--with-iconv-dir=/usr/local/ --with-freetype-dir \

--with-mysql=/usr/local/mysql \

--with-mysqli=/usr/local/mysql/bin/mysql_config \

--with-jpeg-dir --with-png-dir --with-zlib \

--with-mhash --enable-sockets --enable-ftp \

--with-libxml-dir --enable-xml --disable-rpath \

--enable-safe-mode --enable-bcmath \

--enable-shmop --enable-sysvsem \

--enable-inline-optimization --with-curl \

--with-curlwrappers \

--enable-mbregex \

--enable-mbstring --with-mcrypt --with-gd \

--enable-gd-native-ttf --with-openssl --with-mhash \

--enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl \

--enable-fpm \

--with-xmlrpc --enable-zip --enable-soap \

--without-pear

ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/         系统为64位数,/usr/lib64

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

make ZEND_EXTRA_LIBS='-liconv'

make install

cp php.ini-production /usr/local/php/etc/php.ini

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm 


更改 php-fpm

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

mkdir -p /var/log/nginx

chmod +x /var/log/nginx

chown -R www:www /var/log/nginx

mkdir -p /data/www

chmod +x /data/www

chown -R www:www /data/www

cp -p /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

vim /usr/local/php/etc/php-fpm.conf

找到

pm.max_children = 5

改为

pm.max_children = 64


找到

pm.start_servers = 2

改为

pm.start_servers = 20


找到

pm.min_spare_servers = 1

改为

pm.min_spare_servers = 5


找到

pm.max_spare_servers = 3

改为

pm.max_spare_servers = 35


找到

pm.max_requests = 500

改为

pm.max_requests = 1024


找到

user = nobody

group = nobody

改为

user = www

group = www

:wq  

/usr/local/php/sbin/php-fpm -t

[22-Jul-2013 09:57:45] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful  如果看到这个提示,证明测试成功

/etc/init.d/php-fpm restart    也可以使用这种办法重启服务

/usr/local/php/sbin/php-fpm &   开机启动php-fpm服务

netstat -tuplna

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address    Foreign Address    State  PID/Program name  

tcp  0  0 127.0.0.1:9000    0.0.0.0:*     LISTEN  30540/php-fpm  

tcp  0  0 0.0.0.0:3306    0.0.0.0:*     LISTEN  3  

[1]+ Done     /usr/local/php/sbin/php-fpm


vim /etc/rc.d/rc.local 
/usr/local/php/sbin/php-fpm & 在配置文件的最后一行写入


tar zxvf pcre-6.0.tar.gz

cd pcre-6.0

./configure

make && make install


tar zxvf nginx-1.0.9.tar.gz

cd nginx-1.0.9

./configure --user=www --group=www \

--prefix=/usr/local/nginx \

--sbin-path=/usr/local/nginx/sbin/nginx \

--conf-path=/usr/local/nginx/conf/nginx.conf \

--with-http_stub_status_module \

--with-http_ssl_module \

--with-pcre \

--lock-path=/var/run/nginx.lock \

--pid-path=/var/run/nginx.pid

make && make install

cp -p /usr/local/nginx/conf/nginx.conf ~ 在更改配置文件前最好先备份

vim /usr/local/nginx/conf/nginx.conf

找到

events {

 worker_connections 1024;

}

改为

events {

 use epoll;

 worker_connections 1024;

}


找到

#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_params;

  #}

改为

location ~ \.php$ {

   root   html;

   fastcgi_pass 127.0.0.1:9000;

   fastcgi_index index.php;

   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   include  fastcgi_params;

  }


找到

#user nobody;

改为

user www;

/usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果看到这两行配置,说明nginx配置成功了

cp /usr/local/nginx/sbin/nginx /etc/init.d/

chmod +x /etc/rc.d/init.d/nginx 

/etc/rc.d/init.d/nginx restart  也可以使用这种办法重启nginx

/usr/local/nginx/sbin/nginx &   开机启动php-fpm服务

/usr/local/nginx/sbin/nginx -s reload  nginx平滑重启

/usr/local/nginx/sbin/nginx -s reload

nginx: [error] invalid PID number "" in "/var/run/nginx.pid"

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 从新生成nginx pid文件

/usr/local/nginx/sbin/nginx -s reload   再次执行如果没有报错 证明配置正确


netstat -tuplna | grep nginx

tcp  0  0 0.0.0.0:80     0.0.0.0:*     LISTEN  7458/nginx 看到这一行,证明nginx端口正处在监听状态


vim /etc/rc.d/rc.local

/usr/local/nginx/sbin/nginx & 输入一行



测试

http://服务器ip

222620407.png

如果看到这个提示证明,nginx安装成功

下面建立php测试页,看一下lnmp的架构是否成功

cd /usr/local/nginx/html/

touch index.php

vim index.php

<?php

phpinfo();

?>           写入以下内容,保存推出

输入 http://服务器ip/index.php


223138533.png

看到这个页面,就是php的测试页,也就是说lnmp的架构成功了


php添加模块


tar zxvf memcache-2.2.7.tgz 

cd memcache-2.2.7

/usr/local/php/bin/phpize 

Configuring for:

PHP Api Version:         20131106

Zend Module Api No:      20131226

Zend Extension Api No:   220131226

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

ls

memcache.so 


vim /usr/local/php/etc/php.ini 

;extension=php_pdo_firebird.dll

;extension=php_pdo_mysql.dll

extension=memcache.so          添加模块 配置文件 ;为注释此行配置不起作用

session.save_handler = memcache

session.save_path = "tcp://127.0.0.100:11211"

pkill nginx            

/etc/rc.d/init.d/nginx -s reload    平滑重启nginx

/etc/rc.d/init.d/php-fpm  reload    修改配置文件从新加载服务(线上业务最好是reload)

http://服务器ip/test.php   观察没有加载模块



wKioL1kVDoPjN6IYAAATZopz3pQ427.png-wh_50



本文转自 mailfile 51CTO博客,原文链接:http://blog.51cto.com/mailfile/1254505,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
技术小阿哥
+ 关注