安装完成nginx后,我们网站目录默认在/var/www,默认一个站点的,多个站点需要在/var/www中创建多个站点。我们一般以域名方式创建一个站点目录,然后设置站点目录权限(使用管理员账号操作),nginx配制文件默认在/etc/nginx/sites-available目录中,在此目录中创建多个站点配制文件,然后把配制文件软链接复制到/etc/nginx/sites-enabled目录中,最后重启nginx生效(域名要解析到服务器)。
创建站点目录
sudo mkdir -p /var/www/nyw.com
sudo mkdir -p /var/www/mgj.com
给站点目录设置权限(www-data:www-data 就是 Nginx 默认的用户组合用户名)
sudo chown -R www-data:www-data /var/www/nyw.com
sudo chown -R www-data:www-data /var/www/mgj.com
创建新站点配制文件,复制一份在修改吧
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mgj.com
创建软链接
ln -s /etc/nginx/sites-available/mgj.com /etc/nginx/sites-enabled/mgj.com
修改配制文件后,重启nginx生效
如下是站点nginx配制文件
server {
listen 80;
listen [::]:80;
# 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/mgj.nyw.com;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name mgj.nyw.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
在nginx配制文件中设置项目伪静态,添加如下我画出来的配制
切换服务器php版本号
sudo update-alternatives --config php