Ubuntu 16.04 安裝Nginx, MySQL, PHP

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

sudo apt-get update

2、安裝Nginx

sudo apt-get install nginx

//在Ubuntu 16.04中,Nginx安裝后即開始運(yùn)行
如果ufw防火墻正在運(yùn)行,需要設(shè)置允許連接到Nginx。即Nginx在安裝時(shí)注冊(cè)u(píng)fw

sudo ufw allow 'Nginx HTTP'

//通過下面命令驗(yàn)證
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

//啟動(dòng)nginx
sudo service nginx start

//停止nginx
sudo service nginx stop

//重啟nginx
sudo service nginx restart

//執(zhí)行命令找到nginx路徑
ps aux | grep nginx
輸出:/usr/sbin/nginx

//查找nginx基本配置
/usr/sbin/nginx -V
輸出內(nèi)容包括:--conf-path=/etc/nginx/nginx.conf   這個(gè)是niginx配置文件默認(rèn)路徑

//因?yàn)槲覀冇胹ervice服務(wù)啟動(dòng)nginx,要修改配置文件路徑可以修改
/usr/lib/systemd/system/nginx.service這個(gè)文件
修改完sudo systemctl reload nginx即可

//Systemctl是一個(gè)systemd工具,主要負(fù)責(zé)控制systemd系統(tǒng)和服務(wù)管理器。
Systemd是一個(gè)系統(tǒng)管理守護(hù)進(jìn)程、工具和庫(kù)的集合,用于取代System V初始進(jìn)程。Systemd的功能是用于集中管理和配置類UNIX系統(tǒng)。

//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參數(shù)決定需要執(zhí)行哪個(gè)腳本
            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/;  //二級(jí)域名
     listen               80;

     # 首頁(yè)重定向
     location =/ {
      rewrite ^/ http://aa.hostname.com/aa/index.html last;
     }
    
     # /aa 請(qǐng)求轉(zhuǎn)給后端節(jié)點(diǎn)
     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的進(jìn)程間通信有兩種方式,一種是TCP,一種是UNIX Domain Socket.
其中TCP是IP加端口,可以跨服務(wù)器.而UNIX Domain Socket不經(jīng)過網(wǎng)絡(luò),只能用于Nginx跟PHP-FPM都在同一服務(wù)器的場(chǎng)景.用哪種取決于你的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是一個(gè)文件,由php-fpm生成,類型是srw-rw----.

UNIX Domain Socket可用于兩個(gè)沒有親緣關(guān)系的進(jìn)程,是目前廣泛使用的IPC機(jī)制,比如X Window服務(wù)器和GUI程序之間就是通過UNIX Domain Socket通訊的.這種通信方式是發(fā)生在系統(tǒng)內(nèi)核里而不會(huì)在網(wǎng)絡(luò)里傳播.UNIX Domain Socket和長(zhǎng)連接都能避免頻繁創(chuàng)建TCP短連接而導(dǎo)致TIME_WAIT連接過多的問題.對(duì)于進(jìn)程間通訊的兩個(gè)程序,UNIX Domain Socket的流程不會(huì)走到TCP那層,直接以文件形式,以stream socket通訊.如果是TCP Socket,則需要走到IP層,對(duì)于非同一臺(tái)服務(wù)器上,TCP Socket走的就更多了.

UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPM
TCP Socket(本地回環(huán)):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPM
TCP Socket(Nginx和PHP-FPM位于不同服務(wù)器):
Nginx <=> socket <=> TCP/IP <=> 物理層 <=> 路由器 <=> 物理層 <=> TCP/IP <=> socket <=> PHP-FPM

像mysql命令行客戶端連接mysqld服務(wù)也類似有這兩種方式:
使用Unix Socket連接(默認(rèn)):
mysql -uroot -p --protocol=socket --socket=/tmp/mysql.sock
使用TCP連接:
mysql -uroot -p --protocol=tcp --host=127.0.0.1 --port=3306
//修改配置文件后
nginx -t 
當(dāng)你執(zhí)行 nginx -t 得時(shí)候,nginx會(huì)去測(cè)試你得配置文件得語(yǔ)法,并告訴你配置文件是否寫得正確,同時(shí)也告訴了你配置文件得路徑:
nginx的配置文件在/etc/nginx/nginx.conf, vim /etc/nginx/nginx.conf

