且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Centos 6.4编译安装LNMP

更新时间:2022-06-04 10:52:56

一、系统版本与所安装软件版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@www ~]# uname -a
Linux www.test.com.cn 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@www ~]# mkdir Downloads
[root@www ~]# mkdir nginx
[root@www Downloads]# cd nginx/
[root@www nginx]# pwd
/root/Downloads/nginx
[root@www nginx]# wget http://nginx.org/download/nginx-1.4.0.tar.gz
--2013-09-03 17:11:41--  http://nginx.org/download/nginx-1.4.0.tar.gz
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 773451 (755K) [application/octet-stream]
Saving to: ?.ginx-1.4.0.tar.gz?
[root@www php]# pwd
/root/Downloads/php
[root@www php]# wget http://cn2.php.net/get/php-5.4.14.tar.gz/from/this/mirror
--2013-09-03 17:18:22--  http://cn2.php.net/get/php-5.4.14.tar.gz/from/this/mirror
Resolving cn2.php.net... 220.181.136.36220.181.136.37
Connecting to cn2.php.net|220.181.136.36|:80... failed: Connection timed out.
Connecting to cn2.php.net|220.181.136.37|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://cn2.php.net/distributions/php-5.4.14.tar.gz [following]
--2013-09-03 17:19:45--  http://cn2.php.net/distributions/php-5.4.14.tar.gz
Connecting to cn2.php.net|220.181.136.37|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15773155 (15M) [application/x-gzip]
Saving to: ?.hp-5.4.14.tar.gz?

mysql

1
2
3
4
5
6
7
8
[root@www mysql]# cd /root/Downloads/mysql/
[root@www mysql]# wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.13.tar.gz
--2013-09-03 17:55:30--  http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.13.tar.gz
Resolving cdn.mysql.com... 216.156.211.64216.156.211.17
Connecting to cdn.mysql.com|216.156.211.64|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35951160 (34M) [application/x-tar-gz]
Saving to: ?.ysql-5.6.13.tar.gz?

   注:自从mysql 5.5.8之后,MySQL的源码包编译安装都要用到cmake来进行编译了,编译的过程没有本质的区别,但是要直观很多.

二、安装nginx  

   1、安装前提:

1
2
[root@www nginx]# yum remove httpd php*
#移除己安装的httpd与php包


1
2
[root@www nginx]# yum groupinstall "Development Tools" -y
#安装Development Tools

   2、解压、编译安装nginx  

1
2
3
4
#到下载nginx的目录
[root@www nginx]# tar -zvxf nginx-1.4.0.tar.gz
[root@www nginx]# cd nginx-1.4.0
[root@www nginx-1.4.0]# ./configure  --prefix=/opt/nginx/

   3、如果在编译过程有如下提示需要安装:pcre pcre-devel

1
2
3
4
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

   4、安装pcre包

1
[root@www nginx-1.4.0]# yum install pcre pcre-devel -y

   5、重新编译安装  

1
2
3
[root@www nginx-1.4.0]# ./configure  --prefix=/opt/nginx/
#编译成功后
[root@www nginx-1.4.0]#make && make install

   6、启动nginx

1
2
[root@www nginx-1.4.0]# cd /opt/nginx/sbin
[root@www sbin]# ./nginx

   7、测试nginx    

1
2
3
4
5
[root@www sbin]# elinks http://127.0.0.1
#如果系统中没有elinks需要安装:
elinks-0.12-0.21.pre5.el6_3.x86_64.rpm
nss_compat_ossl-0.9.6-1.el6.x86_64.rpm
[root@www sbin]#yum install elink* -y

===================================到此nginx安装完毕=====================================

三、安装msyql

   1、安装所需的库文件

1
2
3
[root@www mysql]# pwd
/root/Downloads/mysql
[root@www mysql]# yum install -y gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel*

   2、编译步骤

1
2
3
4
5
[root@www mysql]# mkdir -p /usr/local mysql
[root@www mysql]# mkdir -p /data/mysql
[root@www mysql]# groupadd mysql
[root@www mysql]# useradd -g mysql mysql
[root@www mysql]# chown -R mysql.mysql /data/mysql

   3、安装cmake

1
[root@www mysql]#yum install -y cmake

   4、解压、编译mysql       

1
2
[root@www mysql-5.6.13]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[roo@www mysql-5.6.13]#make &&make install

   参数说明:
       -DCMAKE_INSTALL_PREFIX=/usr/local/mysql //安装目录

       -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock //指定sock文件所在的目录
       -DMYSQL_DATADIR=/data/mysql //数据库存放目录
       -DWITH_MYISAM_STORAGE_ENGINE=1 //安装myisam存储引擎
       -DWITH_INNOBASE_STORAGE_ENGINE=1 //安装innodb存储引擎
       -DWITH_MEMORY_STORAGE_ENGINE=1 //安装memory存储引擎                     

       -DWITH_READLINE=1 //快捷键功能        

       -DENABLED_LOCAL_INFILE=1 //允许从本地导入数据
       -DDEFAULT_CHARSET=utf8   //使用utf8字符
       -DDEFAULT_COLLATION=utf8_general_ci //校验字符
       -DEXTRA_CHARSETS=utf8,gbk  //安装utf8,gbk扩展字符集
       -DMYSQL_TCP_PORT=3306 //MySQL监听端口
       -DMYSQL_USER=mysql //MySQL用户名    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@www mysql]# pwd
 /usr/local/mysql
