LNMP:Linux+Nginx+Mysql+PHP
系统环境:
rhel 6.3 selinux,iptables disabled
需要的软件:
mysql-5.5.12.tar.gz
nginx-1.2.7.tar.gz
libiconv-1.13.1.tar.gz
libmcrypt-2.5.8.tar.bz2
mhash-0.9.9.9.tar.bz2
mcrypt-2.6.8.tar.gz
php-5.4.12.tar.bz2
libiconv-1.13.1.tar.gz
源码包编译安装LNMP平台
Mysql:
#安装方法在INSTALL-SOURCE中有详细描述
#源码安装参照INSTALL-SOURCE中2.9. Installing MySQL from Source
#源码安装配置参照INSTALL-SOURCE中2.9.4. MySQL Source-Configuration Options
yum install make cmake gcc gcc-c++ ncurses-devel bison -y
tar xzf mysql-5.5.12.tar.gz
cd mysql-5.5.12
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \
#安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#存储引擎支持,The MyISAM, MERGE, MEMORY, and CSV engines are mandatory (always compiled into the server) and need not be installed explicitly.
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8 \
#使用字符utf8
-DDEFAULT_COLLATION=utf8_general_ci \
#校验字符
-DENABLED_LOCAL_INFILE=1 \
#允许从本地导入数据
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \
#Unix socket文件路径
-DMYSQL_TCP_PORT=3306 \
#监听端口
-DWITH_EXTRA_CHARSETS=all \
#额外字符
-DWITH_READLINE=1 \
#支持快捷键
-DWITH_SSL=yes
#支持SSL
cmake失败
make clean
rm -fr CMakeCache.txt
cmake成功
make && make install
useradd -M -s /sbin/nologin mysql
#mysql数据库的用户,使用mysql用户对数据库进行管理,保证安全性
cd /usr/local/lnmp/mysql
chown -R mysql.mysql ./
scripts/mysql_install_db --help
scripts/mysql_install_db --user=mysql --basedir=/usr/local/lnmp/mysql/ --datadir=/usr/local/lnmp/mysql/data/
#必须指定mysql安装目录与data目录
chown -R root ./
chown -R mysql data/
cp support-files/my-medium.cnf /etc/my.cnf
#把mysql的配置文件copy到/etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
#mysql的init脚本
vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
source ~/.bash_profile
/etc/init.d/mysqld start
mysql_secure_installation
#数据库使用配置
SUCCESS
PHP:
tar xzf libiconv-1.13.1.tar.gz
#加强系统对字符编码转换功能
cd libiconv-1.13.1
./configure --libdir=/usr/local/lib64/
make && make install
tar xjf libmcrypt-2.5.8.tar.bz2
#mcrypt,mhash用作加密功能
cd libmcrypt-2.5.8
./configure --libdir=/usr/local/lib64/
make && make install
cd libltdl/
./configure --libdir=/usr/local/lib64/ --enable-ltdl-install
make && make install
tar xjf mhash-0.9.9.9.tar.bz2
./configure --libdir=/usr/local/lib64/
make && make install
ldconfig /usr/local/lib64/
#ldconfig,configure dynamic linker run-time bindings,加载下库文件,不然安装mcrypt会报错
tar xzf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure --libdir=/usr/local/lib64/
make && make install
ldconfig /usr/local/lib64
#再次加载,不然php安装可能会失败
若提示/bin/rm: cannot remove `libtoolT': No such file or directory忽视此项,对PHP编译无影响
yum install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel gmp-devel openssl-devel readline-devel php-pear openldap-devel.i686 -y
tar xjf php-5.4.12.tar.bz2
cd php-5.4.12
./configure --prefix=/usr/local/lnmp/php --enable-fpm --with-fpm-user=php --with-fpm-group=php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-libxml-dir --with-pcre-dir --with-pear --with-zlib --with-curl --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-gettext --with-gmp --with-mhash --enable-mbstring --with-mcrypt --with-mysql=/usr/local/lnmp/mysql/ --with-readline --enable-inline-optimization --enable-sockets --enable-soap --with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config --with-ldap --with-ldap-sasl
#configure: error: Cannot find ldap libraries in /usr/lib
解决办法:yum install openldap-devel.i686 or yum install openldap-devel -y,cp -fr /usr/lib64/libldap* /usr/lib/
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/lnmp/php/etc/php.ini
#copy配置文件
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#将php-fpm添加为init程序
chmod +x /etc/init.d/php-fpm
#添加可执行权限
cd /usr/local/lnmp/php/etc/
cp php-fpm.conf.default php-fpm.conf
#php-fpm配置文件
vi php-fpm.conf
[global]
pid = run/php-fpm.pid
user = php
group = php
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
#得来有计算公式min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.max_spare_servers = 35
pm.max_requests = 500
useradd -M -s /sbin/nologin php
vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
source ~/.bash_profile
/etc/init.d/php-fpm start
#若提示没有libmcrypt库,ldconfig /usr/local/lib64(同时将/usr/local/lib64加入/etc/ld.so.conf新一行)
netstat -antple|grep 9000
#看9000端口是否开启,用来处理动态请求
SUCCESS
Nginx:
tar xzf nginx-1.2.7.tar.gz
cd nginx-1.2.7
vi src/core/nginx.h
#define NGINX_VERSION "1.2.7"
#define NGINX_VER "nginx/" NGINX_VERSION
#去掉NGINX_VERSION,即版本号,提升nginx的安全性
修改后为
#define NGINX_VERSION "1.2.7"
#define NGINX_VER "nginx/"
useradd -M -s /sbin/nologin nginx
yum install pcre-devel
./configure --prefix=/usr/local/lnmp/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
cd /usr/local/lnmp/nginx
vi conf/nginx.conf
user nginx nginx;
worker_processes 1;
#nginx开启的工作进程数
pid logs/nginx.pid;
#去掉注释
events {
use epoll;
#使用epoll事件模型
worker_connections 1024;
#总连接数=worker_processes*worker_connections
}
nginx -t
#测试配置文件是否成功
nginx
#开启nginx服务
nginx -s stop
#停止nginx服务
nginx -s reload
#重新加载nginx
EXAMPLE:
nignx默认发布目录/usr/local/lnmp/nginx/html
vi /usr/local/lnmp/nginx/conf/nginx.conf
nignx支持php动态网页
http {
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
#fastcgi使用本机9000端口处理动态网页请求
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
#将fastcgi_params改为fastcgi.conf
}
}
测试页index.php
<?php
phpinfo();
?>
浏览器访问http://nginxserver_IP/index.php得到php反馈页面即php安装即成功
nginx负载均衡
vi /
http {
upstream myproject {
#
server IP:port weight=number;
server 192.168.122.5;
server 192.168.122.4;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://myproject;
#用户访问www.test.com时,会跳转到myproject上进行服务器负载分配
}
}
phpMyAdmin:
#用于数据库管理
tar xjf phpMyAdmin-3.5.7-all-languages.tar.bz2 -C /usr/local/lnmp/nginx/html/
cd /usr/local/lnmp/nginx/html/
mv phpMyAdmin-3.5.7-all-languages/ php
cd php/
cp config.sample.inc.php config.inc.php
vi /usr/local/lnmp/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm index.php;
#添加对index.php的默认访问设置
}
浏览器访问http://nginxserver_IP/php/
输入mysql的用户名和密码
SUCCESS