1. MySQL的編譯安裝:javascript
1. 增長一個mysql用戶:-M就不生成家目錄php
# useradd -s /sbin/nologin -M mysql
2. 在data目錄下面創建一個mysql目錄,並修改權限:-p建立級聯目錄,-R繼承權限css
# mkdir -p /data/mysql # chown -R mysql:mysql /data/mysql
3. 下載MySQL安裝包:下載地址選擇的是搜狐鏡像:mirrors.sohu.com,須要其他版本的能夠在裏面本身找。源碼包的大小目前不超過100M(2016年)。html
# cd /usr/local/src # wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.11.tar.gz
4. 下載完成後,將mysql解壓並進行cmake和後續操做:java
# tar zxvf mysql-5.7.11.tar.gz # cd mysql-5.7.11 # cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql/\ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci # make && make install
由於MySQL自己很大,因此,make的時間會很是長。node
5. 下面進行初始化:mysql
./scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
出現錯誤: bash: ./scripts/mysql_install_db: 權限不夠;查詢./scripts/mysql_install_db的權限,發現沒有x:nginx
# ll ./scripts/mysql_install_db
-rw-r--r-- 1 mysql mysql 33853 12月 3 18:24 ./scripts/mysql_install_db
添加x權限,從新初始化:sql
# chmod a+x ./scripts/mysql_install_db # ./scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
6. 初始化成功,出現兩個 OK,說明初始化成功。拷貝配置文件到相應位置:shell
# cp my.cnf /etc/my.cnf ## 覆蓋原來的my.cnf # cd support-files/ # cp mysql.server /etc/init.d/mysqld # chmod 755 !$ # /etc/init.d/mysqld start # ps aux |grep mysqld
啓動MySQL服務之後,在進程裏面能夠看到mysqld。
2. php的編譯安裝
針對Nginx的php安裝和針對apache的php安裝是有區別的。區別在於,Nginx中的php是以fastcgi的方式結合nginx的。能夠理解爲nginx代理了php的fastcgi,而apache是把php做爲本身的模塊來調用的。
PHP官方下載地址: http://www.php.net/downloads.php,以5.4版本爲例:
php和nginx的安裝沒有順序,下面就先安裝php。
1. 下載和創建用戶
# cd /usr/local/src # wget http://au1.php.net/distributions/php-5.4.44.tar.bz2 # tar jxf php-5.4.44.tar.bz2 # useradd -s /sbin/nologin php-fpm
2. 配置編譯參數:
# cd php-5.4.44 ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ --with-fpm-user=php-fpm \ --with-fpm-group=php-fpm \ --with-mysql=/usr/local/mysql \ --with-mysql-sock=/tmp/mysql.sock \ --with-libxml-dir \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-zlib-dir \ --with-mcrypt \ --enable-soap \ --enable-gd-native-ttf \ --enable-ftp \ --enable-mbstring \ --enable-exif \ --enable-zend-multibyte \ --disable-ipv6 \ --with-pear \ --with-curl \ --with-openssl
3. 安裝
# make && make install
4.修改配置文件:
# cp php.ini-production /usr/local/php/etc/php.ini # > /usr/local/php/etc/php-fpm.conf
## 清空配置
# vim !$ ## 添加以下配置: [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 ## 保存配置文件後,檢驗配置是否正確的方法爲: # /usr/local/php/sbin/php-fpm -t,能夠設立一個alias。
5. 啓動php
首先要拷貝一個啓動腳本到/etc/init.d/下
# cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod 755 /etc/init.d/php-fpm # chkconfig --add php-fpm # chkconfig php-fpm on ## 上面添加開機啓動 # useradd -s /sbin/nologin -M php-fpm ## 在LNMP環境中,php是以一個服務方式來提供的,配置的時候,user = php-fpm; group = php-fpm就指定了這個用戶,所以,須要創建一個php-fpm不可登陸的帳戶來運行php-fpm服務。 # service php-fpm start # ps aux |grep php-fpm
如上,若是能夠看到php-fpm的進程(20餘個),說明配置成功。
3. Nginx的編譯安裝
1. 下載、解壓Nginx
# cd /usr/local/src/ # wget http://nginx.org/download/nginx-1.8.0.tar.gz # tar zxvf nginx-1.8.0.tar.gz
2. 配置:
# cd nginx-1.8.0 # ./configure \ --prefix=/usr/local/nginx \ --with-http_realip_module \ --with-http_sub_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre
3. 編譯、安裝Nginx
# make && make install
4. 啓動nginx,檢查nginx是否啓動:
# /usr/local/nginx/sbin/nginx # ps aux |grep nginx
【1】nginx啓動腳本和配置文件
1. 編寫啓動腳本:
# vim /etc/init.d/nginx
將下面內容寫入shell腳本:
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload(){ echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart(){ stop start } configtest(){ $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
保存後,更改權限,添加到開機啓動:
# chmod 755 /etc/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
【2】Nginx的php解析功能添加
要支持php的解析,首先要修改配置文件,將PHP解析部分註釋打開,修改默認的root目錄路徑,這樣就能夠在給定的路徑下面對php進行解析了。
# vim /usr/local/nginx/conf/nginx.conf ## 打開以下內容的註釋: location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; }
注意,上面倒數第三行/usr/local/nginx/html是默認的訪問路徑,配置錯誤會顯示:502 Bad Gateway!
# cd /usr/local/nginx/html # vim 1.php ## 寫入以下內容: <?php phpinfo (); ?>
在瀏覽器中輸入:192.168.220.11/1.php,若是能看到以下圖般的php頁面,說明配置正確:
4. 參數調優
【1】修改Nginx配置文件:
清空原來的配置文件:
# > /usr/local/nginx/conf/nginx.conf # vim /usr/local/nginx/conf/nginx.conf ## 寫入以下內容: user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; ## 以下打開vhosts目錄的配置開關 include vhosts/*.conf; }
檢查配置有無錯誤的命令:
# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
顯示如上內容,說明沒有配置錯誤。另外,咱們自定義的Nginx啓動腳本也是能夠檢測語法錯誤的:
# /etc/init.d/nginx configtest
功能和上面的命令同樣。
【2】關閉默認虛擬主機:
創建vhosts目錄:
# cd /usr/local/nginx/conf
# mkdir vhosts
創建默認虛擬主機配置文件,禁止ip和非指定域名訪問:
# cd vhosts/ # vim default.conf ## 寫入以下內容: server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root /tmp/123; deny all; }
其中,/tmp/123須要建立,裏面不用放文件,空目錄便可。deny all表示一切非指定域名、或者ip直接訪問的請求所有被禁止。
【3】配置一個可訪問域名:
# vim aaa.conf # 寫入以下內容: server { listen 80; ## 指定網址域名 server_name aaa.com; index index.html index.htm index.php; ## 指定家目錄,網頁文件放在這個目錄 root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; ## 配置支持socket模式,視狀況而定 fastcgi_pass unix:/tmp/www.sock; fastcgi_index index.php; ## 指定家目錄路徑 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
system32\drivers\etc\hosts
注意:須要管理員權限才能編輯,寫入以下配置:
192.168.220.11 aaa.com
如此在瀏覽器輸入地址:
http://aaa.com/1.php
是否能獲得和上面同樣的php頁面?能的話,說明配置成功。
curl工具的測試方法以下:
# curl -xlocalhost:80 aaa.com/1.php -I HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 17 Feb 2016 14:47:47 GMT Content-Type: text/html Connection: keep-alive X-Powered-By: PHP/5.4.44
顯示200 OK,表示訪問成功。如此,自定義的網頁域名就設置成功了。
【4】php-fpm.conf的參數優化:
清空原來的php-fpm.conf,寫入新內容:
# > /usr/local/php/etc/php-fpm.conf # vim !$ ## 寫入以下內容: [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/www.sock user = php-fpm group = php-fpm listen.owner = nobody listen.group = nobody pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 10240
slowlog = /path/slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir]=/data/www/:/tmp/
其中,參數中的listen.owner = nobody,listen.group = nobody和的nginx的配置一致,不然,可能由於權限的問題致使沒法訪問目錄,致使502 Bad Gateway! Slowlog的添加也有很重要的做用,能夠方便在網頁瀏覽變慢的時候,排除故障;open_basedir是開放的訪問目錄,用冒號進行擴展。