注:本文很多源码包都源自sourceforge.net,我直接wget的时候不允许下载,URL都已经给出具体怎么下载合适,请读者自己做主!本文所有的软件包来源的URL均来官网
1、安装LNMP所依赖的软件包组件
yum -y install gcc gcc-c++ pcre-devel openssl-devel mysql-devel libxml2-devel patch bzip2 bzip2-devel curl-devel libjpeg libjpeg-devel libpng-devel libpng freetype freetype-devel openldap openldap-devel perl-CPAN bison ncurses-devel
2、下载安装libiconv
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar xf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/ make && make install
3、下载安装libmcrypt(mhash,mcrypt,libmcrypt是我这边线上开发要用的扩展,读者可以自行选择是否安装)
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download cp /root/下载/libmcrypt-2.5.8.tar.gz . sourceforge好像不允许直接wget下载 tar xf libmcrypt-2.5.8.tar.bz2 cd libmcrypt-2.5.8 ./configure make && make install /sbin/ldconfig cd libltdl/ ./configure --enable-ltdl-install make &&make install
4、下载安装mhash
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download cp /root/下载/mhash-0.9.9.9.tar.gz . tar xf mhash-0.9.9.9.tar.bz2 cd mhash-0.9.9.9 ./configure make && make install ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
5、下载安装mcrypt
wget http://sourceforge.net/projects/mcrypt/files/latest/download cp /root/下载/mcrypt-2.6.8.tar.gz . tar xf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 /sbin/ldconfig ./configure make && make install
6、添加Nginx运行所需的用户、组
groupadd -r nginx useradd -r -g nginx nginx
7、下载解压Nginx
wget http://nginx.org/download/nginx-1.4.5.tar.gz tar xf nginx-1.4.5.tar.gz
8、为了避免在http响应的时候会出现Nginx等的信息,我们在编译的时候可以稍作配置自定义一个信息!
cd nginx-1.4.5/src/http/ vim ngx_http_header_filter_module.c 修改以下两行: static char ngx_http_server_string[] = "Server: nginx" CRLF; static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; 修改为: static char ngx_http_server_string[] = "Server: XZWeb 1.0" CRLF; #XZWeb为想要显示的名称 static char ngx_http_server_full_string[] = "Server: XZWeb 1.0" NGINX_VER CRLF; #XZWeb为想要显示的名称
9、编译安装Nginx
./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre make && make install
10、编写Nginx启动脚本
vim /etc/rc.d/init.d/nginx #!/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
11、添加Nginx为系统服务并设置开机自启动
chmod +x /etc/rc.d/init.d/nginx chkconfig --add nginx chkconfig nginx on
12、修改Nginx配置文件,屏蔽http响应信息中的Nginx版本等信息
在http{}端添加server_tokens off;
13、链接复制linux一些64位的库文件防止在安装PHP的时候报错
cp -frp /usr/lib64/libjpeg.* /usr/lib cp -frp /usr/lib64/libpng* /usr/lib cp -frp /usr/lib64/libldap* /usr/lib ln -s /usr/lib64/mysql/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so ln -s /usr/lib/libmysqlclient.so /usr/lib64/libmysqlclient.so
14、下载安装PHP
wget http://cn2.php.net/get/php-5.5.9.tar.gz/from/this/mirror tar xf php-5.5.9 cd php-5.5.9 ./configure --prefix=/data/install/php --with-mysql --with-mysqli --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 --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 make ZEND_EXTRA_LIBS='-liconv' make test make intall
15、为PHP提供配置文件
cp php.ini-production /etc/php.ini
16、为php-fpm提供Sysv init脚本,并将其添加至服务列表
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm chmod +x /etc/rc.d/init.d/php-fpm chkconfig --add php-fpm chkconfig php-fpm on
17、为php-fpm提供配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
18、编辑php-fpm的配置文件(此处只贴一些我修改过的,具体还有哪些需要修改会在另一篇中补充)
vim /usr/local/php/etc/php-fpm.conf 配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行): pm.max_children = 150 pm.start_servers = 8 pm.min_spare_servers = 5 pm.max_spare_servers = 10 pid = /data/install/php/var/run/php-fpm.pid
19、接下来就可以启动php-fpm了
service php-fpm start
20、使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了)
ps -ef |grep php-fpm
21、下载安装memcache扩展(PHP连接memcached服务器的扩展)
cd /data/software/src/ wget http://pecl.php.net/get/memcache-2.2.7.tgz tar xf memcache-2.2.6.tgz cd memcache-2.2.6 /data/install/php/bin/phpize ./configure --with-php-config=/data/install/php/bin/php-config make && make install
22、下载安装xcache扩展
wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz tar xf xcache-3.1.0.tar.gz cd xcache-3.1.0 /data/install/php/bin/phpize ./configure --enable-xcache --with-php-config=/data/install/php/bin/php-config make && make install
23、配置PHP文件,添加扩展
vim /etc/php.ini extension_dir = "/data/install/php/lib/php/extensions/no-debug-non-zts-20121212/" [memcache] extension = "memcache.so" [xcache] extension = "xcache.so" Xcache.size = 128M Xcache.count = 2 Xcache.ttl = 86400 Xcache.gc_interval = 3600 Xcache.var_size = 0
24、查看扩展是否加载成功
/data/install/php/bin/php -v #查看下xcache信息,此处先看下xcache跟zend的,其他的可以在phpinfo中查看; #有关xcache的详细官方配置介绍 http://xcache.lighttpd.net/wiki/XcacheIni
25、修改下php-fpm的配置文件,适应线上环境(此处只给出我修改的一些项目,具体还能改哪些会在后面的博文中补充)
pid = /data/install/php/var/run/php-fpm.pid error_log = /data/logs/php-fpm-error.log log_level = notice emergency_restart_threshold = 10 emergency_restart_interval = 1m process_control_timeout = 5s daemonize = yes user = nobody group = nobody listen = 127.0.0.1:9000 pm.max_children = 192 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
26、配置fastcgi
vim /etc/nginx/fastcgi_params fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name;
27、重启php-fpm
service php-fpm restart
28、修改Nginx配置文件,使其适合线上环境
vim /etc/nginx/nginx.conf user nobody; worker_processes 4; error_log /data/logs/error.log crit; worker_cpu_affinity 0001 0010 0100 1000; worker_rlimit_nofile 20480; #可以直接设置为65535 pid /data/logs/nginx.pid; events { use epoll; worker_connections 20480; #可以直接设置成65535,注意尽量跟上面的设置统一 } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; charset utf-8; server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; server_names_hash_bucket_size 128; client_header_buffer_size 4k; large_client_header_buffers 4 4k; client_max_body_size 8m; keepalive_timeout 65; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 600; fastcgi_buffer_size 16k; fastcgi_buffers 16 16k; fastcgi_busy_buffers_size 32k; #open_file_cache max=204800 inactive=20s; #open_file_cache_min_uses 1; #open_file_cache_valid 30s; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; server { listen 80; server_name 192.168.239.130; #charset koi8-r; access_log /data/logs/www.access.log main; location / { root /data/web; # index index.html index.htm; index index.php; } location ~ \.(js|css|jpg|png|gif)$ { root /data/web; expires 30d; } location ~ \.php$ { root /data/web; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_pass_header on; if ( $fastcgi_script_name ~ \..*\/.*php ) { return 403; } fastcgi_param SCRIPT_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 { deny all; } } }
29、重启Nginx(也可以重新reload一下配置文件)
service nginx restart
30、关于这篇博文的一些问题
1、有人可能会问为什么没有安装mysql的步骤呢,这里安装的只是Nginx跟PHP,一般线上很少会 PHP、Nginx、mysql同在一台服务器吧(即使就有一台服务器最好是用虚拟机的方式将不同服务安 装到不同虚拟机做业务分离),要想解决PHP安装依赖mysql某些文件的话直接yum安装mysql- devel就可以了,节省时间节省空间多好!!!
2、博文中提到的后续文章什么时候能更新?这个不好说 ,我也是菜鸟在摸索中,等我摸索好了自 然会发出来的,哈哈@_@;
3、博文中提到的屏蔽Nginx信息,PHP信息是指什么?两张图便知晓
修改后
看图你们就知道我的良苦用心了吧!!!!!
29/