该文档为制作LNMP一键安装脚本而写,命令比较少注释,基本复制粘贴一步到底,也是为方便大家制作一键安装脚本。
系统环境:centos5.8 64位
1、Nginx安装
1.1 安装nginx所需的pcre库
pcre全称(Perl CompatibleRegular Expressions),安装pcre库是为了使nginx支持http rewrite模块
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
tar zxf pcre-8.30.tar.gz
cd pcre-8.30
./configure
make && make install
1.2 安装nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.2.9.tar.gz
useradd nginx -s /sbin/nologin -M
tar zxf nginx-1.2.9.tar.gz
cd nginx-1.2.9
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.2.9 --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /application/nginx-1.2.9 /application/nginx
ll /application/nginx
echo /usr/local/lib >>/etc/ld.so.conf
ldconfig
/application/nginx/sbin/nginx -t
#检查语法
/application/nginx/sbin/nginx
#启动nginx
1.3 测试nginx
2、MySQL安装
2.1 安装mysql软件
wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.1/mysql-5.1.68.tar.gz
user add -s /sbin/nologin -M mysql
tar zxf mysql-5.1.68.tar.gz
cd mysql-5.1.68
--prefix=/application/mysql5.1.68 \
--with-unix-socket-path=/application/mysql5.1.68/tmp/mysql.sock \
--localstatedir=/application/mysql5.1.68/data \
--enable-assembler \
--enable-thread-safe-client \
--with-mysqld-user=mysql \
--with-big-tables \
--without-debug \
--with-pthread \
--enable-assembler \
--with-extra-charsets=complex \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static
make && make install
cp support-files/my-small.cnf /etc/my.cnf
#Mysql配置文件,默认存放在support-files路径下,但mysql调用的配置文件为/etc/my.cnf
ln -s /application/mysql5.1.68 /application/mysql
2.2 初始化mysql数据库
mkdir -p /application/mysql/data
chown -R mysql /application/mysql
/application/mysql/bin/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
#初始化mysql数据文件,--basedir为数据库目录 --datadir为创建数据库和表的存放地
2.3 启动mysql
方法一:
cp support-files/mysql.server /application/mysql/bin/
chmod 700 /application/mysql/bin/mysql.server
/application/mysql/bin/mysql.server start
/application/mysql/bin/mysql.server stop
方法二:
/application/mysql/bin/mysqld_safe --user=mysql &
方法三:
cp support-files/mysql.server /etc/init.d/mysql
chmod 700 /etc/init.d/mysql
/etc/init.d/mysql start
2.4 mysql测试
mysqladmin -u root password'123456'
mysql -uroot -p123456 -e "select version();"
3、PHP安装
首先需安装php相应的库文件
3.1 检查PHP所需lib库
rpm -qa zlib libxml libjpeg freetype libpnggd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
如果安装操作系统时未选择X software development软件包,则可能没有上述lib库,可以通过下面命令补装
yum groupinstall "X Software Development"
3.2 安装libiconv库
cd /usr/local/src
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
cd ../
3.3 安装libmcrypt库
cd /usr/local/src
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
tar zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
sleep 2
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
3.4 安装mhash加密扩展库
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
tar zxf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
sleep 2
cd ../
这儿为32位系统的库文件链接
rm -f /usr/lib/libmcrypt.*
rm -f /usr/lib/libmhash*
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
这儿为64位系统的库文件链接
rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s/usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
3.5 安装mcrypt加密扩展库
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
tar zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
sleep 2
3.6 安装php
php5.2版本需要安装php-fpm补丁
php5.3以后版本,源码中已经内嵌了 php-fpm,不需要安装补丁包,只需要在configure的时候添加编译参数即可,
wget http://cn2.php.net/get/php-5.3.27.tar.gz/from/this/mirror
tar zxf php-5.3.27.tar.gz
cd php-5.3.27
./configure \
--prefix=/application/php5.3.27 \
--with-mysql=/application/mysql \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safa-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libevent-dir \
--enable-ftp
make && make install
ln -s /application/php5.3.27/ /application/php
/bin/cp /usr/local/src/php-5.3.27/php.ini-production /application/php5.3.27/lib/php.ini
#把默认生产配置文件做为PHP配置文件
cp /application/php/etc/php-fpm.conf.default /application/php/etc/php-fpm.conf
#把默认php-fpm.conf.default进程管理文件改名为php/etc/php-fpm.conf
/application/php/sbin/php-fpm -t
#校验语法
/application/php/sbin/php-fpm
#启动php
4、Nginx关联PHP
4.1 修改nginx相关配置
修改nginx配置(该文件为nginx默认配置文件,未修改过),在47行位置,插入下列内容
35 server {
36 listen 80;
37 server_name localhost;
38
39 #charset koi8-r;
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
47 location~ \.(php|php5)?$ {
48 fastcgi_pass 127.0.0.1:9000;
49 fastcgi_index index.php;
50 include fastcgi.conf;
51 }
站点目录为默认路径,在站点目录添加php配置
[root@yangrongnginx]# cat >/application/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
#上面为php长标签格式,短标签格式为(短标签默认不打开):
#<?
#phpinfo();
#?>
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
4.2 测试PHP连接
在浏览器输入IP/index.php
5、Nginx关联MySQL
5.1连接数据库配置
[root@yangrongnginx]# cat >/application/nginx/html/mysql.php <<EOF
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('127.0.0.1','root','123456') or mysql_error();
//$link_id=mysql_connect('127.0.0.1','test','');
if($link_id){
echo "mysql connect issuccessful!";
}else{
echo mysql_error();
}
//是php单行注释
/* 这是php多行注释*/
?>
EOF
注意连接数据库的用户名和密码是否正确
5.2 测试
在浏览器输入IP/mysql.php