Nginx + MySQL + PHP 开发技巧
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论