linux学习(二) -- ubuntu下lnmp环境的配置

亲测的教程,,希望能对大家提供些许帮助,转载请注明出处
ubuntu+nginx+mysql+php7

一.安装Nginx

1、首先添加nginx_signing.key(必须,否则出错)
$ wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key add nginx_signing.key
2、添加Nginx官方提供的源
$ echo "deb http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
$ echo "deb-src http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
3、更新源并安装Nginx
$ sudo apt-get update
$ sudo apt-get install nginx
安装Nginx完成后可查看版本号,输入
$ /usr/sbin/nginx -v
至此Nginx安装完成,此时访问IP可以看到Welcome Nginx的界面。

二.安装mysql

# 安装MySQL,可选
sudo apt-get install mysql-server
# MySQL初始化配置
sudo mysql_install_db
# MySQL安全配置
sudo mysql_secure_installation

三.安装php7

1、添加PPA(PHP 7 is not available for Ubuntu Trusty in the standard Ubuntu repositories)
$ sudo apt-get install python-software-properties software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
2、安装php7以及所需的一些扩展
$ sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom
3、配置php7
打开php.ini配置文件:
$ sudo vim /etc/php/7.0/fpm/php.ini
找到cgi.fix_pathinfo选项,去掉注释;,然后将值设置为0:
cgi.fix_pathinfo=0
启用php7.0-mcrypt
$ sudo phpenmod mcrypt
启动后重启php7.0-fpm,输入:
$ sudo service php7.0-fpm restart

四.配置

 1>主配置文件 /etc/nginx/nginx.conf
------------------------------------------------------------------------------------------------------------------------------------------------
#用户名给成默认的 www-data
user www-data;
worker_processes 1;
 
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
 
 
events {
worker_connections 1024;
}
 
 
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
 
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
 
access_log /var/log/nginx/access.log main;
 
sendfile on;
#tcp_nopush on;
 
keepalive_timeout 65;
 
#gzip on;
 
include /etc/nginx/conf.d/*.conf;
 
#这里引入自己的配置文件
include /etc/nginx/sites/*;
 
}
 
------------------------------------------------------------------------------------------------------------------------------------------------
 
2.个人站点的配置 /etc/nginx/sites/xxx.conf
------------------------------------------------------------------------------------------------------------------------------------------------
server{
  #分别写了www和不带www的域名
  server_name www.zhengde.site zhengde.site;
  listen 80;
  #工程根目录
  root /var/data/blog/;
  charset UTF-8;
  #日志文件位置,自己选择
  access_log /var/log/nginx/blog/access.log;
  error_log /var/log/nginx/blog/error.log;
 
  index index.php index.html index.htm;
 
  #error_page 500 502 503 504 404 /missing.html;
  #error_page 403 =404 /missing.html;
 
  #主要作用是xxxx/index.php/控制器/方法,一般的index.php是隐藏的,不写这句就找不到了
  location / {
    try_files $uri /index.php$is_args$args;
  }
 
  location ~ .php$ {
    #这个要看你监听的位置
  fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  
  #二选一,第一种写法
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    #二选一,第二种写法,但是需要在外层创建 fastcgi.conf文件
    # include fastcgi.conf;
  }
}
------------------------------------------------------------------------------------------------------------------------------------------------
 
 以上是基本的配置.如果你想更进一步学习nginx,建议找一本书籍,这样更系统,更权威.
 
主要参考
https://segmentfault.com/a/1190000005796170
 
 
 
原文地址:https://www.cnblogs.com/redirect/p/6369684.html