LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
实验环境:
系统环境: RHEL6 x86-64 selinux and iptables disabled
一、Mysql 安装
1.安装软件包依赖性:
[[email protected] ~]# yum install -y gcc gcc-c++ ncurses-devel bison openssl-devel zlib-devel
[[email protected] ~]# yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm 用cmake[有进度条]
2.编译安装数据库
[[email protected] ~]# tar zxf mysql-boost-5.7.11.tar.gz
[[email protected] ~]# cd mysql-5.7.11/
[[email protected] mysql-5.7.11]# 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 \ #Unix socket 文件路径
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎
> -DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎
> -DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
> -DDEFAULT_COLLATION=utf8_general_ci \ #校验字符
> -DEXTRA_CHARSETS=all \ #安装所有扩展字符集
> -DWITH_BOOST=/root/mysql-5.7.11/boost/boost_1_59_0/
[[email protected] mysql-5.7.11]# make && make install
注意:
重新编译时,需要清除旧的对象文件和缓存信息
make clean
rm -f CmakeCache.txt
3.完成mysql初始化
[创建所需的用户及组]
[[email protected] mysql]# groupadd -g 27 mysql
[[email protected] mysql]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql
[[email protected] local]# usermod -s /sbin/nologin mysql
[[email protected] mysql]# chown mysql.mysql . -R
4.解析mysql/bin的执行脚本
[[email protected] ~]# vim .bash_profile
[[email protected] ~]# source .bash_profile
[[email protected] mysql]# chown root . -R
5.修改配置文件中的数据库存放目录socket 文件路径与编译时一致
[[email protected] support-files]# vim /etc/my.cnf
6.拷贝执行脚本到本地执行目录下
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld
7.完成初始化 指定用户 并创建数据库
[[email protected] mysql]# mysqld --initialize --user=mysql
[[email protected] mysql]# chown mysql data -R 更改数据目录所有人为mysql
开启数据库 并修改密码
[[email protected] mysql]# /etc/init.d/mysqld start
[[email protected] mysql]# mysql_secure_installation #按提示完成 mysql 安全设置,生产环境推荐使用
二、Php 安装
1.解决安装所需依赖性
下载包
php-5.6.20.tar.bz2
安装依赖性
[[email protected] ~]# yum install libjpeg-turbo-devel-1.2.1-1.el6.x86_64
[[email protected] php-5.6.20]# yum install -y libxml2-devel
[[email protected] php-5.6.20]# yum install -y openssl-devel
[[email protected] php-5.6.20]# yum install -y curl-devel
[[email protected] ~]# yum install -y gd-devel-2.0.35-11.el6.x86_64.rpm
[[email protected] ~]# yum install -y gmp-devel
[[email protected] ~]# yum install libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm -y
[[email protected] ~]# yum install -y net-snmp-devel
[[email protected] ~]# yum install re2c-0.13.5-1.el6.x86_64.rpm -y
2.编译安装
[[email protected] ~]# tar jxf php-5.6.20.tar.bz2 解包
[[email protected] php-5.6.20]# ./configure --help 寻找帮助
--with-openssl 指定驱动 加密
--with-gd 图
--with-zlib php网页压缩(防盗链)
--with-pear 功能模块的组件(安装)
执行
[[email protected]
php-5.6.20]# ./configure --prefix=/usr/local/lnmp/php
--with-config-file-path=/usr/local/lnmp/php/etc --enable-mysqlnd
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
--with-openssl --with-snmp --with-gd --with-zlib --with-curl
--with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir
--with-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
--with-mhash
输出的信息
Generating filesconfigure: creating ./config.statuscreating main/internal_functions.ccreating main/internal_functions_cli.c+--------------------------------------------------------------------+| License: || This software is subject to the PHP License, available in this || distribution in the file LICENSE. By continuing this installation || process, you are bound by the terms of this license agreement. || If you do not agree with the terms of this license, you must abort || the installation process at this point. |+--------------------------------------------------------------------+Thank you for using PHP.
[[email protected] php-5.6.20]# make && make install
3.配置php解析文件/配置php-fpm配置文件
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
[[email protected] etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default
[[email protected] etc]# pwd
/usr/local/lnmp/php/etc
[[email protected] php-5.6.20]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[[email protected] fpm]# pwd
/root/php-5.6.20/sapi/fpm
[[email protected] fpm]# cp init.d.php-fpm /etc/init.d/php-fpm 启动脚本
[[email protected] fpm]# chmod +x /etc/init.d/php-fpm 加执行权限
4.修改php配置文件 [pid][timezone]
[[email protected] etc]# vim php-fpm.conf
[[email protected] etc]# vim php.ini
添加php配置文件中指定的用户 并启动php
[[email protected] etc]# useradd -u 800 -M -d /usr/local/lnmp/nginx nginx
[[email protected] etc]# /etc/init.d/php-fpm start
三、Nginx 安装
1.安装依赖性并编译
[[email protected] ~]# tar zxf nginx-1.10.1.tar.gz
[[email protected] ~]# cd nginx-1.10.1
[[email protected] core]# pwd
/root/nginx-1.10.1/src/core
[[email protected] core]# vim nginx.h
#define NGINX_VER "nginx" (修改此行, 去掉后面的 “ NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号)
[[email protected] cc]# pwd
/root/nginx-1.10.1/auto/cc
[[email protected] cc]# vim gcc
#CFLAGS=”$CFLAGS -g” (注释掉这行,去掉 debug 模式编译,编译以后程序只有几百 k)
安装依赖
[[email protected] nginx-1.10.1]# yum install -y pcre-devel
编译
./configure
--prefix=/usr/local/lnmp/nginx --with-http_ssl_module
--with-http_stub_status_module --with-file-aio --with-threads
--user=nginx --group=nginx
安装
[[email protected] nginx-1.10.1]# make && make install
做软链接
[[email protected] sbin]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
[[email protected] sbin]# nginx -t 检测语法
[[email protected] ~]# nginx 开启
四、解决nginx和php的依赖关系
编辑nginx配置文件 修改tcp zip php
[[email protected] conf]# pwd
/usr/local/lnmp/nginx/conf
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# nginx -t 检测语法
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[[email protected] conf]# nginx -s reload 重新加载
创建nginx访问php主页的页面
[[email protected] html]# pwd
/usr/local/lnmp/nginx/html
[[email protected] html]# vim index.php
[[email protected] html]# cat index.php
<?php
phpinfo()
?>
设置index.php为访问ip时默认访问的网页
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# nginx -t
[[email protected] conf]# nginx -s reload
浏览器访问
五、解决php和mysql的依赖关系
[[email protected] etc]# pwd
/usr/local/lnmp/php/etc
[[email protected] etc]# vim php.ini
[[email protected] etc]# /etc/init.d/php-fpm reload 平滑加载
六、搭建基于lnmp的论坛
下载 Discuz_X3.2_SC_UTF8.zip
[[email protected] ~]# yum install -y unzip
[[email protected] ~]# unzip Discuz_X3.2_SC_UTF8.zip 解压
修改解压出的目录名字及位置
[[email protected] ~]# mv upload/ /bbs
ngnx设置虚拟主机 实现基于域名访问nginx
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# nginx -s reload
注意:浏览器访问主机做好解析,/etc/hosts
访问:bbs.cara.org/install
根据提示 改权限
[[email protected] bbs]# chmod 777 config/ data
[[email protected] bbs]# chmod 777 config/ data -R
[[email protected] bbs]# chmod 777 uc_client/ uc_server/ -R
[[email protected] mysql]# chmod 755 data
[root@server1 ~]# /etc/init.d/mysqld restart
如下,已经完成!
查看数据库:
七、memcache 基于php的cache
1.安装memcache
修改环境变量
[root@server1 bin]# vim ~/.bash_profile
[root@server1 bin]# source ~/.bash_profile
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
编译安装memcache
[root@server1 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache
[root@server1 memcache-2.2.5]# make && make install
[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
[root@server1 etc]# vim php.ini
[root@server1 etc]# /etc/init.d/php-fpm reload 重新加载
拷贝文件
[root@server1 memcache-2.2.5]# cp example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cp memcache.php /usr/local/lnmp/nginx/html/
2.搭建一个memcache服务器
[root@server2 ~]# yum install -y memcached
[root@server2 ~]# /etc/init.d/memcached start 开启服务
[root@server2 ~]# yum install -y telnet
telnet命令用于登录远程主机,对远程主机进行管理。telnet采用明文传送报文,安全性不好;
[root@server1 html]# vim example.php
[root@server1 html]# vim memcache.php
(client -> nginx -> php-fpm -> php -> memcached -> mysql)
原文地址:http://blog.51cto.com/13362895/2140637