Ubuntu 16.04 安装Nginx, MySQL, PHP

1、第一次使用 apt-get 需要更新本地依赖包索引

sudo apt-get update

2、安装Nginx

sudo apt-get install nginx

//在Ubuntu 16.04中,Nginx安装后即开始运行
如果ufw防火墙正在运行,需要设置允许连接到Nginx。即Nginx在安装时注册ufw

sudo ufw allow 'Nginx HTTP'

//通过下面命令验证
sudo ufw status

//成功输出如下
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

//查看nginx版本
nginx -v

//启动nginx
sudo service nginx start

//停止nginx
sudo service nginx stop

//重启nginx
sudo service nginx restart

//执行命令找到nginx路径
ps aux | grep nginx
输出:/usr/sbin/nginx

//查找nginx基本配置
/usr/sbin/nginx -V
输出内容包括:--conf-path=/etc/nginx/nginx.conf   这个是niginx配置文件默认路径

//因为我们用service服务启动nginx,要修改配置文件路径可以修改
/usr/lib/systemd/system/nginx.service这个文件
修改完sudo systemctl reload nginx即可

//Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

//nginx配置文件示例

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80 default_server;
        listen [::]:80 default_server;
        server_name  http://aaa.cn/;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/project/www;
            index index.php  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            #root 指令用于载入web文件根目录
            root           /usr/project/www;
            fastcgi_pass   0.0.0.0:9000;
            fastcgi_index  index.php;
            #php-fpm使用fastcgi_param 指令的 SCRIPT_FILENAME参数决定需要执行哪个脚本
            fastcgi_param  SCRIPT_FILENAME  /usr/project/www$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one

        location ~ /\.ht {
            deny  all;
        }
    }



    server {
        listen       8080;
        server_name  http://aaa.cn/;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/project/exhibitionwebapp;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

   server {
     server_name http://bbb.aaa.cn/;  //二级域名
     listen               80;

     # 首页重定向
     location =/ {
      rewrite ^/ http://aa.hostname.com/aa/index.html last;
     }
    
     # /aa 请求转给后端节点
     location /aa {
       proxy_pass http://127.0.0.1:20003/;
       proxy_redirect default;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $http_connection;
     }
     location /assets {
       alias /home/user/apps/xx/assets/;
     }
     location / {
      return 403;
     }
   }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

注意:
Nginx和PHP-FPM的进程间通信有两种方式,一种是TCP,一种是UNIX Domain Socket.
其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.用哪种取决于你的PHP-FPM配置:
方式1:
php-fpm.conf: listen = 127.0.0.1:9000
nginx.conf: fastcgi_pass 127.0.0.1:9000;
方式2:
php-fpm.conf: listen = /tmp/php-fpm.sock
nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock;
其中php-fpm.sock是一个文件,由php-fpm生成,类型是srw-rw----.

UNIX Domain Socket可用于两个没有亲缘关系的进程,是目前广泛使用的IPC机制,比如X Window服务器和GUI程序之间就是通过UNIX Domain Socket通讯的.这种通信方式是发生在系统内核里而不会在网络里传播.UNIX Domain Socket和长连接都能避免频繁创建TCP短连接而导致TIME_WAIT连接过多的问题.对于进程间通讯的两个程序,UNIX Domain Socket的流程不会走到TCP那层,直接以文件形式,以stream socket通讯.如果是TCP Socket,则需要走到IP层,对于非同一台服务器上,TCP Socket走的就更多了.

UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPM
TCP Socket(本地回环):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPM
TCP Socket(Nginx和PHP-FPM位于不同服务器):
Nginx <=> socket <=> TCP/IP <=> 物理层 <=> 路由器 <=> 物理层 <=> TCP/IP <=> socket <=> PHP-FPM

像mysql命令行客户端连接mysqld服务也类似有这两种方式:
使用Unix Socket连接(默认):
mysql -uroot -p --protocol=socket --socket=/tmp/mysql.sock
使用TCP连接:
mysql -uroot -p --protocol=tcp --host=127.0.0.1 --port=3306
//修改配置文件后
nginx -t 
当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径:
nginx的配置文件在/etc/nginx/nginx.conf, vim /etc/nginx/nginx.conf

sudo service nginx restart

//如果域名已绑定云主机并且启动了DNS解析,通过域名和ip地址可以访问nginx服务

Ubuntu 16.04 安装Nginx, MySQL, PHP_第1张图片

3、安装MySQL

sudo apt-get install mysql-server

//会要求输入root用户的密码
//执行完后MySQL已经安装完毕,下面是配置

//运行一个简单的安全脚本,它会询问我们是否要修改一些不安全的默认值。
sudo mysql_secure_installation

//当MySQL的系统库(mysql系统库)发生故障或需要新加一个mysql实例时,需要初始化mysql数据库。例如服务器上已经安装了3306端口的mysql服务,需要再启一个3308端口的mysql服务
这是基础命令,详细可自行搜查
sudo /usr/local/mysql/bin/mysql_install_db

//查看mysql版本
mysql -V

//查看mysql默认读取my.cnf的目录
如果没有设置使用指定目录的my.cnf,mysql启动时会读取安装目录根目录及默认目录下的my.cnf文件。
//查看mysql启动时读取配置文件的默认目录

mysql --help|grep 'my.cnf'

输出:
                          order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf 这些就是mysql默认会搜寻my.cnf的目录,顺序排前的优先。

//在我的机子上,/etc/mysql/my.cnf  引用了  /etc/mysql/mysql.cnf  二重引用了 /etc/mysql/mysql.conf.d/mysqld.cnf



//启动MySQL
sudo service mysql start


//连接mysql客户端
sudo mysql -u root


#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size         = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries       = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size   = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

4、安装PHP

//除了源码编译安装也可以apt安装
sudo apt-get install php-fpm php-mysql

//启动 php-fpm

sudo service php-fpm start
或
sudo /etc/init.d/php-fpm start


//查看配置文件目录
php -i | grep php.ini 

Configuration File (php.ini) Path => /usr/local/php/lib
Loaded Configuration File => /usr/local/php/lib/php.ini

//在Linux下可以使用bash命令脚本:
sudo find / -name 'php.ini'
这个语句则会在你整个系统盘中查找php.ini的位置,如果系统很大的话这个可能速度就比较慢,所有如果你确定php.ini是在/etc的话,则可以把bash脚本中的'/'换成“/etc”.

例
find / -name nginx.conf
find / -name php.ini
find / -name my.cnf
find / -name httpd.conf


//配置php.ini
sudo vi php.ini

//把 cgi.fix_pathinfo设置为0,这是一个非常不安全的设置,因为它告诉PHP尝试执行最接近的文件,如果找不到请求的PHP文件,它可以找到。这基本上将允许用户以允许他们执行不应该被允许执行的脚本的方式来制作PHP请求。
cgi.fix_pathinfo=0

//重启,重新加载配置
sudo systemctl restart php7.0-fpm


//查看php已安装扩展命令
php -m 


//配置好nginx后,新建php文件测试
sudo nano /usr/project/www/test.php

//写入
Ubuntu 16.04 安装Nginx, MySQL, PHP_第2张图片

你可能感兴趣的:(Ubuntu 16.04 安装Nginx, MySQL, PHP)