Nginx + MySQL + PHP 开发技巧

发布于 2023-08-10 18:31:55 字数 3977 浏览 64 评论 0

Nginx

安装 nginx:

sudo apt-get update
sudo apt-get install nginx

MySQL

安装 Mysql

sudo apt-get install mysql-server mysql-client

安装数据库结构:

sudo mysql_install_db

进行一些安全设置:

sudo mysql_secure_installation

PHP

安装 php:

sudo apt-get install php5-fpm php5-mysql php5-cli

配置 php 进程

sudo nano /etc/php5/fpm/php.ini

cgi.fix_pathinfo 设置为 0

cgi.fix_pathinfo=0
sudo service php5-fpm restart

配置 nginx 与 php:

sudo nano /etc/nginx/sites-available/default
server {
  listen 80;

  root /usr/share/nginx/html;
  index index.php index.html index.htm;

  server_name localhost;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}
sudo service nginx restart

添加 PHP 扩展

sudo apt-get install php5-curl php5-mcrypt php5-intl php5-apcu php5-redis php5-memcached php5-mongo
sudo php5enmod mcrypt

配置域名

新建目录

/var/www 下新建站点目录:

sudo mkdir -p /var/www/example.com/html

赋予权限:

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www

配置文件

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

example.com

server {
  listen 80;

  root /var/www/example.com/html;
  index index.php index.html index.htm;

  server_name example.com www.example.com;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /var/www/example.com/html;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

启用站点:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

移除默认站点:

sudo rm /etc/nginx/sites-enabled/default

配置:

sudo nano /etc/nginx/nginx.conf
server_names_hash_bucket_size 64;
sudo service nginx restart

for php7.0 on Ubuntu 16.04 LTS

server {
  listen 80;

  root /usr/share/nginx/html/light;
  index index.php index.html index.htm;

  server_name light.dev;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}
gzip on;

 gzip_vary on;
 gzip_proxied any;
 gzip_comp_level 6;
 gzip_buffers 16 8k;
 gzip_http_version 1.1;
 gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

单身狗的梦

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文