Virtualbox搭建LNMP
系统目标:LNMP利用CentOS 6.3(32 bits),Nginx 1.2.3,MySQL 5.5.27,PHP 5.4.6在Virtualbox中搭建LNMP web server服务器,用户能够从Host浏览器访问Guest(CentOS 6.3)Web Server上面的web服务。
使用软件:
1CentOS 6.3 (32 bits)
下载
CentOS 6.332-bit DVD ISO-(3.6G)
下载
CentOS 6.364-bit DVD ISO-(4.0G)
或 CentOS Download
2Nignx
nginx-1.2.3.tar.gz
依赖软件:pcre-8.31.tar.gz
3MySQL
mysql-5.5.27.tar.gz
依赖软件:cmake( cmake构建MySQL, cmake
VS autotools)
4PHP
PHP 5.4.6
依赖软件:
libconv-1.14.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.gz
扩展软件:
memcached-1.4.14.tar.gz (libevent-2.0.19-stable.tar.gz memcache-2.2.6.tgz)
eaccelerator
PDO_MYSQL-1.0.2.tgz
ImageMagick-6.7.9-0.tar.gz
imagick-3.1.0RC2.tgz
5 WinSCP和PuTTY,使用WinSCP可以在host和guest间方便地传输文件,使用PuTTY可以在Host主机进行操作,安装Guest所需的各种软件。
WinSCP
PuTTY
系统库更新:
yum -y install gcc gcc-c++ autoconf bison bison-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel \
openssl openssl-devel openldap openldap-devel nss-ldap openldap-clients openldap-servers
一、VirtualBox 安装 CentOS 6.3(32 bits)
1 下载CentOS 6.3ISO文件,从Virtualbox安装CentOS 6.3
网络类型:bridged(NAT配置需要做PORT FORWARDING,本文不做介绍)
本文所用Host主机为Windows 7,在Windows 7中打开命令提示符,执行 ipconfig/all 查看host主机和Virtualbox的IP地址分别为:
192.168.0.6 和 192.168.56.1
在对Guest系统CentOS 6.3进行配置和安装软件前,可以利用Virtualbox的备份(snapshot)功能对刚安装完成的Guest系统的当前状态进行备份,以备以后恢复到系统最初状态。
2配置CentOS 6.3网络
(1)配置静态IP地址、子网掩码、网关
#查看网络设备
ifconfig-a #如果只有一个网卡,会看到 eth0
cp /etc/sysconfig/network-scripts/ifcfg-eth0/etc/sysconfig/network-scripts/ifcfg-eth0.default
vi/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
HWADDR="08:00:27:D8:B4:EF"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.0.25"
NETMASK="255.255.255.0"
BROADCAST="192.168.0.255"
NETWORK="192.168.0.0"
GATEWAY="192.168.0.1"
:wq
(2)配置域名
cp/etc/resolv.conf/etc/resolv.conf.default #备份
vi/etc/resolv.conf #在文件中加入下列行
nameserver8.8.8.8
:wq
service network restart
(3)配置防火墙(设置iptables),开启80(http)、443(https)、 3306(mysql)端口
vi/etc/sysconfig/iptables
# 把下列三行放在端口22规则下面
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport80-jACCEPT
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport443-jACCEPT
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport3306-jACCEPT
(4)关闭 SELINUX(缺少这一步骤,host主机不能访问web server)
cp /etc/selinux/config/etc/selinux/config.default
vi /etc/selinux/config
#将SELINUX值从enforcing改为disabled
SELINUX=disabled
#注释掉下列行
SELINUX=targeted
(5)重启系统
shutdown -r now系统重启后,成功联网,进行系统库更新(更新操作查看前文)。
(6)Host主机安装WinSCP和PuTTY,使用WinSCP将安装所需的软件(如下图所示)传输到CentOS 6.3目录:/usr/local/src。下列软件安装使用PuTTY,界面更加友好。
二、安装MySQL:
1创建组和用户:
groupaddmysql
useradd-gmysqlmysql-s/sbin/nologin
2创建数据库安装目录:
mkdir-p/usr/local/mysql
3创建数据库配置文件存放目录:
mkdir -p /etc/mysql
4创建数据库存放目录:
mkdir-p/data/mysql
chown-Rmysql:mysql/data/mysql
5数据库安装
#安装cmake,cmake用于安装MySQL,可以使用yum -y install cmake,或从源文件安装如下
mkdir -p /usr/local/cmake
tar zxpvf cmake-2.8.9.tar.gz
cd cmake-2.8.9
./configure --prefix=/usr/local/cmake
make
make install
#在文件/etc/profile尾部加入下列行
#export PATH=$PATH:/usr/local/cmake/bin
echo "export PATH=$PATH:/usr/local/cmake/bin" >> /etc/profile
source /etc/profile # 或者 . /etc/profile
tarzxpvfmysql-5.5.27.tar.gz
cdmysql-5.5.27
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_USER=mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
make
make install
6配置数据库
cd/usr/local/mysql
cp ./support-files/my-huge.cnf/etc/mysql/my.cnf
#修改/etc/mysql/my.cnf文件,将下列行添加到部分
vi /etc/mysql/my.cnf
datadir = /data/mysql
7生成mysql系统数据库
scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
8把MySQL加入系统开机启动
cp./support-files/mysql.server/etc/rc.d/init.d/mysqld
chkconfigmysqldon
#启动mysql
servicemysqld start
9把MySQL可执行文件加入系统变量,把MySQL库文件连接到系统默认位置
#在文件/etc/profile尾部加入下列行
#exportPATH=$PATH:/usr/local/mysql/bin
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile #或 . /etc/profie
可采用下列两种方式之一把MySQL库连接到系统默认位置(安装php时会用到libmysqlclient库):
(1)
ln-s/usr/local/mysql/lib/libmysqlclient.so.*/usr/lib
(2)
vi /etc/ld.so.conf
#把下面一行加入/etc/ld.so.conf文件尾部
/usr/local/mysql/lib
ldconfig
# 使用ldconfig -p查看设置结果
ldconfig -p | less
10创建具有root权限的用户(user)和密码(123456789)
mysql -u root -p -S /tmp/mysql.sock # (提示输入密码是直接回车)
GRANTALLPRIVILEGESON*.*TO'user'@'localhost' IDENTIFIEDBY'123456789';
GRANTALLPRIVILEGESON*.*TO'user'@'127.0.0.1'IDENTIFIEDBY'123456789';
11为root用户设置初始密码
mysqladmin -u rootpassword'new password'
12删除空密码账号
#以根用户登陆
mysql -u root -p
USE mysql
SELECT host,user,passwordFROM user;
DELETE FROM user WHERE password="";
FLUSH privileges;
三、PHP和扩展模块的安装与配置
1安装PHP依赖库
(1)安装libiconv:
tarzxpvflibiconv-1.14.tar.gz
cdlibiconv-1.14
./configure--prefix=/usr/local
make
makeinstall
(2)安装libmcrypt
tarzxpvflibmcrypt-2.5.8.tar.gz
cdlibmcrypt-2.5.8
./configure
make
make install
ldconfig
cdlibltdl
./configure--enable-ltdl-install
make
makeinstall(3)安装mhashtarzxpvfmhash-0.9.9.9.tar.gz
cdmhash-0.9.9.9
./configure
make
makeinstall
ln-s/usr/local/lib/libmcrypt.*/usr/lib
ln-s/usr/local/lib/libmhash.* /usr/lib
(4)安装mcrypttar zxpvf mcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8
ldconfig
./configure
make
makeinstall 2安装PHP
mkdir -p /usr/local/php
tarzxpvfphp-5.4.6.tar.gz
cdphp-5.4.6
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock \
--with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir \
--with-zlib --with-libxml-dir=/usr --enable-xml \
--disable-rpath --enable-bcmath --enable-shmop \
--enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers \
--enable-mbregex --enable-fpm \
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --enable-ftp --with-openssl \
-with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl \
--with-xmlrpc --enable-zip --enable-soap --without-pear \
--with-gettext --enable-session
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
#将php可执行文件路径添加到系统路径
echo "export PATH=$PATH:/usr/local/php/bin:/usr/local/php/sbin" >> /etc/profile
source /etc/profile
3安装PHP扩展模块
(1)安装memcached
#安装memcached依赖包libevent
tarzxpvflibevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure--prefix=/usr/local/libevent
make
make install#安装memcached
tar zxpvf memcached-1.4.14.tar.gz
cdmemcached-1.4.14
./configure--with-libevent=/usr/local/libevent
make
make install#安装PHP的memcache扩展
tarzxpvfmemcache-3.0.6.tgz
cdmemcache-3.0.6
phpize
./configure--enable-memcache--with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install (2)安装eaccelerator
tar zxpvf eaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator
phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
(3)安装PDO_MYSQL
tarzxpvfPDO_MYSQL-1.0.2.tgz
cdPDO_MYSQL-1.0.2
phpize
ln -s /usr/local/mysql/include/* /usr/local/include/
./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-mysql=/usr/local/mysql
make
make install
(4)安装ImageMagick
tarzxpvfImageMagick-6.7.9.0.tar.gz
cdImageMagick-6.7.9.0
./configure
make
makeinstall (5)安装imagick
tarzxpvfimagick-3.1.0RC2.tgz
cdimagick-3.1.0RC2
phpize
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig # MagickWand.pc所在库目录,可以在/usr目录下使用find -name "MagickWand.pc"查找该路径
./configure--with-php-config=/usr/local/php/bin/php-config
make
make install
4配置PHP扩展模块
(1)修改php.ini文件
当前所有扩展模块都存放在目录:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
#修改extension_dir = "./"
vi/usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"
#添加下列行
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
#修改output_buffering
output_buffering = on (2)配置 eAccelerator 加速PHP
mkdir -p /usr/local/eaccelerator_cache
vi /usr/local/php/etc/php.ini
#将下列几行加入文件php.ini尾部
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9" (3)配置php-fpm
mv/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
vi/usr/local/php/etc/php-fpm.conf
修改php-fpm配置参数:
pid = run/php-fpm.pid
user= www
group = www
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 5000
#设置php-fpm开机自启动
#回到php源文件目录
cd /usr/local/src/php-5.4.6
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
#为Nginx创建用户组和用户名
groupadd www
useradd -g www www -s /sbin/nologin
service php-fpm start
四、Nginx安装及配置
1安装依赖库
mkdir/usr/local/pcre
tar zxpvfpcre-8.31.tar.gz
cd pcre-8.31
./configure --prefix=/usr/local/pcre
make
make install 2安装Nignx
tar zxpvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module \
--with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.31\
make
make install
--with-pcre指向安装源文件目录
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
source /etc/profile
#启动nginx
nginx
3 设置nginx开机启动
# 创建nginx配置文件
vi /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.1.2.3 version
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library
. /etc/rc.d/init.d/functions
# Source networking configuration
. /etc/sysconfig/network
# Check that networking is up
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ]; then
echo "nginx already running..."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog { start | stop | restart | reload | status | help }"
exit 1
esac
exit $RETVAL
#保存文件
wq
chmod +x /etc/rc.d/init.d/nginx
chkconfig nginx on
service nginx restart
4 fastcgi配置文件
#确认fastcgi配置文件存在
vi /usr/local/nginx/conf/fastcgi_params
#文件内容如下
fastcgi_paramQUERY_STRING $query_string;
fastcgi_paramREQUEST_METHOD $request_method;
fastcgi_paramCONTENT_TYPE $content_type;
fastcgi_paramCONTENT_LENGTH $content_length;
fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME $fastcgi_script_name;
fastcgi_paramREQUEST_URI $request_uri;
fastcgi_paramDOCUMENT_URI $document_uri;
fastcgi_paramDOCUMENT_ROOT $document_root;
fastcgi_paramSERVER_PROTOCOL $server_protocol;
fastcgi_paramHTTPS $https if_not_empty;
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWARE nginx/$nginx_version;
fastcgi_paramREMOTE_ADDR $remote_addr;
fastcgi_paramREMOTE_PORT $remote_port;
fastcgi_paramSERVER_ADDR $server_addr;
fastcgi_paramSERVER_PORT $server_port;
fastcgi_paramSERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_paramREDIRECT_STATUS 200;
5配置nginx, /usr/local/nginx/conf/nginx.conf文件内容如下
#usernobody;
worker_processes8;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid logs/nginx.pid;
events {
worker_connections65535;
}
http {
include mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout65;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzipon;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
root html;
indexindex.html index.htm index.php;
}
#error_page404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504/50x.html;
location = /50x.html {
root 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 html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_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 {
# denyall;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_namesomenamealiasanother.alias;
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_namelocalhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_keycert.key;
# ssl_session_timeout5m;
# ssl_protocolsSSLv2 SSLv3 TLSv1;
# ssl_ciphersHIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
}
service nginx restart
完成:)
相关参考资料如下:
《实战nginx:取代apache的高性能web服务器》
http://www.osyunwei.com/archives/4936.html
http://huangyu.blog.iyunv.com/1021686/842333/
http://www.cnblogs.com/XL-Liang/archive/2012/02/24/2367066.html
http://seanbook.blog.iyunv.com/1354938/948035
版权声明:本文为博主原创文章,未经博主允许不得转载。
页:
[1]