[root@www mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@www mysql]#chmod 755 scripts/mysql_install_db
[root@www mysql]#scripts/mysql_install_db  --user=mysql  --basedir=/usr/local/mysql --datadir=/data/mysql/    #初始化数据库
[root@www mysql]# cp support-files/mysql.server /etc/init.d/mysql   #设置开机启动mysql
[root@www mysql]#chmod 755 /etc/init.d/mysql
[root@www mysql]#chkconfig mysql on     #开机启动
[root@www mysql]#echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile        #将mysql的安装路径加入PATH中,方便启动mysql(重启生效)
[root@www mysql]#/etc/init.d/mysql start     #启动mysql
#可以在进程中查看mysql是否启动
[root@www mysql]#ps aux|grep mysql
#完成。
#设置一个mysql的root密码
[root@www mysql]#mysqlamdin -u root password 'mysql'

   在这里我出现过一个问题:删除mysql 重新安装后会有一些错误。在编译完mysql后启动时会提示
   Starting MySQL..The server quit without updating PID file (/data/mysql/www.centostest.com.cn.pid)

   注:本人是重新编译安装就没有出现此问题,有那一个大神知道具体是什么原因,请指点!
   附:

       具体解决方法请参照:
http://blog.rekfan.com/articles/186.html

1
2
3
4
5
6
7
8
#kill 掉mysql的进程
[root@www mysql]# ps aux |grep mysql
root      1694  0.0  0.1 108332  1508 ?        S    17:21   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/www.centostest.com.cn.pid
mysql     1809  0.1 44.8 1071940 454008 ?      Sl   17:21   0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/www.centostest.com.cn.err --pid-file=/data/mysql/www.centostest.com.cn.pid
root      2299  0.0  0.0 103248   836 pts/0    S+   18:07   0:00 grep mysql
[root@www mysql]#kill -9 1694
#重新启动mysql即可
[root@www mysql]#/etc/init.d/mysql start

============================到此mysql安装完毕=========================================

三、安装php

   1、解压

1
2
3
[root@www php]# pwd
/root/Downloads/php
[root@www php]# tar -zxvf php-5.4.14.tar.gz

   2、安装依赖包  

1
[root@www php]# yum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel libmcrypt-devel

   3、编译  

1
2
3
[root@www php]# ls
php-5.4.14  php-5.4.14.tar.gz
[root@www php-5.4.14]#./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic

     在此遇到configure: error: mcrypt.h not found. Please reinstall libmcrypt.问题

      解决方法:        

1
2
3
4
5
[root@www php-5.4.14]# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
[root@www php-5.4.14]#tar -zxvf libmcrypt-2.5.8.tar.gz
[root@www php-5.4.14]#cd libmcrypt-2.5.8
root@www libmcrypt-2.5.8]#./configure
root@www libmcrypt-2.5.8]#make && make install

   之后再重新编译,完成  

1
2
3
4
5
[root@www libmcrypt-2.5.8]# cd ..
[root@www php-5.4.14]#pwd
/root/Download/php/php-5.4.14
[root@www php-5.4.14]# make && make install
#安装时间有点长。

   编译php出错:

 1、error while loading shared libraries: libmysqlclient.so.18: cannot open shared object    网上找到的解决办法是
       适用于32位系统
           [root@www php-5.4.14]#ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib/
       64位系统应使用下面
           [root@www php-5.4.14]#ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib64/

   2、 cp: cannot stat `ext/phar/phar.phar': No such file or directory
           [root@www php-5.4.14]#cd  ext/phar/

           [root@www phar]# cp ./phar.php  ./phar.phar

   [root@www php-5.4.14]# make && make install

   安装成功后,php的位置就应该在/opt/php目录下,接下去就是配置 

1
2
[root@www php-5.4.14]# cp /root/Downloads/php/php-5.4.14/php.ini-production /opt/php/etc/php.ini
[root@www php-5.4.14]# cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
1
2
3
4
5
6
7
8
9
10
11
[root@www php-5.4.14]# cd /opt/php/etc
[root@www etc]# ls
php-fpm.conf  php-fpm.conf.default  php.ini
[root@www etc]# cd /opt/nginx/conf
#然后配置nginx的配置文件,让其可以运行php
[root@www conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@www conf]# vim nginx.conf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#1、去掉 #user  nobody;  的#号, 变成 user  nobody;
#2、去掉
#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;
    #}
#之前的#号,并修改fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;为fastcgi_param  SCRIPT_FILENAME  /opt/nginx/html$fastcgi_script_name;最终修改后的内容为:
location ~ \.php$ {        
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /opt/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

   4、重启nginx服务,并开启php-fpm

1
2
3
4
5
6
7
8
[root@www conf]# vim nginx.conf
[root@www conf]# /opt/nginx/sbin/nginx
[root@www conf]# /opt/php/sbin/php-fpm
[root@www conf]# vim /opt/nginx/html/phpinfo.php
#内容为
    <?php
    phpinfo();
?>

       访问http://127.0.0.1/phpinfo.php

Centos 6.4编译安装LNMP

======================到此LNMP安装成功==========================================










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