CentOS 7.2 编译安装Nginx+MySQL+PHP环境指南

0 下载量 129 浏览量 更新于2024-09-02 收藏 174KB PDF 举报
"在本文中,我们将详细探讨如何在CentOS 7.2.1511操作系统上编译安装Nginx 1.10.1、MySQL 5.6.33以及PHP 5.6.26,以构建一个完整的Web服务器运行环境。这个过程对于系统管理员和开发者来说是非常有用的,因为它允许他们自定义配置并确保软件版本与项目需求相匹配。" 一、防火墙配置 在开始安装之前,首先要进行防火墙配置。CentOS 7.2.1511默认使用的是firewall服务,但在这里我们将更改为iptables防火墙,因为iptables提供了更灵活的规则设置。首先,我们需要关闭firewalld服务: ``` systemctl stop firewalld.service # 停止firewall systemctl disable firewalld.service # 禁止firewall开机启动 ``` 接着,我们安装iptables服务: ``` yum install iptables-services # 安装 ``` 然后编辑iptables的配置文件 `/etc/sysconfig/iptables`,添加必要的端口开放规则,例如SSH、HTTP和MySQL的端口: ``` # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT :wq! # 保存退出 ``` 最后,重启iptables服务以应用新的规则: ``` systemctl restart iptables.service ``` 二、Nginx的编译安装 1. 更新系统包并安装依赖: ``` yum update yum install -y pcre-devel zlib-devel openssl-devel gcc make ``` 2. 下载Nginx源代码: ``` wget http://nginx.org/download/nginx-1.10.1.tar.gz tar zxf nginx-1.10.1.tar.gz cd nginx-1.10.1 ``` 3. 配置、编译并安装: ``` ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install ``` 4. 创建启动脚本和服务: ``` vi /etc/init.d/nginx chmod +x /etc/init.d/nginx chkconfig --add nginx ``` 5. 启动Nginx: ``` systemctl start nginx ``` 三、MySQL的编译安装 1. 安装依赖: ``` yum install -y cmake libaio-devel ncurses-devel ``` 2. 下载MySQL源代码: ``` wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.33.tar.gz tar zxf mysql-5.6.33.tar.gz cd mysql-5.6.33 ``` 3. 配置、编译并安装: ``` cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_EXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DENABLED_LOCAL_INFILE=1 -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci make make install ``` 4. 初始化MySQL数据库: ``` cd /usr/local/mysql scripts/mysql_install_db --user=mysql ``` 5. 设置权限和启动服务: ``` chown -R root:mysql /usr/local/mysql chgrp -R mysql /usr/local/mysql/data ln -s /usr/local/mysql/bin/* /usr/bin/ systemctl start mysqld ``` 6. 设置root用户密码: ``` /usr/local/mysql/bin/mysqladmin -u root password 'your_password' ``` 四、PHP的编译安装 1. 安装依赖: ``` yum install -y gcc libxml2-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libcurl-devel libbz2-devel gmp-devel libssl-devel ``` 2. 下载PHP源代码: ``` wget https://museum.php.net/php5/php-5.6.26.tar.gz tar zxf php-5.6.26.tar.gz cd php-5.6.26 ``` 3. 配置、编译并安装: ``` ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr --with-freetype-dir=/usr/local/freetype --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --with-zlib-dir=/usr --with-libxml-dir=/usr --enable-xml --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbstring --enable-track-vars --enable-sockets --with-mhash --enable-zip --enable-ftp --enable-soap --with-gd --enable-gd-native-ttf --with-gettext --disable-rpath --enable-shared --enable-opcache --without-pear make make install ``` 4. 创建启动脚本和服务: ``` vi /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm chkconfig --add php-fpm ``` 5. 修改php.ini配置文件,并启动php-fpm: ``` cp php.ini-development /etc/php.ini systemctl start php-fpm ``` 6. 配置Nginx以支持PHP: 修改Nginx的配置文件(如:`/usr/local/nginx/conf/nginx.conf`),添加处理PHP请求的配置段: ``` location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 7. 重新加载Nginx配置: ``` systemctl reload nginx ``` 至此,您已经在CentOS 7.2.1511上成功地编译安装了Nginx 1.10.1、MySQL 5.6.33和PHP 5.6.26,现在您可以在这个环境中部署和运行基于这些技术的Web应用程序了。