LNMP架构部署
LNMP源码安装
源码包版本
nginx-0.7.65
pcre-8.02
mysql-5.1.53-linux-i686-glibc23.tar.gz
php-5.2.13.tar.gz
php-5.2.13-fpm-0.5.13.diff.gz
下载软件包到/bak/software目录下
1.安装ngxin
安装nginx之前,确保系统已经安装了gcc、openssl-devel、pcre-devel和zlib-devel软件库
安装nginx所需的pcre-devel库
tar zxvf pcre-8.02.tar.gz
cd pcre-8.02
./configure
make
make install
tar zxvf nginx-0.7.65.tar.gz
cd nginx-0.7.65
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_gzip_static_module --without-http_rewrite_module --with-pcre=/bak/software/pcre-8.02
make
make install
2.安装mysql免安装版
2.1、新建 mysql 组和用户;
groupadd mysql
useradd -g mysql mysql
2.2、用 root 用户给 mysql 安装路径授权,依次执行:
MYSQL_BASE=/opt/mysql
mkdir -p $MYSQL_BASE/mysqldata/mysqllog
mkdir -p /var/run/mysqld/
chmod -R 777 $MYSQL_BASE
chown -R mysql:mysql $MYSQL_BASE
chown -R mysql:mysql /var/run/mysqld/
注:以上路径是我准备安装 mysql 的路径,部分路径与实际环境相关
2.3、切换到mysql 用户,设置 mysql 用户环境变量:编辑 /home/mysql/.bash_profile 文件,增加如下内容:
su - mysql
vim .bash_profile
export MYSQL_BASE=/opt/mysql
export DATADIR=$MYSQL_BASE/mysqldata
export LD_LIBRARY_PATH=$BASEDIR/lib:/lib:/usr/lib:/usr/local/lib
export TMPDIR=/tmp
export PATH=$PATH:/usr/bin:/bin:/usr/bin/X11:/usr/local/bin
export PATH=$PATH:$BASEDIR/bin:$MYSQL_BASE:$BASEDIR:$LD_LIBRARY_PATH
2.4、将安装包解压到/opt/mysql目录下,依次执行:
cd /bak/software
tar zxvf mysql-5.1.53-linux-i686-glibc23.tar.gz -C /opt/mysql
2.5、开始安装 mysql:
1) 执行:
cd /opt/mysql
./scripts/mysql_install_db --basedir=$BASEDIR --datadir=$DATADIR --user=mysql
2) 切换到 root 用户, 依次执行:
su - root
cd /opt/mysql/
cp ./support-files/my-medium.cnf /etc/my.cnf
cp ./support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
3) 编辑 /etc/my.cnf 文件,找到如下片段:
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
在下面增加:
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
basedir = /opt/mysql
datadir = /opt/mysql/mysqldata
lower_case_table_names = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2G
innodb_data_file_path = ibdata1:1000M:autoextend
innodb_autoextend_increment = 64
innodb_file_per_table = 1
innodb_data_home_dir = /opt/mysql/mysqldata
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 40
innodb_lock_wait_timeout = 120
innodb_locks_unsafe_for_binlog = 1
innodb_autoinc_lock_mode = 2
修改 [client] 和 [mysqld] 下的 socket 那行为:
socket = /var/run/mysqld/mysqld.sock
注:Linux环境是区分大小写的,所以要加 lower_case_table_names = 1 这个配置
4) 启动mysql, 用 mysql 用户执行: /etc/init.d/mysql start 显示:
Starting MySQL........................................................[ 确定 ]
则表示启动成功。
5)设置数据库root用户密码:
/opt/mysql/bin/mysqladmin -u root password 'oracle'
/opt/mysql/bin/mysqladmin -u root -p -h localhost.localdomain password 'oracle'
3.安装php
安装php需要下面软件包的支持,如果没有安装,请自行安装
gcc gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc
glibc-devel glib2 glib2-devel
开始编译安装PHP和PHP-FPM
cd /bak/software
tar zxvf php-5.2.13.tar.gz
gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1
cd php-5.2.13
./configure --prefix=/opt/php --enable-fastcgi --enable-fpm --with-mysql=/opt/mysql
make
make install
cp php.ini-dist /opt/php/lib/php.ini
修改php-fpm.conf配置文件
vim /opt/php/etc/php-fpm.conf
将标签user和group地方注释到
启动PHP
/opt/php/sbin/php-fpm start
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
4.nginx与php整合
vim /opt/nginx/nginx.conf
#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;
#}
去掉注释
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;
}
启动nginx服务
/opt/nginx/sbin/nginx
5.php与nginx测试页、php连接mysql测试页
cd /opt/nginx/html
vim test.php
vim index.php
$link=mysql_connect(localhost,root,mysql);
if(!$link) echo "失败!"; else echo "成功!";
mysql_close();
?>
通过浏览器访问http://IP/test.php
通过浏览器访问http://IP/index.php