yum -y install gcc openssl-devel zlib-devel pcre-devel
yum groupinstall "Developement Tools" "Development Libraries" -yt
|
執行上面最後一條語句解壓php
tar zxvf nginx-0.7.65.tar.gzhtml cd nginx-0.7.65前端 useradd -s /sbin/nologin -M nginx # 添加一個不能登陸的且沒有家目錄 名爲nginx的用戶python ./configure \ #開始編譯 所帶的參數很容易理解筆者很少解釋 編譯完成後沒有報錯就能夠安裝了。linux make && make install 編譯安裝完成後爲了讓nginx開機啓動,筆者提供個systemV風格的腳本web vim /etc/init.d/nginxdsql |
把如下內容粘貼進去shell
#!/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架構,其中也遇到很多問題,最終仍是都一一解決了,但不免有理解錯誤或寫錯的地方,還望前輩們多加指點鼓勵,筆者之後會與朋友們分享更多的知識和技術,但願能與君共同探討,努力進步.