在Ubuntu 1上安装PHP7.2 NGINX和PHP7.2-FPM

22 篇文章 0 订阅
3 篇文章 0 订阅
本文档详细介绍了如何在Ubuntu系统上安装最新版的NGINX和PHP7.2-FPM。首先通过添加PPA源安装NGINX,然后更新软件包并启动服务。接着,添加PHP7.2的PPA源,安装PHP7.2及其相关模块,并配置PHP-FPM使用UNIX套接字。最后,修改NGINX配置以处理PHP文件,并重启服务验证设置。
摘要由CSDN通过智能技术生成
########## Install NGINX ##############

# Install software-properties-common package to give us add-apt-repository package
sudo apt-get install -y software-properties-common

# Install latest nginx version from community maintained ppa
sudo add-apt-repository ppa:nginx/stable

# Update packages after adding ppa
sudo apt-get update

# Install nginx
sudo apt-get install -y nginx

# Check status
sudo service nginx

# Start nginx if it is not already running
sudo service nginx start

# At this point you should have NGINX up and running, open http://localhost on your 
# browser and you should see a welcome page from NGINX

######### Install PHP7.2 and PHP7.2-FPM ################

# check what is the latest PHP version supplied by UBUNTU
sudo apt-cache show php

# You should get something like below where 'Version' would tell us the latest version available in my case is 7.0

# Package: php
# Architecture: all
# Version: 1:7.0+35ubuntu6.1
# Priority: optional
# Section: php
# Source: php-defaults (35ubuntu6.1)
# Origin: Ubuntu
# Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
# Original-Maintainer: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
# Bugs: https://bugs.launchpad.net/ubuntu/+filebug
# Installed-Size: 11
# Depends: php7.0
# Filename: pool/main/p/php-defaults/php_7.0+35ubuntu6.1_all.deb
# Size: 2862
# MD5sum: c5db88de62d1dcb9679e9ae328d7c94a
# SHA1: 49d3f1d1850ac867fedf6fbc5635c33227dfde53
# SHA256: 0b1ccf22b958163669e677d6f660888eec7951d6eda3ae0f49a8e3409773cd87
# Description: server-side, HTML-embedded scripting language (default)
# Description-md5: b955c03ceec2872c327e77278c943d6a
# Phased-Update-Percentage: 90
# Supported: 5y


# Add Repository which gives us the latest php version 7.2
sudo add-apt-repository ppa:ondrej/php

# Lets now check what is the latest PHP version available now after the repository is added
sudo apt-cache show php

# You should now get something like below and notice the latest version is changed i.e Version: 1:7.2+60+ubuntu16.04.1+deb.sury.org+1

# Package: php
# Source: php-defaults (60+ubuntu16.04.1+deb.sury.org+1)
# Priority: optional
# Section: php
# Installed-Size: 12
# Maintainer: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
# Architecture: all
# Version: 1:7.2+60+ubuntu16.04.1+deb.sury.org+1
# Depends: php7.2
# Filename: pool/main/p/php-defaults/php_7.2+60+ubuntu16.04.1+deb.sury.org+1_all.deb
# Size: 5654
# MD5sum: 1089c40cb9763d667b1ba2bfb0b6b2c4
# SHA1: d34c619a14854708f43ae432daf5fe5e73737072
# SHA256: 6b2b24f7232f980818a688f66fd3f9759ac9d1eb0466dee30faa5b2f4c2b36df
# Description: server-side, HTML-embedded scripting language (default)
# Description-md5: 2ccdfdb6b598dc9bdf5572917b808dcb

# Lets now install php7.2 and some important modules which we will need.

sudo apt-get install php7.2-cli php7.2-fpm php7.2-curl php7.2-gd php7.2-mysql php7.2-mbstring zip unzip

# Some modules require you to authenticate installation and prompt you for some information such as:

# Please select the geographic area in which you live. Subsequent configuration questions will narrow this down by presenting a list
# of cities, representing the time zones in which they are located.

#  1. Africa   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. SystemV  13. Etc
#  2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. US


# Geographic area: 8

# Please select the city or region corresponding to your time zone.

#  1. Amsterdam   9. Brussels     17. Guernsey     25. Lisbon      33. Monaco     41. Rome        49. Stockholm  57. Vienna
#  2. Andorra     10. Bucharest   18. Helsinki     26. Ljubljana   34. Moscow     42. Samara      50. Tallinn    58. Vilnius
#  3. Astrakhan   11. Budapest    19. Isle_of_Man  27. London      35. Nicosia    43. San_Marino  51. Tirane     59. Volgograd
#  4. Athens      12. Busingen    20. Istanbul     28. Luxembourg  36. Oslo       44. Sarajevo    52. Tiraspol   60. Warsaw
#  5. Belfast     13. Chisinau    21. Jersey       29. Madrid      37. Paris      45. Saratov     53. Ulyanovsk  61. Zagreb
#  6. Belgrade    14. Copenhagen  22. Kaliningrad  30. Malta       38. Podgorica  46. Simferopol  54. Uzhgorod   62. Zaporozhye
#  7. Berlin      15. Dublin      23. Kiev         31. Mariehamn   39. Prague     47. Skopje      55. Vaduz      63. Zurich
#  8. Bratislava  16. Gibraltar   24. Kirov        32. Minsk       40. Riga       48. Sofia       56. Vatican

# Time zone: 27

# Once done all basic modules will be installed now, lets check the version now

php -v

# PHP 7.2.2-3+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Feb  6 2018 16:11:23) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
#    with Zend OPcache v7.2.2-3+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies


