一,安裝nginx:
1,apt方式安裝nginx
root@ku:~# apt-get install nginx
2,安裝完成后可以看到nginx已經啟動運行:
root@ku:/var/www/html# systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-11-18 16:19:26 CST; 31min ago Docs: man:nginx(8) Main PID: 17111 (nginx) Tasks: 3 (limit: 4615) Memory: 4.5M CGroup: /system.slice/nginx.service ├─17111 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─17112 nginx: worker process └─17113 nginx: worker process 11月 18 16:19:26 ku systemd[1]: Starting A high performance web server and a reverse proxy server... 11月 18 16:19:26 ku systemd[1]: Started A high performance web server and a reverse proxy server.
用ss查看端口:
root@ku:/var/www/html# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* users:(("mysqld",pid=6024,fd=32)) LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=6024,fd=34)) LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=17113,fd=6),("nginx",pid=17112,fd=6),("nginx",pid=17111,fd=6)) ...
3,查看默認的配置文件:
ubuntu上默認的站點配置文件位於:/etc/nginx/sites-enabled
查看配置:
root@ku:/etc/nginx/sites-enabled# more default
可以看到默認站點的相關信息:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; server_name _; location / { try_files $uri $uri/ =404; } }
主要內容如上,document root 目錄在 /var/www/html
4,測試在/var/www/html目錄下添加一張圖片並訪問:
root@ku:/etc/nginx/sites-enabled# cp /home/liuhongdi/圖片/1370-macos-big-sur-wallpaper.png /var/www/html/a.png
從瀏覽器查看訪問效果:
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,安裝php-fpm
1,用apt安裝:
root@ku:/etc/nginx/sites-enabled# apt-get install php-fpm
安裝完成后查看php的版本:
root@ku:/var/www/html# php --version PHP 7.4.9 (cli) (built: Oct 26 2020 15:17:14) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies
2,查看php-fpm安裝后是否啟動運行?(默認安裝完成后會自動啟動)
root@ku:/var/www/html# systemctl status php7.4-fpm ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-11-18 16:15:47 CST; 13min ago Docs: man:php-fpm7.4(8) Process: 16977 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock
/etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/> Main PID: 16974 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 1, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 4615) Memory: 9.6M CGroup: /system.slice/php7.4-fpm.service ├─16974 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─16975 php-fpm: pool www └─16976 php-fpm: pool www 11月 18 16:15:47 ku systemd[1]: Starting The PHP 7.4 FastCGI Process Manager... 11月 18 16:15:47 ku systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
可見php-fpm已經運行
三,在nginx配置中添加對php的支持:
1,在站點配置文件中添加對php的支持
root@ku:/etc/nginx/sites-enabled# ls default root@ku:/etc/nginx/sites-enabled# vi default
把以下內容取消注釋:效果如下:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; }
重啟nginx
root@ku:/etc/nginx/sites-enabled# systemctl restart nginx
2,測試php程序能否正常執行:
root@ku:/etc/nginx/sites-enabled# cd /var/www/html/ root@ku:/var/www/html# vi info.php
代碼為:
root@ku:/var/www/html# more info.php <?php phpinfo(); ?>
從瀏覽器查看:訪問以下地址:
http://127.0.0.1/info.php
效果如圖:
從phpinfo的輸出中可以發現還缺少mysql/gd等較常用的擴展,
四,安裝php的常用擴展:
1,安裝
root@ku:/var/www/html# apt-get install php7.4-mysql php-gd root@ku:/var/www/html# apt-get install php7.4-mbstring
2,重啟php-fpm服務:
root@ku:/var/www/html# systemctl restart php7.4-fpm
3,再次從瀏覽器訪問phpinfo,可以發現已經包含了這幾個擴展
五,查看linux的版本
root@ku:~# more /etc/os-release NAME="Ubuntu Kylin" VERSION="20.10 (Groovy Gorilla)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.10" VERSION_ID="20.10" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=groovy UBUNTU_CODENAME=groovy root@ku:~# uname -r 5.8.0-29-generic