在阿里云上搭建nginx + ThinkPHP 的实践

时间:2023-02-04 12:36:36

作为一个程序猿,理应用linux系统来作为平时的工作机环境,哎,之前倒是用过一段时间的linux,可惜后来换了本本,后来竟然没有保持,嗷嗷后悔中。。。

废话不多说,大家用windows的理由都一样,但终究是要找补回来的,当你搭建一台linux服务器,遇到问题的时候,你发现之前偷的懒都找上来了,诚然,在windows上部署一个php+apache的环境简直是傻瓜至极,so easy..phpstudy分分钟帮你搞定, apache+php+mysql一步到位。

尽管网上都说nginx嗷嗷好,终究由于不熟悉而放弃使用了,用的是著名的 LAMP,也就是 Linux + Apache + Mysql + PHP.

但是由于我们网站是国内某个知名度很高的大网站。。。必须要吹一波,线程动不动飙到400是很容易得事。。。所以感觉很苦恼。 一看进程 httpd线程嗷嗷多,也想用apache 的 mpm worker模式,但是告诉我自己的php是线程安全的,感觉与php版本不搭,于是乎,开始了nginx的坑爹之旅。

首先想到的是,在LAMP上,直接装个nginx不就完了么。那么就只需要nginx就可以了呀,后来发现,完全不行,启动的时候完全不行,首先就是nginx 的conf文件对于解析php的设置,网上版本嗷嗷多,試了好多,终于找到一个可以用的,最后竟然说数据库不支持。。。

后面想着自己从头搭建一台LNMP服务器,发现ali云上有这样的镜像,不用从头来了,于是搞了一个,最后conf的配置如下:

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; events {
worker_connections 1024;
} http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80 default_server;
listen [::]:80 default_server;
server_name yourserver1 yourserver2;
root /usr/share/nginx/xxxx; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

最后记得给你的folder都加上权限。 chmod 777 -R /usr/share/nginx/xxx

毕。