ubuntu 18.04 安裝 LNMP

20181228 ubuntu 18.04 安裝 LNMP

開始時是要學習 Docker 中 安裝 nginx、php、mysql的
開始的時候虛擬機上 ubuntu 18.04 an zhao按照官網已經安裝過了 LAMP
然後就總是出現這樣那樣的錯誤!浪費無數腦細胞之後,決定從頭開始!
安裝完 ubuntu 18.04 之後,不要安裝 LAMP ,而是按照 LNMP 的次序一步一步的來過!

1、安裝 nginx

sudo apt install nginx

確認:查看端口
netstat -anp |grep 80
或者
sudo lsof -i:80
	COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
	nginx   1750     root    6u  IPv4  22825      0t0  TCP *:http (LISTEN)
	nginx   1750     root    7u  IPv6  22826      0t0  TCP *:http (LISTEN)
	nginx   1752 www-data    6u  IPv4  22825      0t0  TCP *:http (LISTEN)
	nginx   1752 www-data    7u  IPv6  22826      0t0  TCP *:http (LISTEN)

確認:cha查看 nginx 服務
sudo systemctl status nginx

2 、安裝 php

sudo apt install php php-fpm
sudo apt-get install php7.2 php7.2-fpm
確認:查看版本
php -v
	PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
	Copyright (c) 1997-2018 The PHP Group
	Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
		with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

確認:查看進程
ps -ef |grep php
	root     10848     1  0 12:41 ?        00:00:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
	www-data 10862 10848  0 12:41 ?        00:00:00 php-fpm: pool www
	www-data 10863 10848  0 12:41 ?        00:00:00 php-fpm: pool www
	dhbm     10983  1232  0 12:48 pts/0    00:00:00 grep --color=auto php

3 、修改站點配置,測試 php 文件解析

1)、新建一個 php 文件 
	cd /var/www/html
	sudo vim info.php
加入以下 3 行
	<?php
	 phpinfo();
	?>
2)、修改站點配置
cd /etc/nginx/sites-available/
	** a. 去掉 location ~ \.php$ { 這行的註釋,同時去掉配對的 } 這行的註釋
	** b. 去掉 fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 這行的註釋
	** c. 同時,修改成對應的 php-fpm 版本號的文件名(我安裝的是 php7.2)
3)、測試 nginx,重啓 nginx 服務
nginx -t
		nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
	...

需要 sudo

sudo nginx -t
	nginx: [emerg] "fastcgi_pass" directive is not allowed here in /etc/nginx/sites-enabled/default:62
	nginx: configuration file /etc/nginx/nginx.conf test failed
按照錯誤提示的行號(我的錯誤在 62 行),檢查以上 a,b,c 處修改正確否!

4)、紀錄一下修改的結果
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    # fastcgi_pass 127.0.0.1:9000;
    }

5)、正確測試結果
	sudo nginx -t
		nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
		nginx: configuration file /etc/nginx/nginx.conf test is successful
6)、正確之後再重啓 nginx 服務
	sudo systemctl restart nginx
	
	** 不確認的話,可以查看 nginx 服務狀態!
		sudo systemctl status nginx
	**中途總是折騰不對時,多次 reboot ,實踐證明:必須 nginx 測試正確後,重啓服務纔會正常!
7)、測試結果
	本地測試: 
		curl 127.0.0.1
		curl 127.0.0.1/info.php
	瀏覽器測試:
		http://192.168.1.191/
		http://192.168.1.191/info.php

4、錯誤處理:nginx打開php文件總是顯示下載,並開始下載我的 info.php 文件

這是之前學習時遇到的問題(另一個虛擬機)
原因同上!

5、錯誤處理:nginx打開php文件,顯示 502 Bad Gateway

回頭處理之前學習時遇到的問題(另一個虛擬機)
不出現下載了,但是,出來 502 Bad Gateway 錯誤

檢查 /etc/nginx/sites-available/default 配置中 php部分
	fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

cd /var/run/php/
ls
	php7.2-fpm.pid
	
