VPS从零到high之6—安装配置apache-worker + php-fpm

VPS从零到high之6—安装配置apache-worker + php-fpm

changblog.com/469

Update 2:人类在前进,时代在变迁。apache现在已经出了新版,提供了event模式,php也内置了fpm。我的vps配置也有了很大改变,下边这个配置,已经不够潮了,除非你知道自己在做啥,否则,建议更改一下这个配备。

我现在的配备是ubuntu lts 32位,nginx,php-fpm(php自带的fpm),apc,memcache,mariadb,csf,chive。因为主要使用drupal,所以用的nginx编译版本,加入了fastcgi cache和purge代码。用mariadb代替mysql,csf代替了fail2ban,chive代替phpmyadmin。

如果还是想用文章的配置,我建议去看看这个:tuxlite

安装apache2-worker+ php-fpm+ apc+ memcache+ mysql。

debian的包管理就是好,省了很多事情,节约时间,基本上安装不是问题,需要操心的就是配置。centos的确因为和redhat的关系,很多人在用,但是每每看到他的安装教程上边大量的编译,我就郁闷。这么喜欢自己编译,还不如用gentoo呢。

我用的是debian6 64系统,linode的512款,xeon 4核cpu,1g swap。

网上大部分apache2的安装教程主要是apache2-perfork+ mod_php的,因为php-fpm是刚刚加入到php5.3的官方代码中的,所以在apache下边用这个的人还不多,远没有nginx上普及。apache的好处的确就是稳定,特别是配合mod_php的情况下,所以很多人都是牺牲效率,安装这个,然后用缓存来弥补损失的。我之所以使用php fastcgi,主要还是想为可能的向nginx转移做准备。

对了,这些操作全是在root权限下进行的。因为是新配置机器,为了方便,我并没加新用户,然后用sudo。这些安全设置加强我最后做。

这些内容基于 tuxlite 提供的免费lmap安装脚本实现,这是一个国外的一键安装脚本,因为网上用worker+php-fpm的安装很少,所以这是一个很稀有的资源。里边不光提供了这种安装模式,还提供了常规的mod_phphe和fcgi模式的安装,以及vhost的建立脚本。并且它还会自动安装varnish等辅助软件,并设置开关。如果自己要求不高,又不喜欢折腾,推荐选用。他还提供了lnmp的安装脚本。我用的是单用户lamp1.1的脚本。

因为 libapache2-mod-fastcgi授权不兼容debian包的,如果要安装,要么用第三方的源,要么在官方源中接受non-free:

打开/etc/apt/sources.list文件,备份,然后在每句后边加入non-free contrib,我的是这个样子的:

1 deb http://ftp.us.debian.org/debian/ squeeze main contrib non-free
2 deb-src http://ftp.us.debian.org/debian/ squeeze main contrib non-free
3  
4 deb http://security.debian.org/ squeeze/updates main contrib non-free
5 deb-src http://security.debian.org/ squeeze/updates main contrib non-free
6  
7 deb http://ftp.us.debian.org/debian/ squeeze-updates main contrib non-free
8 deb-src http://ftp.us.debian.org/debian/ squeeze-updates main contrib non-free
9  
10 deb http://packages.dotdeb.org squeeze all
11 deb-src http://packages.dotdeb.org squeeze all

然后要:

1 apt-get update
2 apt-get upgrade

开始安装,debian里边最爽的一步,特别是通过ssh看到国外vps那酷爽的速度的时候。

安装apache:

1 apt-get install apache2 apache2-mpm-worker libapache2-mod-fastcgi libapache2-mod-rpaf
2 a2dismod php4
3 a2dismod php5
4 a2dismod fcgid
5 a2enmod actions
6 a2enmod fastcgi
7 a2enmod ssl
8 a2enmod rewrite
9  
10 mkdir -p /srv/www/fcgi-bin.d
11 ln -s /usr/sbin/php5-fpm  /srv/www/fcgi-bin.d/

