以下是在 CentOS 8 系统上从源代码编译安装 Nginx、MySQL 和 PHP 的详细步骤。
首先更新系统并安装必要的开发工具:
sudo dnf update -y
sudo dnf groupinstall "Development Tools" -y
sudo dnf install -y wget cmake make gcc gcc-c++ pcre-devel zlib-devel openssl-devel \
libxml2-devel libxslt-devel gd-devel geoip-devel perl-ExtUtils-Embed \
bison ncurses-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel \
oniguruma-devel libzip-devel
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.23.3.tar.gz
sudo tar -zxvf nginx-1.23.3.tar.gz
cd nginx-1.23.3
sudo ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module
sudo make
sudo make install
sudo useradd -r nginx
sudo cp /usr/local/nginx/sbin/nginx /usr/sbin/
创建 systemd 服务文件 /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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动 Nginx 并设置开机启动:
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
cd /usr/local/src
sudo wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33.tar.gz
sudo tar -zxvf mysql-8.0.33.tar.gz
cd mysql-8.0.33
sudo dnf install -y openssl-devel ncurses-devel libtirpc-devel rpcgen
sudo cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci
sudo make -j$(nproc)
sudo make install
sudo groupadd mysql
sudo useradd -r -g mysql -s /bin/false mysql
sudo chown -R mysql:mysql /usr/local/mysql
cd /usr/local/mysql
sudo bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
记下生成的临时密码(在输出末尾)。
/etc/systemd/system/mysql.service
:[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
[Install]
WantedBy=multi-user.target
启动 MySQL 并设置开机启动:
sudo systemctl daemon-reload
sudo systemctl start mysql
sudo systemctl enable mysql
sudo /usr/local/mysql/bin/mysql_secure_installation
cd /usr/local/src
sudo wget https://www.php.net/distributions/php-8.2.6.tar.gz
sudo tar -zxvf php-8.2.6.tar.gz
cd php-8.2.6
sudo ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mbstring \
--enable-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--with-openssl \
--with-zlib \
--with-curl \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gd \
--with-jpeg \
--with-freetype \
--with-zip \
--with-libxml \
--with-xmlrpc \
--enable-soap \
--enable-intl \
--with-pear
sudo make -j$(nproc)
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
/etc/systemd/system/php-fpm.service
:[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/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
启动 PHP-FPM 并设置开机启动:
sudo systemctl daemon-reload
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑 /usr/local/nginx/conf/nginx.conf
,在 server 块中添加:
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;
}
测试配置并重启 Nginx:
sudo nginx -t
sudo systemctl restart nginx
创建测试 PHP 文件:
sudo echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php
在浏览器中访问 http://your_server_ip/info.php
,应该能看到 PHP 信息页面。
为了方便使用,可以添加环境变量:
echo 'export PATH=/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
至此,您已在 CentOS 8 上成功从源代码编译安装了 Nginx、MySQL 和 PHP。