LNMP编译安装
1.安装准备
1.1 先卸载之前yum安装的相关软件
#停止mysql服务
/etc/init.d/mysqld stop
#停止php-fpm服务
/etc/init.d/php-fpm stop
#删除相关rpm包
yum remove php* mysql*
#删除相关配置文件
rm -rf /var/lib/mysql /etc/my.cnf
1.2 安装编译工具和相关依赖
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 openldap-clients openldap-servers libXpm* pcre-devel gd-devel net-snmp-devel
1.3 下载依赖安装包
mkdir /tmp/source
cd /tmp/source
wget http://nginx.org/download/nginx-1.10.1.tar.gz
wget http://cn2.php.net/distributions/php-5.4.35.tar.bz2#php5.4
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.22.tar.gz
wget https://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmcrypt%2Ffiles%2FLibmcrypt%2F2.5.8%2F&ts=1418528625&use_mirror=tcpdiag
wget http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmhash%2Ffiles%2Fmhash%2F0.9.9.9%2F&ts=1418530194&use_mirror=hivelocity
wget http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmcrypt%2Ffiles%2FMCrypt%2F2.6.8%2F&ts=1418531968&use_mirror=softlayer-dal
wget http://zlib.net/zlib-1.2.8.tar.gz
1.4 安装libiconv,libmcrypt,mhash,bision
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/iconv #指定iconv安装目录
make && make install
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr/local
make && make install
2.编译安装mysql
2.1 安装cmake
wget https://cmake.org/files/v3.6/cmake-3.6.1.tar.gz
tar zxvf cmake-3.6.1.tar.gz
cd cmake-2.8.3/
./configure
make
make install
2.2 准备mysql&nginx用户,数据目录
useradd -r mysql
useradd -r nginx
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql
mkdir -p /data/www
chown -R nginx.nginx /data/www
2.3 安装mysql
tar zxvf mysql-5.6.13.tar.gz
cd mysql-5.6.13
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysql -DENABLE_DOWNLOADS=1
make && make install
mysql编译选项说明:
选项 | 功能 |
---|---|
-DCMAKE_INSTALL_PREFIX=dir_name | 设置mysql安装目录 |
-DMYSQL_UNIX_ADDR=file_name | 设置监听套接字路径,这必须是一个绝对路径名。默认为/tmp/mysql.sock |
-DDEFAULT_CHARSET=charset_name | 设置服务器的字符集。 缺省情况下,MySQL使用latin1的(CP1252西欧)字符集。cmake/character_sets.cmake文件包含允许的字符集名称列表。 |
-DDEFAULT_COLLATION=collation_name | 设置服务器的排序规则。 |
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 |
存储引擎选项: MyISAM,MERGE,MEMORY,和CSV引擎是默认编译到服务器中,并不需要明确地安装。静态编译一个存储引擎到服务器,使用-DWITH_engine_STORAGE_ENGINE= 1 可用的存储引擎值有:ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), 和PERFSCHEMA (Performance Schema) |
-DMYSQL_DATADIR=dir_name | 设置mysql数据库文件目录 |
-DMYSQL_TCP_PORT=port_num | 设置mysql服务器监听端口,默认为3306 |
-DENABLE_DOWNLOADS=bool | 是否要下载可选的文件。例如,启用此选项(设置为1),cmake将下载谷歌所使用的测试套件运行单元测试。 |
如果cmake过程出错,需要重新cmake需要删除CMakeCache.txt
rm CMakeCache.txt
2.4 初始化mysql
#basedir为mysql安装目录,datadir为数据库的数据文件存放目录
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/ --user=mysql
2.5 添加mysql服务到init脚本
cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysqld #拷贝服务控制脚本文件(确保拷贝过去的脚本有x权限)
#vim /etc/profile 将mysql库文件路径和bin路径假如PATH环境变量
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#应用配置文件
source /etc/profile
#mysql用户对安装目录有权限
chown -R mysql.mysql /usr/local/mysql/
2.6 修改mysql的root用户密码
mysqladmin -u root password 'redhat'
2.7 (可选)运行安全设置向导脚本,修改MySQL用户root的密码,同时可禁止root远程连接,移除test数据库和匿名用户。
/usr/local/mysql/bin/mysql_secure_installation
2.8(可选)设置配置文件
my.cnf的配置文件的默认读取顺序为:
File Name(上面的优先) | Purpose |
---|---|
/etc/my.cnf | Global options |
/etc/mysql/my.cnf | Global options |
SYSCONFDIR/my.cnf | Global options |
$MYSQL_HOME/my.cnf | Server-specific options |
defaults-extra-file | The file specified with --defaults-extra-file=path, if any |
~/.my.cnf | User-specific options |
~/.mylogin.cnf | Login path options |
简单配置如下:
[client] #客户端配置
port=3306
socket=/usr/local/mysql/mysql.sock
default-character-set = utf8
[mysqld]
port=3306
bind-address=127.0.0.1 #绑定地址
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock
user=mysql
############# default settings ################
# time zone
default-time-zone = system
character-set-server = utf8
default-storage-engine = InnoDB
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/data/mysqld/mysqld.pid
2.9 启动mysql服务
/etc/init.d/mysqld restart
chkconfig --add mysqld
chkconfig mysqld on
3.安装php
3.1编译
tar xf php-5.4.35.tar.gz
cd php-5.4.35
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysql=/usr/local/mysql/ --enable-mbstring --enable-soap --enable-bcmath --enable-zip --enable-calendar --enable-sockets --with-curl=/usr/local/ --with-gd --with-png-dir=/usr/local/ --with-freetype-dir=/usr/local/ --with-jpeg-dir=/usr/local/ --with-mhash=/usr/local/ --with-zlib-dir=/usr/local/ --with-mcrypt=/usr/local/ --with-libxml-dir=/usr/local --with-iconv=/usr/local/iconv
make && make install
#复制php的配置文件php.ini,此文件是php用于初始化解释器环境的配置文件
cp php.ini-development /usr/local/php/etc/php.ini
3.2 重命名php-fpm的配置文件
cd /usr/local/php/
mv etc/php-fpm.conf.default etc/php-fpm.conf
3.3 将php-fpm脚本添加到init.d目录
#脚本放置在php的编译目录
cd lnmp_src/php-5.5.38
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm
3.4 启动php-fpm
/usr/local/php/sbin/php-fpm &
#或者通过其init脚本启动
/etc/init.d/php-fpm restart
4.安装nginx
4.1 安装nginx的依赖库
yum install pcre-devel
yum install zlib-devel
4.2 编译安装nginx
#下载
wget http://nginx.org/download/nginx-1.10.1.tar.gz
#解压
tar xf nginx-1.10.1.tar.gz
./configure --prefix=/usr/local/nginx --with-pcre --user=nginx --group=nginx
make
make install
4.3 将nginx的执行路径加入PATH
vim /etc/profile
PATH=$PATH:/usr/local/nginx/sbin
export PATH
4.4 编辑nginx的配置文件,加入解析php相关配置
server {
listen 80;
server_name www.pl.com;
root /data/www; #定义文档根目录,$document_root变量将调用此值
location / {
index index.php;
}
location ~ .php$ {
root /data/www;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}