安装php:

1 apt-get install php5-fpm php5-cgi php5-suhosin php-apc php5-mysql php5-dev php5-curl php5-gd php5-imagick php5-mcrypt php5-memcache php5-mhash php5-pspell php5-snmp php5-sqlite php5-xmlrpc php5-xsl

安装mysql:

1 apt-get install mysql-server mysql-client

然后安全化mysql:

1 mysql_secure_installation

安装辅助性软件,包括myphpadmin等,根据需要选择。我没有装awstats统计,我用的自己装载yardvps的独立统计。postfix选择Internet Site输入机器的名称:

1 apt-get install postfix awstats imagemagick phpmyadmin
2 /usr/sbin/postconf -e "inet_interfaces = loopback-only"

安装完成,开始设置。

设置fastcgi,/etc/apache2/mods-available/fastcgi.conf:

1 #<IfModule mod_fastcgi.c>
2 #  AddHandler fastcgi-script .fcgi
3 #  #FastCgiWrapper /usr/lib/apache2/suexec
4 #  FastCgiIpcDir /var/lib/apache2/fastcgi
5 #</IfModule>
6 <IfModule mod_fastcgi.c>
7     FastCgiIpcDir /var/lib/apache2/fastcgi
8     FastCGIExternalServer /srv/www/fcgi-bin.d/php5-fpm -socket /tmp/php-cgi.sock
9      Alias /php5-fcgi /srv/www/fcgi-bin.d
10     AddHandler php-fpm .php
11     Action php-fpm /php5-fcgi/php5-fpm
12     AddType application/x-httpd-php .php
13  
14     <Directory  "/srv/www/fcgi-bin.d">
15     Order deny,allow
16     Deny from all
17         <Files "php5-fpm">
18         Order allow,deny
19         Allow from all
20         </Files>
21     </Directory>
22 </IfModule>

php fastcgi的pool设置,/etc/php5/fpm/pool.d/www.conf:

1 [www]
2 listen = /tmp/php-cgi.sock
3 listen.owner = www-data
4 listen.group = www-data
5 listen.mode = 0666
6 user = www-data
7 group = www-data
8 pm = dynamic
9 pm.max_children = 50
10 pm.start_servers = 20
11 pm.min_spare_servers = 5
12 pm.max_spare_servers = 35
13 pm.max_requests = 500
14 request_terminate_timeout = 120s
15 chdir = /
16  php_flag[display_errors] = off
17 php_admin_value[error_log] = /var/log/fpm-php.www.log
18 php_admin_flag[log_errors] = on
19 php_admin_value[memory_limit] = 128M
20 php_admin_value[date.timezone] = US/Pacific-New

设置apache:

1 #Removing Apache server information from headers.
2 sed -i 's/ServerTokens .*/ServerTokens Prod/' /etc/apache2/conf.d/security
3 sed -i 's/ServerSignature .*/ServerSignature Off/' /etc/apache2/conf.d/security
4  
5 #Add *:443 to ports.conf
6 temp=`grep "NameVirtualHost \*:443$" /etc/apache2/ports.conf | wc -l`
7 if [ $temp -lt 1 ]; then
8 sed -i '/\<IfModule mod_ssl.c\>/ a\    NameVirtualHost \*:443' /etc/apache2/ports.conf
9 fi
10 #Change default log rotation for apache log files
11 sed -i 's/\tweekly/\tdaily/' /etc/logrotate.d/apache2
12 sed -i 's/\trotate .*/\trotate 10/' /etc/logrotate.d/apache2
13 #Tweak apache.conf, maxclients 45
14 cp /etc/apache2/{apache2.conf,apache2.conf.bak}
15 #sed -i 's/Timeout .*/Timeout 5/' /etc/apache2/apache2.conf
16 sed -i 's/\(^\s*StartServers\)\s*[0-9]*/\1         1/' /etc/apache2/apache2.conf
17 sed -i 's/\(^\s*MaxClients\)\s*[0-9]*/\1           45/' /etc/apache2/apache2.conf
18 sed -i 's/\(^\s*MinSpareThreads\)\s*[0-9]*/\1      2/' /etc/apache2/apache2.conf
19 sed -i 's/\(^\s*MaxSpareThreads\)\s*[0-9]*/\1      5/' /etc/apache2/apache2.conf
20 sed -i 's/\(^\s*ThreadLimit\)\s*[0-9]*/\1          15/' /etc/apache2/apache2.conf
21 sed -i 's/\(^\s*ThreadsPerChild\)\s*[0-9]*/\1      15/' /etc/apache2/apache2.conf
22 sed -i 's/\(^\s*MaxRequestsPerChild\)\s*[0-9]*/\1  2000/' /etc/apache2/apache2.conf

