CentOS/AWS编译安装LNMP环境

本文可看作AWS/CENTOS搭建LAMP环境的姊妹篇。

1、启用额外的库(参照racksapce,aws的默认ami库里面东西很全,基本上都可以yum到,可以忽略这部分)

centos5没有自带的nginx和php-fpm,因此从别的库拿来。导入RHEL EPEL

wget http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
sudo rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm

编辑remi的repo

sudo vim /etc/yum.repos.d/remi.repo

如下:

[remi]
name=Les RPM de remi pour Enterprise Linux $releasever - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority

后来我发现这样子的话安装的nginx版本太古老,安装remi库用于安装phpfpm,另外用nginx.repo安装最新版的nginx,方法

编辑

/etc/yum.repos.d/nginx.repo

如下

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/5/$basearch/
gpgcheck=0
enabled=1

2、安装

yum install mysql mysql-server nginx php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy php-pecl-apc

安装完之后

chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
chkconfig --levels 235 nginx on
/etc/init.d/nginx start
chkconfig --levels 235 php-fpm on
/etc/init.d/php-fpm start


3、配置 设置mysql

mysql_secure_installation

如下(里面有加粗斜体部分的enter,不知道为什么看不到,显示成了<strong><em>&lt;– ENTER</em></strong>,反正看到Y/N就enter,看到密码就输新密码)

[root@server1 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): <em><strong>&lt;-- ENTER</strong></em>
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] <strong><em>&lt;-- ENTER</em></strong>
New password: <em><strong>&lt;-- yourrootsqlpassword</strong></em>
Re-enter new password: <strong><em>&lt;-- yourrootsqlpassword</em></strong>
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] <em><strong>&lt;-- ENTER</strong></em>
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <strong><em>&lt;-- ENTER</em></strong>
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] <strong><em>&lt;-- ENTER</em></strong>
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] <strong><em>&lt;-- ENTER</em></strong>
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

[root@server1 ~]#

有的人会在启动php-fpm时,出现如下错误

[28-Nov-2011 08:11:01] ERROR: [pool www] cannot get uid for user 'apache'

解决办法:

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

找到如下两行,将apache修改为nginx

user = apache
group = apache

修改php.ini

vi /etc/php.ini

修改如下

cgi.fix_pathinfo=0 #原为1

修改nginx.conf,只是建议的数值,可以自行根据情况修改

vi /etc/nginx/nginx.conf
worker_processes  4;  #原为1

keepalive_timeout  2; #原为65

修改default.conf(有的没有default.conf,合并在nginx.conf里面),打开默认文件夹下的php权限:

server {
    listen       80;
    server_name  _;

    #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;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$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;
    }
}

重新导入nginx

/etc/init.d/nginx reload

然后

vi /usr/share/nginx/html/info.php

输入

<?php
phpinfo();
?>

就可以看到结果了

另外有的服务器默认没有开启gzip,编辑

vi /etc/nginx/nginx.conf

开启

gzip on;
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 1100;
gzip_comp_level 5;
gzip_buffers 8 16k;
gzip_types text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml;
gzip_vary on;
#gzip_disable “MSIE [1-6]\.”;

其他的eAccelerator,Memcache,PhpMyadmin之类请自行安装。

《CentOS/AWS编译安装LNMP环境》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.