Techyou labs
真正的爱应该超越生命的长度,心灵的宽度,灵魂的深度
常用标签
您正在查看:标签 apache2 下的文章

ACL+grsecurity+jailkit

目录最好设置成750,否则会出现403错误

php等cgi程序,去掉其他用户的任何权限,防止在开通了shell的情况下被跨站攻击
/home 目录本身为751(根据suexec配置选项所得)
/home下所有目录默认750,并分配如下ACL权限,web-httpd为Apache运行身份,www-data为nginx运行身份
/home/cgi-system/ 目录为751,子目录及其文件为750

setfacl -m u:web-httpd:rx,g:web-httpd:rx,u:www-data:rx,g:www-data:rx,o::- -R /home
setfacl -d -m u:web-httpd:rx,g:web-httpd:rx,u:www-data:rx,g:www-data:rx,o::- -R /home

logs目录设置用户可读

#setfacl -m u:user:r,g:user:r -R /home/user/logs
setfacl -d -m u:user:r,g:user:r -R /home/user/logs

/home/username 目录属性为710/750
下级目录和文件属主为用户本身,权限750

参考
http://linux.chinaunix.net/techdoc/beginner/2009/10/27/1141592.shtml
http://www.cnblogs.com/cabin/archive/2010/09/18/1830180.html
http://www.ibm.com/developerworks/cn/linux/l-acl/
http://ljh0242.blog.163.com/blog/static/5697009020088223959537/

Nginx+Apache(suexec+fcgid)+PHP5.3.x+Mysql5.1.x+Pureftpd+Quota服务器部署笔记
首选32bit centos
源码包全部放在/usr/src/source_package下
1:初始化环境

#yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel mhash mhash-devel libmcrypt libmcrypt-devel libc-client libc-client-devel

2:安装libevent

#wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz#tar xf libevent-1.4.14b-stable.tar.gz#cd libevent-1.4.14b-stable #./configure --enable-shared#make && make install

3:安装pcre

#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz#tar xf pcre-8.10.tar.gz#cd pcre-8.10/#./configure --enable-shared --enable-utf8 --enable-unicode-properties#make && make install

4:安装libunwind

#wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz#tar xf libunwind-0.99.tar.gz#cd libunwind-0.99#./configure --enable-shared#make && make install

5:安装google-perftools

#wget http://google-perftools.googlecode.com/files/google-perftools-1.6.tar.gz#tar xf google-perftools-1.6.tar.gz#cd google-perftools-1.6#./configure --enable-shared##注:64bit 必须加上--enable-frame-pointers#make && make install

6:安装nginx.

#wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz#tar zxvf ngx_cache_purge-1.0.tar.gz#wget http://nginx.org/download/nginx-0.8.50.tar.gz#tar zxvf nginx-0.8.50.tar.gz#cd nginx-0.8.50/#cd ../#useradd -d /dev/null -s /sbin/nologin -c "Nginx Runner" -r www-data#mkdir -p /zh/nginx#mkdir -p /tmp/nginx/client/cache#mkdir -p /tmp/nginx/proxy/cache#mkdir -p /tmp/nginx/fastcgi/cache

如想提速,可以将缓存目录划到/dev/shm下

#./configure --prefix=/zh/nginx --user=www-data --group=www-data --with-rtsig_module --with-select_module --with-poll_module --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_gzip_static_module -–with-file-aio --with-http_stub_status_module --http-client-body-temp-path=/tmp/nginx/client --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --with-google_perftools_module --add-module=/usr/src/source_package/ngx_cache_purge-1.0#make && make install

如需要无缝升级:

#cp -r --force objs/nginx /zh/nginx/sbin#/etc/init.d/nginx restart

7:Mysql5.1、PHP5.3.x安装编译mysql

#./configure --prefix=/usr/local/mysqld --enable-assembler --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=max-no-ndb --enable-shared --with-tcp-port=3306 --with-mysqld-user=mysqld --with-low-memory --with-mysqlmanager

安装并初始化环境

# make # make install# cp support-files/my-medium.cnf /usr/local/mysqld/etc/my.cnf

更改目录权限

# chown -R mysqld /usr/local/mysqld # chown -R mysqld /var/mysqldata # chown -R mysqld:mysqld /usr/local/mysqld # chown -R mysqld:mysqld /var/mysqldata   # chmod 711 /usr/local/mysqld -R# chmod 755 /usr/local/mysqld/lib/ -R# chmod 700 /var/mysqldata -R

