今天主要是把我在Amazon EC2使用Ubuntu架设LNMP的过程写下来,希望能对有同样需求的同学有所帮助。
这里的LNMP指的是Linux+Nginx+Mysql+Php。
对于LNMP的快速搭建,其实是有LNMP一键安装包,但是考虑到悲剧的EC2 Instance性能,使用LNMP一键安装包半天都编译不好,而且非常容易出错。所以在这里我使用的是在Ubuntu上安装包的方式搭建LNMP。
在Amazon EC2上我选择的Instance是一个Ubuntu 10.04的Instance(AMI ID:ami-2c0fa42d)。
因为我是Mac,所以直接使用的是Terminal运行下面这些命令,Win下可以用putty,Linux下使用终端就好了。
1.登录Amazon EC2
ssh -i ec2.pem [email protected] DNS
2.编辑源。(使用过Ubuntu的同学都知道,我们在安装完Ubuntu的时候都是要先编辑源,在这里也是这样。)
sudo vi /etc/apt/sources.list
lucid(10.04)的源添加下面的内容到sources.list
deb http://archive.ubuntu.com/ubuntu/ lucid main restricted
universe multiverse
deb http://archive.ubuntu.com/ubuntu/ lucid-security main
restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ lucid-updates main restricted
universe multiverse
deb http://archive.ubuntu.com/ubuntu/ lucid-backports main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ lucid main restricted
universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ lucid-security main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ lucid-updates main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ lucid-backports main
restricted universe multiverse
deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main
deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
因为官方源里可能一些包都是旧的,所以使用了第三方ppa源。
当然如果你是用的Ubuntu maverick(10.10),那就添加下面的源。
deb http://archive.ubuntu.com/ubuntu/ maverick main restricted
universe multiverse
deb http://archive.ubuntu.com/ubuntu/ maverick-security main
restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ maverick-updates main
restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ maverick-backports main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ maverick main restricted
universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ maverick-security main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ maverick-updates main
restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ maverick-backports main
restricted universe multiverse
deb http://ppa.launchpad.net/nginx/stable/ubuntu maverick main
3.更新源。(替换好源接着更新下源。)
sudo apt-get update
4.安装辅助工具 unzip unrar wget rsync。
sudo apt-get -y install unzip unrar wget rsync cron aptitude
5.安装 Nginx,Php,Mysql
sudo apt-get install nginx php5-common php5-dev php5-cgi
php5-fpm php-apc php5-mysql php5-curl php5-gd php5-idn php-pear
php5-mcrypt php5-memcache php5-ming php5-recode php5-tidy
php5-xmlrpc php5-xsl mysql-server
6.创建网站根目录(在这里我们把网站根目录放在 /home/www 下)
sudo mkdir /home/www
7.更改网站目录权限(用户名为ubuntu)
sudo chown -R ubuntu /home/www
8.修改nginx配置文件
sudo vi /etc/nginx/sites-enabled/default
把其中的:
server {
#listen 80; ## listen for ipv4; this line is default and
implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www;
index index.html index.htm;
替换成:
server {
#listen 80; ## listen for ipv4; this line is default and
implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/www;
index index.php index.html index.htm;
其中的:
#location ~ .php$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi_params;
#}
替换成:
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}
9.安装phpmyadmin管理Mysql数据库。
wget -c http://cdnetworks-kr-1.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.4.3.2/phpMyAdmin-3.4.3.2-all-languages.zip
unzip phpMyAdmin-3.4.3.2-all-languages.zip
mv phpMyAdmin-3.4.3.2-all-languages /home/www/phpmyadmin
cd /home/www/phpmyadmin
cp config.sample.inc.php config.inc.php
vi config.inc.php
将其中的:
$cfg['blowfish_secret'] = ”;
改为:
$cfg['blowfish_secret'] = ‘web’;
里面的代码:
// $cfg['Servers'][$i]['controluser'] = ‘pma’;
// $cfg['Servers'][$i]['controlpass'] = ‘pmapass’;
// $cfg['Servers'][$i]['pmadb'] = ‘phpmyadmin’;
// $cfg['Servers'][$i]['bookmarktable'] = ‘pma_bookmark’;
// $cfg['Servers'][$i]['relation'] = ‘pma_relation’;
// $cfg['Servers'][$i]['table_info'] = ‘pma_table_info’;
// $cfg['Servers'][$i]['table_coords'] = ‘pma_table_coords’;
// $cfg['Servers'][$i]['pdf_pages'] = ‘pma_pdf_pages’;
// $cfg['Servers'][$i]['column_info'] = ‘pma_column_info’;
// $cfg['Servers'][$i]['history'] = ‘pma_history’;
// $cfg['Servers'][$i]['tracking'] = ‘pma_tracking’;
// $cfg['Servers'][$i]['designer_coords'] =
‘pma_designer_coords’;
// $cfg['Servers'][$i]['auth_swekey_config'] =
‘/etc/swekey-pma.conf’;
全部删除//,然后将其中的:
$cfg['Servers'][$i]['controluser'] = ‘pma’;
$cfg['Servers'][$i]['controlpass'] = ‘pmapass’;
pma对应的是mysql用户名,pmapass对应的是mysql密码。
10.重启服务
sudo service nginx restart //重启nginx
sudo service php5-fpm restart //重启fastcgi进程
sudo service mysql restart //重启mysql进程
至此,Amazon EC2的LNMP就安装完毕了。