顯示具有 apache 標籤的文章。 顯示所有文章
顯示具有 apache 標籤的文章。 顯示所有文章

2021-05-19

apache + php 連線過多卡住

 apache + php 連線過多卡住,若是資料庫問題,可考慮使用proxysql,畢竟php沒有sql thread pool,就算是用 pconnect() 設定上也是很麻煩,也且一個連線也只能給同一個process的thread共用,只能說是半套,那不如直接用connect()連完就drop即可。

若不是資料庫而是apache撐不住,可以調整 prefork模組的設定(可用apachectl -V或httpd -V查看是不是prefork模式),

譬如 apache 2.4的/etc/apache2/mods-available/mpm_prefork.conf:

<IfModule mpm_prefork_module>

        StartServers            4

        MinSpareServers         15

        MaxSpareServers         75

        ServerLimit             3000

        MaxRequestWorkers       3000

        MaxConnectionsPerChild  9000

</IfModule>

或apacpe 2.0 的 /etc/httpd/conf/httpd.conf

<IfModule prefork.c>

StartServers       8

MinSpareServers    15

MaxSpareServers   75

ServerLimit      3000

MaxClients       3000

MaxRequestsPerChild  9000

</IfModule>


2018-07-16

Python CGI on Apache & FASTCGI on NginX

在Apache2上修改http.conf,讓Apache能以CGI執行python:
==============================================
...
LoadModule cgid_module modules/mod_cgid.so
...
<Directory "/opt/bitnami/apache2/htdocs">
...
Options FollowSymLinks MultiViews +ExecCGI
AddHandler cgi-script .py
...
</Directory>
==========================================================
若是使用bitnami LAMP stack,需再同時修改 conf/bitnami/bitnami.conf,修改設定同上
==========================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

==========================================================
在NginX上修改nginx.conf,加入以下片段,以fast-cgi執行python:
==========================================================
location ~ \.py$ {
    root html;
    fastcgi_read_timeout 300;
    #fastcgi_pass 127.0.0.1:10240;
    fastcgi_pass unix:/tmp/python-fcgi.sock; #使用.sock監聽
    fastcgi_index index.py;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
}
==========================================================
若是使用bitnami,可以新建一個 conf/bitnami/pythonfastcgi.conf,加入如上設定,並修改 conf/bitnami.conf,加入:

include "/Applications/nginxstack-1.10.1-2/nginx/conf/bitnami/pythonfastcgi.conf";
==========================================================
然後python需安裝flup,用來當做fastcgi的gateway(即符合fastcgi協定的SERVER,這樣才能溝通):
pip3 install flup

然後寫一支 wsgi.py,並啟動(以/tmp/python-fcgi.sock監聽):
==========================================================
#!/usr/local/bin/python3
#coding=utf-8

from html import escape
import sys, os
from flup.server.fcgi import WSGIServer

def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    yield '<h1>FastCGI Environment</h1>'
    yield '<table>'
    for k, v in sorted(environ.items()):
        yield '<tr><th>%s</th><td>%s</td></tr>' % (escape(k), escape(v))
    yield '</table>'

if __name__ == '__main__':
    # WSGIServer(app, bindAddress=('127.0.0.1',10240)).run()
    WSGIServer(app, bindAddress='/tmp/python-fcgi.sock').run()
==========================================================
修改為執行檔並執行,再執行nginx:
chmod +x wsgi.py
./wsgi.py
systemctl start nginx

uWSGI是一個軟體專案名稱,而flup本身就是一個架構在uWSGI專案之上的server,同時支援了:
wsgi-->可支援fastcgi
uwsgi-->python web server protocol
http協定
等,因此Nginx也可以改用uwsgi(設定uwsgi_pass)去執行,可參考下面最後一個鏈結

ref:
https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
https://httpd.apache.org/docs/2.4/howto/cgi.html
http://uwsgi-docs.readthedocs.io/en/latest/WebServers.html
https://segmentfault.com/a/1190000003993249
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

2015-02-03

將.htaccess轉成nginx重寫規則

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

APACHE & PHP 基本安全設定

.htaccess
http://www.socss.cn/2011/php_0902/167.html

php.ini
http://jamesbond0479.blog.163.com/blog/static/241475820115231759458/

在php的生產環境下建議做一些額外的安全措施:
enable_dl = Off
disable_functions = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source

如果你建立的是虛擬主機那麼你需要一份更長的函數列表:
disable_functions = escapeshellarg, escapeshellcmd, exec, passthru, proc_close, proc_get_status, proc_open, proc_nice, proc_terminate, shell_exec, system, ini_restore, popen, dl, disk_free_space, diskfreespace, set_time_limit, tmpfile, fopen, readfile, fpassthru, fsockopen, mail, ini_alter, highlight_file, openlog, show_source, symlink, apache_child_terminate, apache_get_modules, apache_get_version, apache_getenv, apache_note,apache_setenv, parse_ini_file

2011-07-29

反盜連的.htaccess寫法

常逛論壇的一定知道,「盜連的會全家死光光」
但,上天有好生之德,為了避免這個情形,也為了積點陰德,
更為了不要讓我的dreamhost主機爆流量,我自動幫某些客戶的網站小改了.htaccess,
當然RewriteEngine要開啟。