初始化数据库

# /usr/local/mysqld/bin/mysql_install_db --user=mysqld --datadir=/var/mysqldata --basedir=/usr/local/mysqld --skip-name-resolve --defaults-file=/usr/local/mysqld/etc/my.cnf

php编译:

#./configure --prefix=/zh/php5 --enable--mod-charset --enable-roxen-zts --with-libxml-dir=/usr --with-layout=GUN --with-config-file-path=/zh/php5/etc --with-config-file-scan-dir=/home/*/etc --with-openssl --with-zlib --enable-sigchild --enable-bcmath --enable-calendar --with-curl --with-curlwrappers --enable-exif --enable-ftp --disable-rpath --with-gd --with-jpeg-dir --with-png-dir --enable-gd-native-ttf --with-gettext --with-mhash --with-imagick --with-imap --with-imap-ssl --enable-mbstring --with-mcrypt --with-mysql=mysqlnd --enable-mbregex --with-mysql-sock --with-mysqli --with-pdo-mysql --with-pdo-sqlite --enable-soap --enable-sockets --enable-sqlite-utf8 --enable-sysvsem --with-xmlrpc --enable-zip --without-pear --enable-shmop --enable-pcntl --with-libevent=shared --with-kerberos --enable-phar --enable-session --enable-simplexml --enable-snmp --enable-sqlite3 --enable-tidy --enable-tokenizer --enable-xml --enable-xmlreader --enable-xmlrpc --enable-xmlwriter --enable-xsl --enable-fastcgi --enable-cgi --enable-shared#make#make install

安装eAccelerator、Memcache、suhosin扩展:

1:memcache#/zh/php5/bin/phpize#./configure --with-php-config=/zh/php5/bin/php-config --enable-shared --enable-memcache#make && make install2:eaccelerator#/zh/php5/bin/phpize#./configure --with-php-config=/zh/php5/bin/php-config --enable-shared --enable-eaccelerator --with-gnu-ld#make && make install3:suhosin#/zh/php5/bin/phpize#./configure --with-php-config=/zh/php5/bin/php-config --enable-shared --enable-suhosin#make && make install

添加至php.ini

5:Apache(suexec+mod_fcgid)安装

http://blog.37hi.com/archives/229/

http://blog.37hi.com/archives/221/

6:为Apache安装mod_rpaf模块获取真实IP

tar -xf mod_rpaf-0.6.tar.gzcd mod_rpaf-0.6/usr/local/apache2/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

编辑httpd.conf文件添加:

#rpaf_moduleLoadModule rpaf_module modules/mod_rpaf-2.0.so#Mod_rpaf settingsRPAFenable OnRPAFproxy_ips 127.0.0.1 [your_ips]RPAFsethostname OnRPAFheader X-Forwarded-For#rpaf_module end

[your_ips]请修改为你本机所监听web服务的ip.多个IP用空格空开.

7:pureftpd安装。

8:简单优化

9:编写web控制端。

网上大多流传的是

location / {
 client_max_body_size    100m;
 client_body_buffer_size 128k;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 proxy_pass http://127.0.0.1:81/;
 proxy_set_header   Host   $host;
 proxy_set_header   X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 }

这样做没任何意义,所有的请求全部转发到后端去了。我们只需要Apache来处理PHP部分,所以,正确的配置应该是

location ~ .*\.(php|php5)?$ {
 client_max_body_size    100m;
 client_body_buffer_size 128k;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 proxy_pass http://$host:81/;
 proxy_set_header   Host   $host;
 proxy_set_header   X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 }

如果是这样将变量作为反向代理的地址的话会在Nginx0.6.18以后的版本中都会提示“ no resolver defined to resolve ”的类似错误,而在proxy_pass 中直接设置地址却不会。

原因是Nginx0.6.18以后的版本中启用了一个resolver指令,在使用变量来构造某个server地址的时候一定要用resolver指令来制定DNS服务器的地址,所以解决这个问题的方法很简单:

在nginx的配置文件中的http{}部分添加一行resolver 8.8.8.8;即可.

上面这种方法有一个缺陷,因为使用了resolver,所以效率要低下些。采用第一种方法中类似的 proxy_pass http://127.0.0.1:81/;也有一个问题,因为使用了 proxy_set_header Host $host;这个指令,故如果进行外部代理,可能无法取得正确内容。此事需要使用 proxy_set_header Host $proxy_host;指令来代替原有 proxy_set_header Host $host;

