yum -y install gcc openssl-devel zlib-devel pcre-devel
yum groupinstall "Developement Tools" "Development Libraries" -yt
|
执行上面最后一条语句解压
tar zxvf nginx-0.7.65.tar.gz cd nginx-0.7.65 useradd -s /sbin/nologin -M nginx # 添加一个不能登录的且没有家目录 名为nginx的用户 ./configure \ #开始编译 所带的参数很容易理解笔者不多解释 编译完成后没有报错就可以安装了。 make && make install 编译安装完成后为了让nginx开机启动,笔者提供个systemV风格的脚本 vim /etc/init.d/nginxd |
把以下内容粘贴进去
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac |
保存退出 并给/etc/init.d/nginxd 赋予执行权限
chmod +x /etc/init.d/nginxd
chkconfig --add nginxd ##让入开机启动选项中
chkconfig nginxd on #让其开机自动启动
service nginxd start #立即启动nginx 服务
出现OK字样 并且通过web浏览器 访问该主机出现以下画面 表示nginx 编译完成并成功启动
下面是编译安装MYSQL
Mysql
先解压缩mysql-5.5.3-m3.tar.gz 需进入该源码包所在目录 笔者目录为/root/soft tar zxvf mysql-5.5.3-m3.tar.gz #解压缩到当前目录 cd mysql-5.5.3-m3/ /usr/sbin/groupadd mysql #添加mysql用户 开始编译、安装 (具体参数不多介绍,都是些基本功能的启用,有兴趣者可以查看mysql的官方文档)
编译安装时间可能有些长,需要耐心等待。。 chmod +w /usr/local/webserver/mysql #给mysql目录写的权限 cd / mkdir /mysql/{data,binlog,relaylog} -p #创建mysql数据库、日志存放目录 以mysql用户帐号的身份建立数据表 /usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql -- --datadir=/mysql/data --user=mysql |
手动创建Mysql的配置文件
vim /mysql/my.cnf
[client]
character-set-server = utf8 port = 3306 socket = /tmp/mysql.sock [mysqld] character-set-server = utf8 replicate-ignore-db = mysql replicate-ignore-db = test replicate-ignore-db = information_schema user = mysql port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/webserver/mysql datadir = /mysql/data log-error = /mysql/mysql_error.log pid-file = /mysql/mysql.pid open_files_limit = 10240 back_log = 600 max_connections = 5000 max_connect_errors = 6000 table_cache = 614 external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 300 #thread_concurrency = 8 query_cache_size = 512M query_cache_limit = 2M query_cache_min_res_unit = 2k default-storage-engine = MyISAM thread_stack = 192K transaction_isolation = READ-COMMITTED tmp_table_size = 246M max_heap_table_size = 246M long_query_time = 3 log-slave-updates log-bin = /data0/mysql/3306/binlog/binlog binlog_cache_size = 4M binlog_format = MIXED max_binlog_cache_size = 8M max_binlog_size = 1G relay-log-index = /mysql/relaylog/relaylog relay-log-info-file = /mysql/relaylog/relaylog relay-log = /mysql/relaylog/relaylog expire_logs_days = 30 key_buffer_size = 256M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover interactive_timeout = 120 wait_timeout = 120 skip-name-resolve #master-connect-retry = 10 slave-skip-errors = 1032,1062,126,1114,1146,1048,1396 #master-host = 192.168.1.1
#master-user = username
#master-password = password #master-port = 3306 server-id = 1 innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 512M innodb_data_file_path = ibdata1:256M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 16M innodb_log_file_size = 128M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 #log-slow-queries = /mysql/slow.log #long_query_time = 10 [mysqldump] quick max_allowed_packet = 32M |
创建管理MySQL数据库的shell脚本
vim /mysql/mysqld ###注意 下面的3,4行账号密码可以自行创建,第2行的默认端口3306一般不要改动。
#!/bin/sh
mysql_port=3306 mysql_username="admin" mysql_password="123456" function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/mysql/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /usr/local/webserver/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 5 function_start_mysql } function_kill_mysql() { kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}') kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}') } if [ "$1" = "start" ]; then function_start_mysql elif [ "$1" = "stop" ]; then function_stop_mysql elif [ "$1" = "restart" ]; then function_restart_mysql elif [ "$1" = "kill" ]; then function_kill_mysql else printf "Usage: /mysql/mysqld {start|stop|restart|kill}\n" fi |
赋给shell脚本可执行权限
chmod +x /mysql/mysqld #该脚本可以用/mysql/mysqld start |restart |stop 来执行
启动Mysql: /mysql/mysqld start 通过命令行登录管理MySQL服务器(提示输入密码时直接回车): /usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock 输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(123456):
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '123456';
|
至此Mysql已全部配置完成
PHP
首先 需要解压编译安装
cd /root/soft 解压编译安装libevent-1.4.14b-stable tar zxvf libevent-1.4.14b-stable.tar.gz cd libevent-1.4.14b-stable ./configure && make && make install --------------------------------------------------------------------------------------------------------- cd /root/soft tar zxvf libconv-1.13.1.tar.gz cd libiconv-1.13.1
|
cd /root/soft tar jxvf php-5.3.3.tar.bz2 cd php-5.3.3 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-openssl --enable-fpm --with-libevent-dir=/usr/local --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/local #需要说明两点 1.--with-mysql和--with-mysqli的路径是你mysql的具体所在的目录 2.--enable-fpm 启动fpm .其他都是些基本选项 ,简单易懂 出现以下画面表示编译成功 下面就是安装了 make ZEND_EXTRA_LIBS='-liconv' #因为-liconv的目录不是在/usr/local下所以安装时需要手动指定 说明: 如果在make 时,mysql的路径没有错误,但一直error,尝试使用低版本的mysql在进行此步骤,因为可能存在版本差异的路径bug。。 cp /root/soft/php-5.3.3/php.ini-production /usr/local/php/etc/php.ini
|
全部安装工作准备已经完成,剩下进行一些配置.
首先修改下面来配置修改 /usr/local/php/etc/php-fpm.conf 该配置文件只用修改以下四点 ; Note: Used when pm is set to either 'static' or 'dynamic' ; The number of child processes created on startup. ; The desired minimum number of idle server processes. ; The desired maximum number of idle server processes. 只需要把蓝色部分前面的注释去掉,修改完成,试着启动 /usr/local/php/sbin/php-fpm & 启动后用netstat -tnlp 查看 如果有如图所示端口 测表示正常启动 如需要开机自动启动 则可以添加到/etc/rc.d/rc.local中 vim /etc/rc.d/rc.local 在空白行添加 /usr/local/php/sbin/php-fpm &
|
配置fastcgi_params 文件 cd /etc/nginx/ vim fastcgi_params 将里面内容替换为 fastcgi_param GATEWAY_INTERFACE CGI/1.1; 保存退出 |
下面就是最后修改nginx.conf配置文件 vim /etc/nginx/nginx.conf 首先将 #user nobody; 之后找到 location ~ \.php$ { 把前面注释全部去掉 root 后面的路径是你网页文件所放置的路径,笔者放在/www下 该目录如果不存在的话还需要手动创建。 找到 location / { 修改为如上所示 主要是添加index 后面的index.php 修改完成后保存退出,重新启动nginx服务 service nginxd restart 之后编辑/www/index.php 在其中添加 <?php 保存退出 之后用web浏览器访问 如果出现如下画面 表示服务成功,并能连接到php
|
下面是测试msql 的连接 把刚才/www/index.php中的内容修改成 <?php 然后刷新web页面,如果出现OK!字样 测表示正常连接 |
如果都没有问题,那么恭喜你,你已经全源码成功实现LEMP架构。
好了,笔者经过两天时间终于写完这篇LEMP架构,其中也遇到不少问题,最终还是都一一解决了,但难免有理解错误或写错的地方,还望前辈们多加指点鼓励,笔者以后会与朋友们分享更多的知识和技术,希望能与君共同探讨,努力进步.