# Lets also check if the PHP7.2-FPM is running, if not start it

sudo service php7.2-fpm status

sudo service php7.2-fpm start  # (if the service isn't running already)

# Now we need to configure the Gateway so that PHP-FPM uses the UNIX socket for executing PHP files.
# For this we need to check where is the PHP7.2-fpm service running from and where are their configurations stored

sudo ps aux | grep php # (this will list php process)

# You will see the output something like below depending on how many processes are running

# root     13122  0.0  0.0 412040 14752 ?        Ss   12:17   0:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
# www-data 13123  0.0  0.0 412040  8424 ?        S    12:17   0:00 php-fpm: pool www
# www-data 13124  0.0  0.0 412040  8424 ?        S    12:17   0:00 php-fpm: pool www
# root     13126  0.0  0.0  11288   868 pts/1    S+   12:17   0:00 grep --color=auto php

# You will see the php-fpm master process is its location as /etc/php/7.2/fpm/php-fpm.conf. We now need to check the socket details
# from the PHP7.2-fpm by view this file.

vim /etc/php/7.2/fpm/php-fpm.conf

# You will see at the end of the file something like 'include=/etc/php/7.2/fpm/pool.d/*.conf' which means this process manager is using configurations from
# the pool.d directory. When viewing the /etc/php/7.2/fpm/pool.d/www.conf you will see that the PHP7.2-fpm (process manager) is 
# listening to the socket at 'listen = /run/php/php7.2-fpm.sock'
# Copy the socket location i.e. /run/php/php7.2-fpm.sock and we will use this socket to tell nginx to use this socket

vim /etc/nginx/site-available/default

# You will need to tell NGINX to process the index.php files as well so replace the following line

# index index.html index.htm index.nginx-debian.html;
# to
# index index.html index.htm index.php;

# Also you will see something like the following block for 'location'

#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
#       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
#}

# Make changes so to the above block so you have the following one:

location ~ \.php$ {
       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
}

# Note we have uncommented the location, and fastcgi_pass lines and also pasted the correct socket that
# is used by our php7.2-fpm (process manager)


# lets now restart both nginx and php7.2-fpm

sudo service nginx reload
sudo service php7.2-fpm restart


# Now lets create a index.php file within the document root i.e. root /var/www/html;

touch /var/www/html/index.php

# and put phpinfo() and save it.


# now open http://localhost and you should see the screen with all php informaiton
好的,那我就来帮您写一份关于 Nginx 和 LNMP 的实训报告。报告的主要内容包括实训目的、实验环境、实验过程、实验结果和实验总结等。请您参考以下报告模板: ## 实训目的 本次实训的目的是了解 Nginx 和 LNMP 的基本概念和使用方法,掌握 Nginx 的配置和部署方法,以及学会使用 LNMP 搭建 Web 服务器。 ## 实验环境 本次实验使用的环境如下: - 操作系统:Ubuntu 18.04 LTS - Nginx 版本:1.14.0 - PHP 版本:7.2.24 - MySQL 版本:5.7.28 ## 实验过程 ### 安装 Nginx 1. 打开终端,执行以下命令安装 Nginx: ``` sudo apt-get update sudo apt-get install nginx ``` 2. 安装完成后,启动 Nginx 服务: ``` sudo systemctl start nginx ``` 3. 使用浏览器访问服务器的 IP 地址,如果能看到 Nginx 的欢迎页面,则说明 Nginx 安装成功。 ### 配置 Nginx 1. 打开 Nginx 的配置文件 `/etc/nginx/nginx.conf`,并备份原文件: ``` sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` 2. 修改配置文件,将 `worker_processes` 设置为 CPU 核数的两倍,将 `worker_connections` 设置为 1024。 3. 在 `http` 块中添加以下内容,用于反向代理到后端应用服务器: ``` upstream backend { server 127.0.0.1:8000; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } ``` 4. 重新启动 Nginx 服务: ``` sudo systemctl restart nginx ``` ### 安装 LNMP 1. 安装 LNMP: ``` sudo apt-get install php7.2-fpm mysql-server ``` 2. 启动 PHP-FPM 服务: ``` sudo systemctl start php7.2-fpm ``` 3. 配置 PHP-FPM,打开 `/etc/php/7.2/fpm/pool.d/www.conf` 文件,修改以下内容: ``` listen = /run/php/php7.2-fpm.sock ``` 4. 重新启动 PHP-FPM 服务: ``` sudo systemctl restart php7.2-fpm ``` ### 部署应用程序 1. 创建一个 PHP 应用程序,例如 `index.php`,并将其放置在 `/var/www/html` 目录下。 2. 打开浏览器,访问服务器的 IP 地址,如果能看到 PHP 应用程序的页面,则说明 LNMP 部署成功。 ## 实验结果 本次实验成功安装和配置了 Nginx,以及使用 LNMP 部署了一个 PHP 应用程序。通过反向代理,Nginx 可以将请求转发到后端应用服务器,提高应用程序的性能和可靠性。 ## 实验总结 本次实验让我深入了解了 Nginx 和 LNMP 的基本概念和使用方法。Nginx 是一个高性能的 Web 服务器,可以通过反向代理和负载均衡提高应用程序的性能和可靠性。LNMP 则是一套经典的 Web 服务器架构,使用 PHP、MySQL 和 Nginx 来搭建 Web 服务器。通过实验,我深刻理解了 Web 服务器的基本原理和实现方法,对我的职业发展将有很大的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值