设置phpmyadmin通过自签名的ssl访问:

1 #Force phpmyadmin logins to be SSL secured
2 temp=`grep -i forcessl /etc/phpmyadmin/config.inc.php | wc -l`
3 if [ $temp -lt 1 ]; then
4 echo '$cfg['ForceSSL'] = 'true';' >> /etc/phpmyadmin/config.inc.php
5 fi
6 #Generating self signed SSL certs for securing phpmyadmin, script logins
7 echo -e " "
8 echo -e "\033[35;1m Generating SSL certs, you do not have to enter any details when asked. But recommended to enter Hostname FQDN for 'Common Name'! \033[0m"
9 mkdir /etc/ssl/localcerts
10 openssl req -new -x509 -days 365 -nodes -out /etc/ssl/localcerts/apache.pem\
11  -keyout /etc/ssl/localcerts/apache.key

设置mysql:

1 #Tweak my.cnf
2 cp /etc/mysql/{my.cnf,my.cnf.bak}
3 if [ -e /usr/share/doc/mysql-server-5.1/examples/my-medium.cnf.gz ]; then
4 gunzip /usr/share/doc/mysql-server-5.1/examples/my-medium.cnf.gz
5 cp /usr/share/doc/mysql-server-5.1/examples/my-medium.cnf /etc/mysql/my.cnf
6 else
7 gunzip /usr/share/doc/mysql-server-5.0/examples/my-medium.cnf.gz
8 cp /usr/share/doc/mysql-server-5.0/examples/my-medium.cnf /etc/mysql/my.cnf
9 fi
10 sed -i '/myisam_sort_buffer_size/ a\skip-innodb' /etc/mysql/my.cnf
11 /etc/init.d/mysql restart

设置/etc/php5/fpm/php.ini,包括php最大可用内存,上传最大尺寸,php执行时间:

1 #Tweak php.ini, Execution time 2 mins, upload time 5mins, mem limit 64mb and upload file size 25MB.
2 sed -i 's/^\(max_execution_time = \)[0-9]*/\1120/' /etc/php5/fpm/php.ini
3 sed -i 's/^\(max_input_time = \)[0-9]*/\1300/' /etc/php5/fpm/php.ini
4 sed -i 's/^\(memory_limit = \)[0-9]*M/\164M/' /etc/php5/fpm/php.ini
5 sed -i 's/^\(post_max_size = \)[0-9]*M/\125M/' /etc/php5/fpm/php.ini
6 sed -i 's/^\(upload_max_filesize = \)[0-9]*M/\125M/' /etc/php5/fpm/php.ini
7 sed -i 's/disable_functions =/disable_functions = exec, system, passthru, shell_exec, escapeshellarg, escapeshellcmd, proc_close, proc_open, dl, popen, show_source/' /etc/php5/fpm/php.ini

设置Awstats,如果你装了的话:

