- 1.png
- linux+Nginx+Mysql+PHP搭配可以说目前使用比较广泛,本文是介绍使用源码编译安装,包括具体的编译参数以及配置文件
- 准备工作
- 环境
- Ubuntu
- 创建工作目录
/www/ 工程根目录 /www/server/ 后台程序环境 /www/website/ 部署站点根目录 /www/website/html/ 测试网站的根目录
- 增加用户以及用户组
sudo groupadd www
sudo useradd -g www www
- 添加用户权限
sudo chown -R www /www/website/
- 安装编译器
- 正式开始前,编译环境gcc g++开发库之类的需要提前装好
sudo apt-get -y install gcc build-essential make
- Nginx
- 2.png
- 安装依赖包
sudo apt-get install libxml2-dev build-essential openssl libssl-dev make curl libcurl4-gnutls-dev libjpeg-dev libpng-dev libtool-bin bison zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool libxml2-dev build-essential openssl libssl-dev make curl libcurl4-gnutls-dev libjpeg-dev libpng-dev libtool-bin bison zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool
- 进入后台环境文件夹
cd /www/server/
- 下载源码包
wget http://nginx.org/download/nginx-1.22.0.tar.gz
- 解压
tar -zxvf nginx-1.22.0.tar.gz
- 进入解压文件夹
cd nginx-1.22.0
- 准备编译参数
./configure --prefix=/www/server/nginx \ --user=www \ --group=www \ --with-pcre \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_slice_module \ --with-mail \ --with-threads \ --with-file-aio \ --with-stream \ --with-mail_ssl_module \ --with-stream_ssl_module
- 编译安装
make && make install
- 编译卸载
make uninstall
- 即可进行卸载
- 配置参数
sudo vim /www/server/nginx/conf/nginx.conf
- 配置用户或者组
#user nobody;
- 更改为
user www www;
- 启动
/www/server/nginx/sbin/nginx
- 注意
- 以非root权限启动时,会出现
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 错误
- 原因
- Linux只有root用户可以使用1024以下的端口
- 解决办法
- 以root权限启动
sudo ./nginx # 启动Nginx sudo ./nginx -s stop # 停止Nginx sudo ./nginx -v # 查看是否安装成功 sudo ./nginx -s reload # 重启Nginx sudo ./nginx -t # 验证配置文件是正确 sudo ./nginx -s quit # 退出nginx
- 清理Nginx包命令如下
dpkg --get-selections|grep nginx #列出Nginx关联的包 sudo apt-get --purge remove nginx # 删除Nginx包 sudo apt-get --purge remove nginx-common #删除nginx-common sudo apt-get --purge remove nginx-core #删除nginx-core
- 查看Nginx进程
ps -ef|grep nginx
- 杀死所有Nginx进程
killall nginx
- MySQL
- 3.png
- 安装依赖
sudo apt install build-essential cmake bison libncurses5-dev libssl-dev pkg-config
- 进入后台环境文件夹
cd /www/server/
- 下载源码包
- 源码包分为带boost版和不带boost版的,方便起见直接下载带boost的
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.22.tar.gz
- 解压
tar -zxvf mysql-boost-8.0.22.tar.gz
- 进入解压文件夹
cd mysql-8.0.22
- 准备编译参数
cmake \ -DCMAKE_INSTALL_PREFIX=/www/server/mysql \ -DMYSQL_UNIX_ADDR=/www/server/mysql/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DFORCE_INSOURCE_BUILD=1 \ -DMYSQL_DATADIR=/www/server/mysql/data \ -DMYSQL_TCP_PORT=3306 \ -DWITH_BOOST=boost
- 编译安装
make && make install
- 初始化MySQL,并生成root密码
bin/mysqld --initialize --user=mysql
- 保存一下密码,后面进入MySQL要用到
- 测试启动
bin/mysqld_safe --user=mysql
- 进入MySQL
bin/mysql -u root -p
- 输入上面生成的初始密码
- 修改root随机密码
- 将密码置为空
use mysql; update user set authentication_string='' where user='root';
- 修改密码为root
ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
- 如果出现如下错误
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> GRANT ALL PRIVILEGES ON *.* TO IDENTIFIED BY '123' WITH GRANT OPTION;
- 需要先执行
flush privileges;
- 然后再执行
ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
- 启动
/www/server/mysql/bin/mysql start
- 其他参数
mysql restart 重新启动 mysql stop 停止 mysql status 查看状态
- 查看MySQL进程
ps -ef|grep mysql
- 杀死所有MySQL进程
killall mysql
- 清理MySQL包命令如下
sudo apt purge mysql-* sudo rm -rf /www/server/mysql /var/lib/mysql
- PHP
- 4.png
- 安装必要的库文件
sudo apt-get install -y gcc make openssl curl libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev libssl-dev libsqlite3-dev libcurl4-openssl-dev libgmp3-dev libonig-dev libreadline-dev libxslt1-dev libffi-dev libpnglite-dev libpcre3-dev libiconv-hook-dev libiconv-hook1 libmcrypt-dev libmcrypt4 libmhash-dev libmhash2 libltdl-dev libssl-dev libcurl4-openssl-dev libmysqlclient-dev libmagickcore-dev libmagickwand-dev libedit-dev autoconf automake m4
- 进入后台环境文件夹
cd /www/server/
- 下载源码包
wget https://www.php.net/distributions/php-7.4.0.tar.gz
- 解压
tar -zxvf php-7.4.0.tar.gz
- 准备编译参数
./configure --prefix=/www/server/php --with-config-file-scan-dir=/www/server/php/etc/php.d --with-config-file-path=/www/server/php/etc --enable-mbstring --enable-zip --enable-bcmath --enable-pcntl --enable-ftp --enable-xml --enable-shmop --enable-soap --enable-intl --with-openssl --enable-fileinfo --enable-exif --enable-calendar --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-opcache --enable-fpm --enable-session --enable-sockets --enable-mbregex --enable-wddx --with-curl --with-iconv --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-jis-conv --with-openssl --with-pdo-mysql=mysqlnd --with-gettext --with-zlib --with-bz2 --with-recode --with-xmlrpc --with-mysqli=mysqlnd
- 编译安装
make -j && make install
- 出现Thank you for using PHP.就说明安装成功了
- 配置文件php.ini修改
sudo vim /www/server/php/74/etc/php.ini
- 修改disable_functions禁止危险函数
disable_functions = passthru,exec,system,chroot,chgrp,chown,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,phpinfo
- 配置php-fpm参数文件
sudo vim /www/server/php/74/etc/php-fpm.conf
- 修改参数
- global下修改为
pid = /www/server/php/74/var/run/php-fpm.pid error_log = /www/server/php/74/var/log/php-fpm.log
- 修改参数
- www下添加
user = www group = www
- 启动
/www/server/php/74/sbin/php-fpm start
- 清理MySQL包命令如下
sudo apt purge php-* sudo rm -rf /www/server/php /var/lib/php*
- 查看PHP进程
ps -ef|grep php
- 配置nginx代理php-fpm
- 创建配置文件
mkdir /www/server/nginx/conf/enable-php.conf
- 内容如下
location ~ [^/]\.php(/|$){ try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include /www/server/nginx/conf/fastcgi.conf; }
- 修改nginx配置文件
sudo vim /www/server/nginx/conf/nginx.conf
- 修改测试网站的根目录
root /www/webstie/html;
- 其中server段增加如下配置
include /www/server/nginx/conf/enable-php.conf;
- 创建测试php文件
- 在/www/webstie/html/创建index.php文件,输入如下内容
<?php echo "你好!";
- 然后重新启动php-fpm和nginx
- 浏览器访问
http://你的服务器ip/index.php
- 出现你好!说明php跟nginx都配置成功了
- 相关文章
- Linux部署Lnmp+PHPMyadmin源码安装及配置
【linux编译安装nginx+php+mysql环境】
开始
完结