这里有必要说明下php效率问题,suphp比suexec要快一点;比suphp更快的还有suphp_mod_php;再快一些的是mpm-peruser,不过安装配置的麻烦程度也随之递增。考虑到后期部署其他语言的方便性,故选择通用性更强的fcgi模式。
suexec编译选项要注意的事项:
--with-suexec-caller=web-httpd
调用用户,应该跟Apache运行用户保持一致
--with-suexec-uidmin=500
可使用suexec的用户最小uid
--with-suexec-gidmin=500
可使用suexec的用户组最小gid
--with-suexec-userdir=/wwwroot
定义suEXEC允许访问的用户宿主目录下的子目录。suEXEC将以用户身份执行这个目录下的所有可执行程序,所以这些程序必须是"安全程序"。如果使用"简单的"UserDir指令(即不带"*"),则此处应该被设置为相同的值。当UserDir指令所指向的目录与"passwd"文件所指定的用户宿主目录不同时,suEXEC将不会正常工作,其默认值是"public_html"。如果所支持的虚拟主机对每个用户有不同的UserDir ,则应该把他们集中在同一个父目录下,而用这个参数指向这个父目录。如果配置不当,"~userdir"下的cgi请求将无效!
--with-suexec-docroot=/home/
此选项尤为重要,必须是php程序运行的上级目录。不支持通配符*。
--with-suexec-umask=4755
更多配置请看这里
http://lamp.linux.gov.cn/Apache/ApacheMenu/suexec.html

Apache2.2.x安装配置选项略,详情见前面这里
1:mod_fcgid安装
APXS=/usr/local/apache2/bin/apxs ./configure.apxs
make & make install
2:mod_encoding 安装(解决中文路径问题)
详情搜索本站,在此略过。

权限问题:这是最繁琐的问题,折磨我很久的问题,请注意bin目录,build目录,include目录(这里指的是相对应Apache目录,下文中未明确指出的地方,均是指相对Apache目录)下的文件属主必须为root, modules下的httpd.exp属主为root。其他未明确指出的目录或文件,属主应为Apache运行用户。

关于用户的cgi程序目录问题:1,必须隶属于--with-suexec-docroot目录(关于suexec详细参数,可以运行bin/suexec -V查看)。2,cgi程序和cgi的上级目录属主必须为用户本身,且cgi程序有x执行权限。

错误例子:
suexec_log
[2006-01-02 15:03:16]: user mismatch (postfix instead of httpd)
[2006-01-02 15:06:07]: uid: (200/postfix) gid: (200/200) cmd: index.cgi
[2006-01-02 15:06:07]: target uid/gid (200/200) mismatch with directory (502/502) or program (502/502)
[2006-01-02 15:06:11]: uid: (200/postfix) gid: (200/200) cmd: index.cgi
[2006-01-02 15:06:11]: target uid/gid (200/200) mismatch with directory (502/502) or program (502/502)
cgi文件的父目录的属性不匹配,必须也chown 到uid=200的那个。

最近有用户说服务器上无法访问带空格的目录和文件,原因是apache默认只支持拉丁语的url,google下
就是在linux下通过apache访问中文(据测试,日文也可以)的url路径的模块.日本人开发的,不要太愤青了哦。
我的环境:centos5,httpd 2.2.9
测试通过.
注意:需要mod_headers的支持(一般默认apache编译已经有了,所以在有的modules里看不到。)
Url:http://webdav.todo.gr.jp/

1.download and patch

cd /zh/src/
wget http://webdav.todo.gr.jp/download/mod_encoding-20021209.tar.gz
#download patch
wget http://webdav.todo.gr.jp/download/experimental/
         mod_encoding.c.apache2.20040616
tar -xzvf mod_encoding-20021209.tar.gz

用新版本mod_encoding.c覆盖

cp mod_encoding.c.apache2.20040616 mod_encoding-20021209/mod_encoding.c
#download patch
wget http://www.aconus.com/~oyaji/faq/
         mod_encoding.c-apache2.2-20060520.patch
cp mod_encoding.c-apache2.2-20060520.patch mod_encoding-20021209/
cd mod_encoding-20021209
patch -p0 < mod_encoding.c-apache2.2-20060520.patch

2.install iconv-hook
安装mod_encoding前首先需要安装iconv_hook。

cd /zh/src/mod_encoding-20021209/lib
./configure
make
make install