1 #Configure Awstats
2 temp=`grep -i sitedomain /etc/awstats/awstats.conf.local | wc -l`
3 if [ $temp -lt 1 ]; then
4 echo SiteDomain="$HOSTNAME_FQDN" >> /etc/awstats/awstats.conf.local
5 fi
6 #Disable Awstats from executing every 10 minutes. Put a hash in front of any line.
7 sed -i 's/^[^#]/#&/' /etc/cron.d/awstats
8  
9 #Debian 5 doesn't have buildstatic tool in the right directory.
10 DEB_VER=`cat /etc/debian_version`
11 DEB_VER=${DEB_VER:0:1}
12 if [ $DEB_VER -eq 5 ] && [ ! -d /usr/share/awstats/tools ]; then
13 mkdir /usr/share/awstats/tools
14 cp -R /usr/share/doc/awstats/examples/* /usr/share/awstats/tools
15 fi

安装重试失败阻止软件,增加安全性:

1 apt-get install fail2ban

至此,完成。开始重新启动。

1 /etc/init.d/apache2 restart
2 /etc/init.d/php-fpm restart

虚拟主机设置文件的样板,web目录设置在/srv/www/:

1 <VirtualHost *:80>
2  
3     ServerName changblog.com
4     ServerAlias www.changblog.com
5     ServerAdmin [email protected]
6     DocumentRoot /srv/www/changblog.com/public_html/
7     ErrorLog /srv/www/changblog.com/logs/error.log
8     CustomLog /srv/www/changblog.com/logs/access.log combined
9  
10     <Directory /srv/www/changblog.com/public_html>
11         Options Indexes FollowSymLinks
12         AllowOverride All
13         Order allow,deny
14         allow from all
15     </Directory>
16  
17     ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
18     <Location /cgi-bin>
19         Options +ExecCGI
20     </Location>
21  
22 </VirtualHost>
23  
24 <IfModule mod_ssl.c>
25 <VirtualHost *:443>
26  
27     ServerName changblog.com
28     ServerAlias www.changblog.com
29     ServerAdmin [email protected]
30     DocumentRoot /srv/www/changblog.com/public_html/
31     ErrorLog /srv/www/changblog.com/logs/error.log
32     CustomLog /srv/www/changblog.com/logs/access.log combined
33  
34     <Directory /srv/www/changblog.com/public_html>
35         Options Indexes FollowSymLinks
36         AllowOverride All
37         Order allow,deny
38         allow from all
39     </Directory>
40  
41     ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
42     <Location /cgi-bin>
43         Options +ExecCGI
44     </Location>
45  
46     SSLEngine on
47     SSLCertificateFile    /etc/ssl/localcerts/apache.pem
48     SSLCertificateKeyFile /etc/ssl/localcerts/apache.key
49  
50     <FilesMatch "\.(cgi|shtml|phtml|php)$">
51         SSLOptions +StdEnvVars
52     </FilesMatch>
53  
54     <Directory /usr/lib/cgi-bin>
55         SSLOptions +StdEnvVars
56     </Directory>
57  
58     BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
59     BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
60  
61 </VirtualHost>
62 </IfModule>

建立虚拟主机就使用lamp1.1的那个domain.sh就可以了。记得要手动删除sites-available中的default那个软连接哦。(看说明哦,先要把权限改成777,然后./domain.sh+参数,来执行。我因为没装统计,所以手动注释了一些相关的代码,按理说,直接用问题不大。)

这个脚本只能添加namebased的虚拟主机,要添加ipbased,还需要手动。我准备使用linode大方提供的ipv6 pool,给以后的每个主机一个ipv6的独立ip。

update1:

对于apache,php的优化,我只是参照的参考资料中的设置,建议根据具体的应用环境和资源进行相应调配。

参考资料:

Apache2, PHP-FPM, FastCGI et APC cache d’Opcode
Nginx proxy to Apache – access remote host IP address using mod_praf(对于varnish或者ngnix做反向代理的时候,需要用到libapache2-mod-rpaf以传递访问者的真实ip,参阅此教程。)



你可能感兴趣的:(VPS从零到high之6—安装配置apache-worker + php-fpm)