Linux installation and configuration nginx+php build

Linux installation and configuration nginx+php build

1.nginx source code package compilation environment and installation of corresponding dependencies

1.1 Install the compilation environment
#安装编译环境
[root@localhost /]# yum install -y gcc gcc-c++
1.2 Install the pcre library, zlib library and openssl library
  • pcre (Perl Compatible Regular Expressions) is a Perl library, a regular expression function library written in C language, and a lightweight function library. The http module of nginx uses pcre to parse regular expressions, so you need to install the pcre library on Linux

  • zlib is a set of general decompression open source libraries that provides in-memory compression and decompression functions and can detect the integrity of decompressed data. nginx uses zlib to gzip the contents of the http package, so the zlib library needs to be installed on Linux.

  • openssl, or open secure sockets layer, is an open source secure sockets layer cryptographic library. Including commonly used password encryption and decryption algorithms, commonly used key algorithms, certificate management and SSL protocols. nginx not only supports the http protocol, but also supports https (that is, transmitting http over the ssl protocol), so you need to install the openssl library on Linux.

#安装pcre库
[root@localhost /]# yum install -y pcre pcre-devel

#安装zlib库
[root@localhost /]# yum install -y zlib zlib-devel

#安装openssl库
[root@localhost /]# yum install -y openssl-devel

2.Install nginx

2.1 Obtain the source code package from the nginx official website and download it

Insert image description here

[root@localhost /]# mkdir nginxfile
[root@localhost /]# cd nginxfile/

#下载nginx源码包
[root@localhost nginxfile]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
2.2 Decompress and compile
#解压源码包
[root@localhost nginxfile]# tar -zxvf nginx-1.24.0.tar.gz

#编译配置
[root@localhost nginxfile]# cd nginx-1.24.0/
[root@localhost nginx-1.24.0]# ./configure --with-http_ssl_module
[root@localhost nginx-1.24.0]# ./configure --with-stream
[root@localhost nginx-1.24.0]# ./configure

#执行安装
[root@localhost nginx-1.24.0]# make && make install

3. Start nginx service

3.1 Run nginx

Enter the directory /usr/local/sbin and execute the ./nginx command

[root@localhost nginx-1.24.0]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx

#执行nginx
[root@localhost sbin]# ./nginx 

#查看nginx的进程开启情况
[root@localhost sbin]# ps -aux | grep nginx
root       72876  0.0  0.0  34444   384 ?        Ss   21:27   0:00 nginx: master process ./nginx
nobody     72877  0.0  0.2  66624  3932 ?        S    21:27   0:00 nginx: worker process
root       72887  0.0  0.0  12136  1120 pts/2    R+   21:28   0:00 grep --color=auto nginx
3.2 Turn off the firewall

Turn off the firewall to prevent local host access from being blocked

[root@localhost sbin]# systemctl stop firewalld.service
[root@localhost sbin]# systemctl status firewalld.service
3.3 Use a local browser to serve the virtual machine address

The welcome to nginx page appears, indicating that the nginx installation is complete.

Insert image description here

4. Modify the nginx configuration file to create a new port page

cd to the path /usr/local/nginx/html and create a new file with the suffix .html

[root@localhost html]# touch web.html

#在文件中编写新的网页
[root@localhost html]# vim web.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>

cd to the directory /usr/local/nginx/conf, modify the configuration file, and add the following configuration

[root@localhost conf]# vim nginx.conf
    server {
    
    
        listen       8080;  #浏览器访问虚拟机的8080端口时显示该页面
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
    
     
            root   html;
            index  web.html index.htm; #访问8080端口时定位到web.html文件
        }

        #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;
        }
        
#停止nginx服务
[root@localhost sbin]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -s stop

#重新启动nginx服务
[root@localhost sbin]# ./nginx

Use a local browser to access port 8080 to view the new page

Insert image description here

5.Install php

5.1 Use yum to install php-fpm
#因为源码安装的方式会需要搭载很多的依赖包,安装编译过程也比较的繁琐,所以这里采用yum源安装途径,yum仓库安装的方法比源码包安装更加的简洁快速
[root@localhost /]# yum -y install php-fpm	

#启动php-fpm服务
[root@localhost /]# systemctl start php-fpm
[root@localhost /]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset:>   Active: active (running) since Tue 2023-08-08 14:29:59 CST; 22s ago
5.2 Modify the php configuration file
#定位到php-fpm的文件下
[root@localhost /]# cd /etc/php-fpm.d/

#修改www.conf文件内容
[root@localhost php-fpm.d]# vim www.conf

Comment out the listen = /run/php-fpm/www.sock section in the www.conf file and add listen = 127.0.0.1:9000

Insert image description here

5.3 Modify nginx configuration file
#定位到nginx/conf文件下
[root@localhost nginx]# cd /usr/local/nginx/conf/

#修改nginx.conf文件内容
[root@localhost conf]# vim nginx.conf

Uncomment the servermissing location ~ \.php$content

Insert image description here

Change the content after the uncommented location ~ \.php$content tofastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name$document_root$fastcgi_script_name

Insert image description here

Add the file name after serverthe first paragraph location /of content (used to locate the php page file that will be created later)indexweb.php

Insert image description here

5.4 Create php page files and view tests
#定位到nginx/html文件下
[root@localhost nginx]# cd /usr/local/nginx/html/

#创建web.php页面文件
[root@localhost html]# vim web.php
<?php
phpinfo();
?>

After all files are configured, you need to restart the php-fpm service and nginx

#重启php-fpm服务
[root@localhost html]# systemctl restart php-fpm
[root@localhost html]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset:>   Active: active (running) since Tue 2023-08-08 15:02:04 CST; 7s ago
 Main PID: 76438 (php-fpm)
 
#重启nginx
#定位到nginx/sbin文件路径下
[root@localhost html]# cd /usr/local/nginx/sbin/
 
#停止nginx
[root@localhost sbin]# ./nginx -s stop
#重新启动nginx
[root@localhost sbin]# ./nginx

#查看开启情况
[root@localhost sbin]# ps -aux | grep nginx 
root       76473  0.0  0.0  34444   440 ?        Ss   15:03   0:00 nginx: master process ./nginx
nobody     76474  0.0  0.2  66564  4172 ?        S    15:03   0:00 nginx: worker process
root       76503  0.0  0.0  12136  1192 pts/1    R+   15:04   0:00 grep --color=auto nginx

Access the web.php page in your local browser

Insert image description here
Note: If the access is unsuccessful, you can check whether the firewall is turned off.

Guess you like

Origin blog.csdn.net/qq_44829421/article/details/132167528