sudo service nginx restart

//如果域名已綁定云主機(jī)并且啟動(dòng)了DNS解析,通過域名和ip地址可以訪問nginx服務(wù)

3、安裝MySQL

sudo apt-get install mysql-server

//會(huì)要求輸入root用戶的密碼
//執(zhí)行完后MySQL已經(jīng)安裝完畢,下面是配置

//運(yùn)行一個(gè)簡(jiǎn)單的安全腳本,它會(huì)詢問我們是否要修改一些不安全的默認(rèn)值。
sudo mysql_secure_installation

//當(dāng)MySQL的系統(tǒng)庫(kù)(mysql系統(tǒng)庫(kù))發(fā)生故障或需要新加一個(gè)mysql實(shí)例時(shí),需要初始化mysql數(shù)據(jù)庫(kù)。例如服務(wù)器上已經(jīng)安裝了3306端口的mysql服務(wù),需要再啟一個(gè)3308端口的mysql服務(wù)
這是基礎(chǔ)命令,詳細(xì)可自行搜查
sudo /usr/local/mysql/bin/mysql_install_db

//查看mysql版本
mysql -V

//查看mysql默認(rèn)讀取my.cnf的目錄
如果沒有設(shè)置使用指定目錄的my.cnf,mysql啟動(dòng)時(shí)會(huì)讀取安裝目錄根目錄及默認(rèn)目錄下的my.cnf文件。
//查看mysql啟動(dòng)時(shí)讀取配置文件的默認(rèn)目錄

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默認(rèn)會(huì)搜尋my.cnf的目錄,順序排前的優(yōu)先。

//在我的機(jī)子上,/etc/mysql/my.cnf  引用了  /etc/mysql/mysql.cnf  二重引用了 /etc/mysql/mysql.conf.d/mysqld.cnf



//啟動(dòng)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

//啟動(dòng) 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'
這個(gè)語(yǔ)句則會(huì)在你整個(gè)系統(tǒng)盤中查找php.ini的位置,如果系統(tǒng)很大的話這個(gè)可能速度就比較慢,所有如果你確定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設(shè)置為0,這是一個(gè)非常不安全的設(shè)置,因?yàn)樗嬖VPHP嘗試執(zhí)行最接近的文件,如果找不到請(qǐng)求的PHP文件,它可以找到。這基本上將允許用戶以允許他們執(zhí)行不應(yīng)該被允許執(zhí)行的腳本的方式來制作PHP請(qǐng)求。
cgi.fix_pathinfo=0

//重啟,重新加載配置
sudo systemctl restart php7.0-fpm


//查看php已安裝擴(kuò)展命令
php -m 


//配置好nginx后,新建php文件測(cè)試
sudo nano /usr/project/www/test.php

//寫入
<?php
phpinfo();

//http訪問
http://server_domain_or_IP/test.php

可以看到php配置信息打印在瀏覽器上

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 更改ip和dnsVi /etc/sysconfig/network-scripts/ifcfg-eth0vi /...
    Xwei_閱讀 2,044評(píng)論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,604評(píng)論 19 139
  • 早餐同事談起她的甲亢,突然發(fā)現(xiàn)原來這個(gè)病可能一直反復(fù),所以一直需要吃藥,驚訝追問后只剩涼了一截的心,對(duì)于總愛置身事...
    苾兒閱讀 242評(píng)論 0 0
  • 又是黃昏。 光線不知覺間由明亮轉(zhuǎn)而灰暗,模糊了色彩、形狀、目所能及的每一個(gè)角落,還有我的心。 我的心,散散淡淡,霧...
    薤露行閱讀 629評(píng)論 5 14
  • 計(jì)劃著自己做油條很長(zhǎng)時(shí)間,于是趁著周末早起配齊面粉,雞蛋,油條膨松劑,白糖,菜油等原材料。 將5克油條膨松劑、20...
    財(cái)富阿拉丁閱讀 564評(píng)論 5 4

友情鏈接更多精彩內(nèi)容