Gentoo 安装配置Nginx+PHP

Gentoo 安装配置Nginx+PHP

在Gentoo中安装Nginx是一件很简单的事情,但是需要做一些配置,不然nginx是无法运行的。
因为Gentoo是完全由个人控制的,所以在安装nginx时需要手动配置需要安装哪些模块如果不进行配置那么nginx时无法运行的会出现以下错误

unknown directive “autoindex” in /etc/nginx/nginx.conf:15

主要有以下三类模块
1.NGINX_MODULES_HTTP
2.NGINX_MODULES_MAIL
3.NGINX_ADD_MODULES
这三类模块的配置需要在/etc/protage/make.conf中进行配置
例如我们要安装gzip和目录浏览模块那么需要在配置文件中进行以下配置

NGINX_MODULES_HTTP=”fastcgi gzip autoindex”

然后通过emerge -av nginx就可以安装了,其中上面的gzip模块必须要配置不然nginx是无法启动的,会报错

unknown directive “gzip” in /etc/nginx/nginx.conf:30

这是因为我们在配置文件中设置的gzip选项为off,而我们并没有安装gzip模块所以会出现报错,同时我们也启用了网站的目录浏览功能。
这时候看看我们的配置文件

user nginx nginx;
worker_processes 1;error_log /var/log/nginx/error_log info;events {worker_connections 1024;use epoll;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main'$remote_addr - $remote_user [$time_local] ''"$request" $status $bytes_sent ''"$http_referer" "$http_user_agent" ''"$gzip_ratio"';client_header_timeout 10m;client_body_timeout 10m;send_timeout 10m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 4 2k;request_pool_size 4k;gzip off;output_buffers 1 32k;postpone_output 1460;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 75 20;ignore_invalid_headers on;index index.html;server {listen 127.0.0.1;server_name localhost;location / {autoindex on;autoindex_localtime on; #启用目录浏览功能}access_log /var/log/nginx/localhost.access_log main;error_log /var/log/nginx/localhost.error_log info;#我自己的文档目录root /home/jacky/jianguoyun/WAMP/www;}# SSL example#server {#       listen 127.0.0.1:443;#       server_name localhost;#       ssl on;#       ssl_certificate /etc/ssl/nginx/nginx.pem;#       ssl_certificate_key /etc/ssl/nginx/nginx.key;#       access_log /var/log/nginx/localhost.ssl_access_log main;#       error_log /var/log/nginx/localhost.ssl_error_log info;#       root /var/www/localhost/htdocs;#}
}

这时候我们启动nginx

gentoo ~ # /usr/sbin/nginx
gentoo ~ # tail /var/log/nginx/error_log

此时看到以下内容
2017/11/30 16:47:54 [notice] 10378#0: using the “epoll” event method
2017/11/30 16:47:54 [notice] 10378#0: nginx/1.12.1
2017/11/30 16:47:54 [notice] 10378#0: OS: Linux 4.12.12-gentoo
2017/11/30 16:47:54 [notice] 10378#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2017/11/30 16:47:54 [notice] 10379#0: start worker processes
2017/11/30 16:47:54 [notice] 10379#0: start worker process 10380
说明nginx已经启动成功了,这时候通常情况下我们就能通过http://localhost进行访问了,但是我们这里遇到了403错误,网上百度了以下可能是目录权限问题,我将我的文档目录权限设置为755就好了

chmod 755 -R /home/jacky/jianguoyun/WAMP/www

PHP支持

nginx对php的支持首先需要在安装nginx的时候开启fastcgi模块,同时在安装php的时候也要添加php-fpm模块的支持

echo “dev-lang/php fpm” >> /etc/portage/package.use/php
emerge -av php

安装完php后需要在nginx配置文件中添加php的配置,在原有配置文件基础上添加

server {listen 127.0.0.1;server_name localhost;location / {autoindex on;autoindex_localtime on; #启用目录浏览功能}access_log /var/log/nginx/localhost.access_log main;error_log /var/log/nginx/localhost.error_log info;#我自己的文档目录root /home/jacky/jianguoyun/WAMP/www;#phplocation ~ \.php$ {#try_files $uri =404;include /etc/nginx/fastcgi.conf;fastcgi_pass 127.0.0.1:9000;}}

这样我们就完成了基础的php环境支持,在nginx文档目录下面新建一个test.php文件

<html><head><title>test</title></head><body><?phpphpinfo();?> </body></html>

访问这个php文件就能验证nginx对php的支持是否已经成功