sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential autoconf libtool pkg-config \
libxml2-dev libssl-dev libcurl4-openssl-dev libsqlite3-dev \
libpng-dev libjpeg-dev libonig-dev libzip-dev
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio
make
sudo make install
sudo nano /etc/systemd/system/nginx.service
添加以下内容:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
wget https://www.php.net/distributions/php-8.2.12.tar.gz
tar -zxvf php-8.2.12.tar.gz
cd php-8.2.12
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--enable-mbstring \
--enable-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--enable-sockets \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-curl \
--with-zlib \
--with-gd \
--with-jpeg \
--with-webp \
--with-freetype \
--with-gettext \
--with-xmlrpc \
--with-pear
make
sudo make install
sudo cp php.ini-production /usr/local/php/etc/php.ini
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sudo nano /etc/systemd/system/php-fpm.service
添加以下内容:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo nano /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sudo /usr/local/nginx/sbin/nginx -t
sudo systemctl restart nginx
sudo nano /usr/local/nginx/html/info.php
添加内容:
<?php phpinfo(); ?>
http://your_server_ip/info.php
查看PHP信息页面echo 'export PATH=/usr/local/php/bin:/usr/local/nginx/sbin:$PATH' >> ~/.bashrc
source ~/.bashrc
sudo apt install -y php-redis php-memcached
这样就完成了在Ubuntu系统上从源码编译安装Nginx和PHP的全过程。