Ubuntu 下安装LEMP环境 实战
---恢復內(nèi)容開始---
1.nginx的服務端的安裝
打開命令行終端,在終端輸入,sudo apt-get install nginx? 回車即開始安裝
kxlc-t@ubuntu:~$ sudo apt-get install nginx [sudo] kxlc-t 的密碼: 正在讀取軟件包列表... 完成 正在分析軟件包的依賴關系樹 正在讀取狀態(tài)信息... 完成 將會同時安裝下列軟件:nginx-common nginx-core 建議安裝:fcgiwrap nginx-doc 下列【新】軟件包將被安裝:nginx nginx-common nginx-core 升級了 0 個軟件包,新安裝了 3 個軟件包,要卸載 0 個軟件包,有 201 個軟件包未被升級。 需要下載 458 kB 的歸檔。 解壓縮后會消耗 1,482 kB 的額外空間。 您希望繼續(xù)執(zhí)行嗎? [Y/n] y 獲取:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-common all 1.10.3-0ubuntu0.16.04.2 [26.6 kB] 獲取:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-core amd64 1.10.3-0ubuntu0.16.04.2 [428 kB] 獲取:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.3-0ubuntu0.16.04.2 [3,490 B] 已下載 458 kB,耗時 3秒 (147 kB/s) 正在預設定軟件包 ... 正在選中未選擇的軟件包 nginx-common。 (正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當前共安裝有 258856 個文件和目錄。) 正準備解包 .../nginx-common_1.10.3-0ubuntu0.16.04.2_all.deb ... 正在解包 nginx-common (1.10.3-0ubuntu0.16.04.2) ... 正在選中未選擇的軟件包 nginx-core。 正準備解包 .../nginx-core_1.10.3-0ubuntu0.16.04.2_amd64.deb ... 正在解包 nginx-core (1.10.3-0ubuntu0.16.04.2) ... 正在選中未選擇的軟件包 nginx。 正準備解包 .../nginx_1.10.3-0ubuntu0.16.04.2_all.deb ... 正在解包 nginx (1.10.3-0ubuntu0.16.04.2) ... 正在處理用于 ureadahead (0.100.0-19) 的觸發(fā)器 ... 正在處理用于 ufw (0.35-0ubuntu2) 的觸發(fā)器 ... 正在處理用于 systemd (229-4ubuntu21.1) 的觸發(fā)器 ... 正在設置 nginx-common (1.10.3-0ubuntu0.16.04.2) ... 正在設置 nginx-core (1.10.3-0ubuntu0.16.04.2) ... 正在設置 nginx (1.10.3-0ubuntu0.16.04.2) ... 正在處理用于 systemd (229-4ubuntu21.1) 的觸發(fā)器 ... 正在處理用于 ureadahead (0.100.0-19) 的觸發(fā)器 ... 正在處理用于 ufw (0.35-0ubuntu2) 的觸發(fā)器 ...2. nginx服務設置,開啟及狀態(tài)的查看
kxlc-t@ubuntu:~$ sudo systemctl start nginx ------設置nginx服務開機自啟動kxlc-t@ubuntu:~$ sudo systemctl start nginx ? ? ?------開啟nginx服務 kxlc-t@ubuntu:~$ sudo systemctl status nginx.service --------查看nginx狀態(tài) ● nginx.service - A high performance web server and a reverse proxy serverLoaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enActive: active (running) since 四 2018-04-19 15:46:55 CST; 17min agoMain PID: 9898 (nginx)CGroup: /system.slice/nginx.service├─9898 nginx: master process /usr/sbin/nginx -g daemon on; master_pro└─9899 nginx: worker process 4月 19 15:46:55 ubuntu systemd[1]: Starting A high performance web server and a 4月 19 15:46:55 ubuntu systemd[1]: nginx.service: Failed to read PID from file / 4月 19 15:46:55 ubuntu systemd[1]: Started A high performance web server and a r 4月 19 16:04:17 ubuntu systemd[1]: Started A high performance web server and a r lines 1-12/12 (END)
3.? nginx版本的輸出
kxlc-t@ubuntu:~$ nginx -v nginx version: nginx/1.10.3 (Ubuntu)4.? 將www-data作為網(wǎng)站的根目錄使用者,默認情況是root使用
kxlc-t@ubuntu:~$ sudo chown www-data:www-data /usr/share/nginx/html/ -R kxlc-t@ubuntu:~$注意:在安裝nginx時候,默認創(chuàng)建用戶www-data用戶
kxlc-t@ubuntu:~$ cat /etc/passwd | grep www-data www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin5. 安裝mariadb數(shù)據(jù)庫
sudo apt-get install mariadb-server mariadb-client6. 配置mariadb數(shù)據(jù)庫
kxlc-t@ubuntu:~$ sudo mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.Enter current password for root (enter for none): OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.You already have a root password set, so you can safely answer 'n'.Change the root password? [Y/n] Y -------設置root用戶的密碼 New password: Re-enter new password: Password updated successfully! Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.Remove anonymous users? [Y/n] Y ------禁止匿名用戶訪問... Success!Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] Y -------不允許root用戶遠程登錄... Success!By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.Remove test database and access to it? [Y/n] Y ------移除test數(shù)據(jù)庫,并且禁止訪問它- Dropping test database...... Success!- Removing privileges on test database...... Success!Reloading the privilege tables will ensure that all changes made so far will take effect immediately.Reload privilege tables now? [Y/n] Y -------重載私有權限表... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDB installation should now be secure.Thanks for using MariaDB!?7. 安裝PHP 插件
sudo apt install php7.0 php7.0-fpm php7.0-mysql php-common php7.0-cli php7.0-common php7.0-json輸出:
kxlc-t@ubuntu:~$ sudo apt install php7.0 php7.0-fpm php7.0-mysql php-common php7.0-cli php7.0-common php7.0-json 正在讀取軟件包列表... 完成 正在分析軟件包的依賴關系樹 正在讀取狀態(tài)信息... 完成 下列軟件包是自動安裝的并且現(xiàn)在不需要了:libevent-core-2.0-5 使用'sudo apt autoremove'來卸載它(它們)。 將會同時安裝下列軟件:php7.0-opcache php7.0-readline 建議安裝:php-pear 下列【新】軟件包將被安裝:php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-jsonphp7.0-mysql php7.0-opcache php7.0-readline 升級了 0 個軟件包,新安裝了 9 個軟件包,要卸載 0 個軟件包,有 201 個軟件包未被升級。 需要下載 3,653 kB 的歸檔。 解壓縮后會消耗 14.5 MB 的額外空間。 您希望繼續(xù)執(zhí)行嗎? [Y/n] Y 獲取:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php-common all 1:35ubuntu6.1 [10.8 kB] 獲取:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-common amd64 7.0.28-0ubuntu0.16.04.1 [840 kB] 獲取:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-json amd64 7.0.28-0ubuntu0.16.04.1 [16.9 kB] 獲取:4 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-opcache amd64 7.0.28-0ubuntu0.16.04.1 [77.1 kB] 獲取:5 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-readline amd64 7.0.28-0ubuntu0.16.04.1 [12.8 kB] 獲取:6 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-cli amd64 7.0.28-0ubuntu0.16.04.1 [1,282 kB] 獲取:7 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 php7.0-fpm amd64 7.0.28-0ubuntu0.16.04.1 [1,288 kB] 獲取:8 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0 all 7.0.28-0ubuntu0.16.04.1 [1,290 B] 獲取:9 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-mysql amd64 7.0.28-0ubuntu0.16.04.1 [124 kB] 已下載 3,653 kB,耗時 5秒 (707 kB/s) 正在選中未選擇的軟件包 php-common。 (正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當前共安裝有 259138 個文件和目錄。) 正準備解包 .../php-common_1%3a35ubuntu6.1_all.deb ... 正在解包 php-common (1:35ubuntu6.1) ... 正在選中未選擇的軟件包 php7.0-common。 正準備解包 .../php7.0-common_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-common (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-json。 正準備解包 .../php7.0-json_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-json (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-opcache。 正準備解包 .../php7.0-opcache_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-opcache (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-readline。 正準備解包 .../php7.0-readline_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-readline (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-cli。 正準備解包 .../php7.0-cli_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-cli (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-fpm。 正準備解包 .../php7.0-fpm_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-fpm (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0。 正準備解包 .../php7.0_7.0.28-0ubuntu0.16.04.1_all.deb ... 正在解包 php7.0 (7.0.28-0ubuntu0.16.04.1) ... 正在選中未選擇的軟件包 php7.0-mysql。 正準備解包 .../php7.0-mysql_7.0.28-0ubuntu0.16.04.1_amd64.deb ... 正在解包 php7.0-mysql (7.0.28-0ubuntu0.16.04.1) ... 正在處理用于 man-db (2.7.5-1) 的觸發(fā)器 ... 正在處理用于 systemd (229-4ubuntu21.1) 的觸發(fā)器 ... 正在處理用于 ureadahead (0.100.0-19) 的觸發(fā)器 ... 正在設置 php-common (1:35ubuntu6.1) ... 正在設置 php7.0-common (7.0.28-0ubuntu0.16.04.1) ...Creating config file /etc/php/7.0/mods-available/calendar.ini with new versionCreating config file /etc/php/7.0/mods-available/ctype.ini with new versionCreating config file /etc/php/7.0/mods-available/exif.ini with new versionCreating config file /etc/php/7.0/mods-available/fileinfo.ini with new versionCreating config file /etc/php/7.0/mods-available/ftp.ini with new versionCreating config file /etc/php/7.0/mods-available/gettext.ini with new versionCreating config file /etc/php/7.0/mods-available/iconv.ini with new versionCreating config file /etc/php/7.0/mods-available/pdo.ini with new versionCreating config file /etc/php/7.0/mods-available/phar.ini with new versionCreating config file /etc/php/7.0/mods-available/posix.ini with new version8. PHP插件服務的啟動及狀態(tài)的查看
kxlc-t@ubuntu:~$ sudo systemctl enable php7.0-fpmkxlc-t@ubuntu:~$ sudo systemctl start php7.0-fpm kxlc-t@ubuntu:~$ sudo systemctl status php7.0-fpm ● php7.0-fpm.service - The PHP 7.0 FastCGI Process ManagerLoaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preseActive: active (running) since 四 2018-04-19 16:54:52 CST; 4min 33s agoProcess: 24868 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, stMain PID: 24877 (php-fpm7.0)Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/seCGroup: /system.slice/php7.0-fpm.service├─24877 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─24879 php-fpm: pool www └─24880 php-fpm: pool www 4月 19 16:54:52 ubuntu systemd[1]: Stopped The PHP 7.0 FastCGI Process Manager. 4月 19 16:54:52 ubuntu systemd[1]: Starting The PHP 7.0 FastCGI Process Manager. 4月 19 16:54:52 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager. 4月 19 16:59:16 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager. lines 1-15/15 (END)
第5步:創(chuàng)建一個Nginx虛擬主機
Nginx服務器就像Apache中的一個虛擬主機。 我們不會使用默認的服務器塊,因為它不足以運行PHP代碼,如果我們修改它,它變得一團糟。 因此,通過運行以下命令來刪除啟用了站點的目錄中的默認符號鏈接。 (它仍然是可用的/etc/nginx/sites-available/default)
sudo rm /etc/nginx/sites-enabled/default然后在/etc/nginx/conf.d/目錄下創(chuàng)建一個全新的服務器文件。 sudo vim /etc/nginx/conf.d/default.conf ? -------將以下文本粘貼到文件中。 將192.168.1.108替換為您的實際服務器IP地址。server {listen 80;listen [::]:80;server_name 192.168.1.108; ----ip地址替換為自己的ip地址root /usr/share/nginx/html/;index index.php index.html index.htm index.nginx-debian.html;location / {try_files $uri $uri/ /index.php;}error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}location ~ \.php$ {fastcgi_pass unix:/run/php/php7.0-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;include snippets/fastcgi-php.conf;}location ~ /\.ht {deny all;} }
?
PHP功能的測試
打開/usr/share/nginx/html/info.php , 輸入下列代碼:
<?php phpinfo()?>
在瀏覽器輸入,ip-adress/info.php,即可出現(xiàn)下面頁面;
如何安裝PHP7.2
PHP7.2是PHP的最新穩(wěn)定版本,于2017年11月30日發(fā)布,與PHP7.1相比,性能有所提升。 我們可以從Ondrej Sury添加PPA來在Ubuntu 17.10上安裝PHP7.2。 那個人也是Certbot PPA的維護者。
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update然后我們可以使用下面的命令安裝PHP7.2和通用擴展。
sudo apt install php7.2 php7.2-fpm php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl現(xiàn)在啟動PHP7.2-FPM。
sudo systemctl start php7.2-fpm在系統(tǒng)啟動時啟用自動啟動。
sudo systemctl enable php7.2-fpm檢查其狀態(tài):
systemctl status php7.2-fpm?
?
?
然后在/etc/nginx/conf.d/目錄下創(chuàng)建一個全新的服務器文件。
sudo nano /etc/nginx/conf.d/default.conf將以下文本粘貼到文件中。 將192.168.1.108替換為您的實際服務器IP地址。
server {listen 80;listen [::]:80;server_name 192.168.1.108;root /usr/share/nginx/html/;index index.php index.html index.htm index.nginx-debian.html;location / {try_files $uri $uri/ /index.php;}error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}location ~ \.php$ {fastcgi_pass unix:/run/php/php7.1-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;include snippets/fastcgi-php.conf;}location ~ /\.ht {deny all;} }轉載于:https://www.cnblogs.com/sunshine-1/p/8884695.html
總結
以上是生活随笔為你收集整理的Ubuntu 下安装LEMP环境 实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一道百度前端线上笔试题
- 下一篇: C++基础学习7:new/delete操