1. 程式人生 > >Ubuntu 18.04.1 下快速搭建 LNMP環境

Ubuntu 18.04.1 下快速搭建 LNMP環境

tro 很多 error: 管理員權限 conf request http lar mysql的安裝

1.Nginx的安裝

Nginx安裝是屬於最簡單的,只需要在命令行執行

sudo apt-get install nginx

就能自動安裝 Nginx,其中過程中需要 選擇 Y/n 的選擇Y就行了,當安裝結束後再執行下列代碼,重啟Nginx服務器

sudo service nginx restart

然後通過chrome瀏覽器訪問 localhost,如果出現Nginx的歡迎頁面,就代碼Nginx安裝成功

2:MySQL的安裝

由於咱們使用的Ubuntu18.04,如果直接不選MySQL版本的話,則自動安裝最新的MySQL,所以,在此次安裝中,就自動安裝了MySQL5.7;

執行命令如下:

sudo apt-get install mysql-server mysql-client 

雖然我看到很多教程上說,在安裝過程中會出現設定MySQL初始密碼的的步驟,不過我在安裝過程中,屬實是什麽都沒發生,就是這個,導致後面有些坑的出現。

在安裝完成後,執行下面命令:

mysql -u root -p

然後要求你輸入密碼,如果之前設置,則輸入設置好的。

如果沒設置,就要開始進入下個坑了,出現下列報錯:

ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘

這個錯誤,簡單的說,就是密碼錯了,但是你沒設置過,怎麽會錯呢,這個就是MySQL自身會設置的隨機密碼的問題了,解決方法就是重新設置新的root密碼,但是問題在於,如果進入MySQL中去。我在這過程中有兩種方法進入MySQL命令行裏,

第一種如下:

sudo mysql

通過管理員權限,直接進入。

第二種如下:

關閉正在運行的 MySQL :
service mysql stop

運行命令:
mysqld_safe --skip-grant-tables &

以上兩種方法,如果執行成功,則會成功進入MySQL的命令行中,

然後就開始進行修改root密碼的操作了:

mysql> use mysql;
mysql> update mysql.user set authentication_string=PASSWORD(‘447728‘),plugin=‘mysql_native_password‘ where user=‘root‘;
mysql>flush privileges;


這一步主要是在設置新密碼的時候,順便把用戶root的plugin進行修改,修改為默認的 mysql_native_password,然後在刷新權限設置。

在執行成功後,重啟MySQL,然後再執行一次上面連接數據庫命令:

sudo service mysql restart 
mysql -u root -p

我這樣解決掉MySQL的使用問題的。

3:PHP7.2.5安裝

我直接執行如下命令:

sudo apt-get install php7.2 php7.2-fpm php7.2-mysql

三個命令一起執行,直接把php相關的各種軟件和拓展直接安裝成功。

4:配置Nginx解析PHP

首先修改你Nginx配置文件,執行命令:

sudo vim /etc/nginx/sites-available/default

修改如下圖:

server {
listen 80;
server_name 域名;

charset utf-8;

#access_log logs/host.access.log main;
# root /var/www/joyway/laraverl/public;
root /var/www/joyway;

location / {
index index.html index.htm index.php l.php;
autoindex off;
}

if (!-e $request_filename) {
rewrite ^/(.*) /index.php/$1 last;
}
#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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache‘s document root
# concurs with nginx‘s one
#location ~ /\.ht {
# deny all;
#}

}

然後執行命令,重啟Nginx:

sudo service nginx restart

接下來我們修改PHP相關配置文件:

sudo vim /etc/php/7.2/fpm/pool.d/www.conf

在37行加入替換代碼:

:set number

技術分享圖片

修改之後,我們重啟php7.2-fpm

sudo service php7.2-fpm start

然後咱們在/var/www裏面新建index.php文件

index.php代碼如下:

<?php

echo 123;

?>

然後在瀏覽器打開:域名/index.php 效果如下:

技術分享圖片

然後咱們在/var/www裏面新建mysql.php文件,

mysql.php代碼如下:

<?php echo ‘<h1>Page loaded with PHP!</h1>‘;
$conn = mysqli_connect(‘localhost‘, ‘site_admin‘, ‘your_password‘);

if(!$conn){
    die(‘<h2>Error: </h2>‘ . mysqli_connect_error());
}else{
    echo ‘<h2>Successfully Connected to MySQL Database!</h2>‘;
}
?>

然後在瀏覽器打開:localhost/mysql.php 效果如下:

Ubuntu 18.04.1 下快速搭建 LNMP環境