只有一個 pid 文件!沒有發現存在這個文件 php7.2-fpm.sock ?
怎麼安裝的 php-fpm?

sudo vim /etc/nginx/sites-available/default
先改用 php-cgi 方式
fastcgi_pass 127.0.0.1:9000;

重啓 php7.2-fpm 服務
	sudo systemctl restart php7.2-fpm.service
重啓 nginx
	sudo systemctl restart nginx

再次測試
http://192.168.1.192/info.php
可以看到正確信息了
 PHP Version 7.2.10-0ubuntu0.18.04.1
..
*** php-cgi 方式 和 php-fpm 方式差別在哪裏來着?需要再次學習去!

***經過回憶:
之前的虛擬機上的 php 是直接按照ubuntu 18.04 官網介紹一次整體安裝了 LAMP
之後是自己從頭 LNMP 一個一個手工安裝的!

6、安裝 mysql

sudo apt install mysql-server

確認
	sudo netstat -tap | grep mysql
	tcp        0      0 localhost.localdo:mysql 0.0.0.0:*               LISTEN      2808/mysqld 

7、設置 mysql 初始密碼

sudo mysql_secure_installation
返回結果如下:
	Securing the MySQL server deployment.

	Connecting to MySQL using a blank password.

	VALIDATE PASSWORD PLUGIN can be used to test passwords
	and improve security. It checks the strength of password
	and allows the users to set only those passwords which are
	secure enough. Would you like to setup VALIDATE PASSWORD plugin?

	Press y|Y for Yes, any other key for No: y     

	There are three levels of password validation policy:

	LOW    Length >= 8
	MEDIUM Length >= 8, numeric, mixed case, and special characters
	STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

** 學習用,簡單設置 12345678
	後續一路 y

8、安裝 phpmyadmin

** 我是按照ubuntu 18.04 官網操作的,但是,她主要針對得失 apache2
** 這次沒有安裝 apache2,所以,中途注意不要讓它自動配置
** 拿不準的話,直接去下載 phpmyadmin 然後,按照 nginx 網站去配置

sudo apt install phpmyadmin
** 注意:Web 自動選擇: apache ,lightd 一個都不選!

摘錄一部分安裝提示如下
...
	should most likely be provided in /usr/share/doc/phpmyadmin.
	...
	Creating config file /etc/phpmyadmin/config-db.php with new version
	...

收到以上提示信息影響,第一次我給配置到了 phpmyadmin 文檔,也紀錄一下
cd /usr/share/doc/phpmyadmin
ls
	changelog.Debian.gz  copyright  html            README            TODO.Debian
	CONTRIBUTING.md      examples   NEWS.Debian.gz  README.Debian.gz

9、新建一個網站 phpmyadmin

1)、爲了統一,將 phpmyadmin ln 到 /var/www
	這是第一次錯誤的操作,ln 到 phpmyadmin 文檔 了
	sudo ln -s /usr/share/doc/phpmyadmin/html /var/www/phpmyadmin

	正確的 ln 路徑
	sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin

2)、直接 cp default 過來修改
	cd /etc/nginx/sites-available/
	sudo cp default phpmyadmin

3)sudo vim /etc/nginx/site-available/phpmyadmin
	修改這個 phpmyadmin 以下 2 處
	listen 999;
	 root /var/www/phpmyadmin;

4)、連接到 sites-enabled
	sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/phpmyadmin

5)測試、重啓 nginx 服務
	sudo nginx -t
	必須測試正確!
	sudo systemctl restart nginx

10、 測試結果

本地先測試
curl 127.0.0.1:999
	出現403 錯誤
		403 Forbidden
	據說是首頁沒有加上 index.php
	
curl 127.0.0.1:999/index.php
	ok!說明 403確實是因爲:首頁沒有加上 index.php

瀏覽器測試
http://192.168.1.191:999
出錯誤了:
403 Forbidden

加上 php文件路徑
http://192.168.1.191:999/index.php

原因同上!

修改之後再來!
vim /etc/nginx/sites-availablephpmyadmin 
	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章