1、安装:
sudo apt-get install nginx
2、配置文件
vi /etc/nginx/sites-enabled/default
server {
listen 8080 default_server; //我80端口已被用,所以设为8080
listen [::]:8080 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html2; //网站目录
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html; //这就是入口文件,会按顺序找
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
3、在/var/www/html2 目录下建立相应的文件
vi index.html
放入一些内容
这时,打开浏览器输入localhost:8080就可以看到入口文件的内容了(:,是不是很好用,很神奇?
4、不过默认是不能解析php文件的哦。
vi index.php
当输入http://localhost:8080/index.php时,会自动下载此文件。):
如何可以解析php呢??上网找吧。。。。。。
答案:http://www.linuxidc.com/Linux/2016-05/131154.htm
以下是当使用tp5框架时,nginx的配置,具体参数根据自身修改。
server {
listen 8080;
server_name guoshu.com;
access_log /var/www/html2/nginx.log;
error_log /var/www/html2/nginx_error.log;
set $root /var/www/html2;
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root $root;
}
location / {
root $root;
index index.html index.php;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ .+\.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000; #如果是php5.5的,用这个可以
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
include fastcgi_params;
}
}