CentOS Linux yum方式安装LNMP

===== 151213 更新:由于alt.ru源关掉了,改回官方源 =====

Nginx有官方源后,安装起来方便很多。使用alt.ru源可以一次性安装nginx+php+mysql。系统用的是CentOS6.2。

安装完后的软件版本(版本根据时间不同会有更新):
Nginx 1.8.0
PHP 5.3.3
MySQL 5.1.73

一、安装nginx
1、执行

rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx

yum安装的版本是nginx.i386 0:1.8.0-1.el6.ngx,是目前最新的稳定版。

2、设置开机自启动:

chkconfig --add nginx
chkconfig nginx on

3、启动nginx:

service nginx start

这里不使用nginx官方源了,因为官方源的nginx不能搭配alt.ru源的php、mysql使用。

二、安装nginx和php
使用fastcgi方式安装php。

1、导入软件库:

vim /etc/yum.repos.d/alt.ru.repo

加入:

[CentALT]
name=CentALT Packages for Enterprise Linux 6 - $basearch
baseurl=http://centos.alt.ru/repository/centos/6/$basearch/
enabled=1
gpgcheck=0

启用 EPEL repo

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm

有两个软件包需要从这个源下:GeoIP、libmcrypt

2、安装nginx、php-fpm、mysql及相关软件包:

yum install nginx php-fpm php-cli php-mysql php-bcmath php-gd php-imap php-mbstring php-mcrypt php-soap php-tidy php-xml php-xmlrpc php-pdo php-ldap php-snmp mysql mysql-server

3、设置开机自启动:

chkconfig nginx on
chkconfig php-fpm on

4、修改/etc/php-fpm.d/www.conf:

vim /etc/php-fpm.d/www.conf

找到:

user = apache
group = apache

修改为:

user = nginx
group = nginx

5、启动php-fpm:

service php-fpm start

6、启动nginx:

service nginx start

三、安装mysql
1、执行

yum install mysql mysql-server

1.1、配置my.cnf
MySQL5.5开始默认使用InnoDB引擎,为了节约内存可以用回MySQL5.1时代的MyISAM引擎,不过内存足够的话就不用这样做了。

vim /etc/my.cnf

在[mysqld]下面加入:

loose-skip-innodb
default-storage-engine = MyISAM

2、设置开机自启动:

chkconfig mysqld on

3、启动mysql:

service mysqld start

4、设置mysql root用户密码及其它设置:

/usr/bin/mysql_secure_installation

默认密码为空,所以直按回车,然后输入mysql root密码。

四、添加nginx fastcgi支持
1、执行:

vim /etc/nginx/nginx.conf

在最后一行”}”前加入以下内容:

    server {
        listen       80;
        server_name  www.aaa.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }
        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


server_name后填IP或域名。如果服务器有多个IP,要实现访问IP为默认页面,就建立多个server段server_name填IP。
/usr/share/nginx/html为nginx的根目录。

2、重启nginx php-fpm

service nginx restart
service php-fpm restart

3、建立phpinfo.php
vim /usr/share/nginx/html/phpinfo.php

输入以下内容:

<?php
phpinfo();
?>

浏览器访问:http://IP或域名/phpinfo.php,查看是否有phpinfo信息显示。

参考资料:
http://phpcj.org/blog/centos6-nginx-mysql-php-fpm/
http://www.centos.bz/2011/03/yum-install-nginx-mysql-php-fastcgi-lnmp/
http://hi.baidu.com/zq500480/blog/item/116fd54e59eb4504b3de0599.html
更新源的支持论坛? http://forums.famillecollet.com/index.php