经验回顾:在CentOS下用源码安装LNMP(nginx,mysql,php)

来源:互联网 发布:知乎悟空反作弊系统 编辑:程序博客网 时间:2024/05/11 22:38

现在我们开干了。

干活之前,先把这些依赖的装了。当然也可以不装,等安装过程中出现错误再根据提示安装也行,那样显得更加DIY。

但是为了方便简洁,按照传统惯例,先压毛了下面这些包:

# yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel cmake


安装nginx

1、首先去http://nginx.org/下载,最好找稳定(stable)版的哦。

下载到本地后,接下来进行第二步。

2、依据国际GNU惯例,先配置# ./configure --prefix=/www/server/nginx --with-http_stub_status_module

prefix改成自己想要装的地方,最后出现提示

Configuration summary  + using system PCRE library  + OpenSSL library is not used  + md5: using system crypto library  + sha1: using system crypto library  + using system zlib library  nginx path prefix: "/www/server/nginx"  nginx binary file: "/www/server/nginx/sbin/nginx"  nginx configuration prefix: "/www/server/nginx/conf"  nginx configuration file: "/www/server/nginx/conf/nginx.conf"  nginx pid file: "/www/server/nginx/logs/nginx.pid"  nginx error log file: "/www/server/nginx/logs/error.log"  nginx http access log file: "/www/server/nginx/logs/access.log"  nginx http client request body temporary files: "client_body_temp"  nginx http proxy temporary files: "proxy_temp"  nginx http fastcgi temporary files: "fastcgi_temp"  nginx http uwsgi temporary files: "uwsgi_temp"  nginx http scgi temporary files: "scgi_temp"
然后make,make install。打完收工。

3、运行一下curl http://localhost,出来欢迎页了,OK,安装成功。
4、不过还不能兴奋。这时的nginx还有个大招没开启,有php的请求来了,没法处理
接下来将开启这个技能。
进到/www/nginx/conf目录下,vim 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;        }

特别解释一下:$document_root要替换成你自己的根目录路径,比如我的这里就是
fastcgi_param  SCRIPT_FILENAME  /www/server/nginx/html/$fastcgi_script_name;
5、重启一下#/www/server/nginx/sbin/nginx -s reload
OK,nginx装好了。别愣着了,接下来赶紧把php装好呗,不然依旧没法处理php请求。


安装php

首先给一个懒人配置,常用的都给你开上。不过没有把oci8给带上,如果需要用oracle的,可以参考另一篇。(ps:如果后面想补装扩展,可以进到扩展包里用phpize生成configure。再./configure  –with-php-config=/usr/local/webserver/php/bin/php-config)。
./configure --prefix="/www/php" --with-apxs2="/opt/64bit/apache_2_4_3/bin/apxs" --with-config-file-path="/opt/64bit/php_5_3_23/etc"  --with-pear="/opt/64bit/php_5_3_23/pear" --enable-shared --enable-inline-optimization --disable-debug --with-libxml-dir --enable-bcmath --enable-calendar --enable-ctype --with-kerberos --enable-ftp --with-jpeg-dir  --with-freetype-dir  --enable-gd-native-ttf  --with-gd   --with-iconv    --with-zlib  --with-openssl --with-xsl   --with-imap-ssl --with-imap --with-gettext     --with-mhash --enable-sockets --enable-mbstring=all --with-curl --with-curlwrappers  --enable-mbregex  --enable-exif --with-bz2  --with-sqlite3   --with-mysql=mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-pdo-sqlite  --enable-fileinfo --enable-phar   --enable-zip --with-pcre-regex   --with-mcrypt
然后老步骤,妹扣,妹扣 install。就完成了。不连篇累牍叙述了。

在linux下执行以下命令:
#php -v
如果打印出版本,则表示PHP安装成功,你若不相信,也可以来一个phpinfo试试,不过在终端上看结果很晕。
#php -r "phpinfo();"

这里注意几个问题:

1、每次输入php安装路径比较麻烦,其实我们可以不用输入的!
将/www/server/php/bin/php 这个文件复制到/usr/sbin下,php将被安装为linux命令
2、配置php.ini,一个容易引发新手错误的重要的事情。
将安装源码里的/www/software/php-7.0.1/php.ini-production 拷贝到php的安装目录的lib文件夹下。当然开发环境想多些提示,就拷另外那个-develop的。
3、启动php-fpm,让其以cgi方式运行,否则nginx找不到能托付的兄弟</strong>(如果不能成功,检查iptables有没有开9000的监听端口)
启动php-fpm前,一定要注意检查配置文件www.conf是否存在,
# cp /www/server/php/etc/php-fpm.d/www.conf.default /www/server/php/etc/php-fpm.d/www.conf
# /www/server/php/sbin/php-fpm

现在扔个php文件到web目录里(我的是原生的html文件夹),执行试试。

PS知识点:

php 5.3.3 后的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:master进程可以理解以下信号INT, TERM 立刻终止QUIT 平滑终止USR1 重新打开日志文件USR2 平滑重载所有worker进程并重新载入配置和二进制模块示例:php-fpm 关闭:kill -INT `cat /usr/local/php/var/run/php-fpm.pid`php-fpm 重启:kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

安装mysql

1、首先下载:
先去http://dev.mysql.com/downloads/mysql/ 下载最新的MySQL Community Server
在Select Platform:里选择Source Code;
Red Hat Enterprise Linux 6 / Oracle Linux 6 (Architecture Independent), RPM Package
然后选择下载Generic Linux (Architecture Independent), Compressed TAR Archive

2、打开安装参考文档:(版本自已选择合适的)比如:http://dev.mysql.com/doc/refman/5.6/en/installing-source-distribution.html
跟着一起来做:

# Preconfiguration setupshell> groupadd mysqlshell> useradd -r -g mysql -s /bin/false mysql# Beginning of source-build specific instructionsshell> tar zxvf mysql-VERSION.tar.gzshell> cd mysql-VERSION//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////这里要注意,如果你要更改安装路径,那么在cmake时加上参数cmake .  -DCMAKE_INSTALL_PREFIX=dir_name////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////shell> cmake .shell> makeshell> make install# End of source-build specific instructions# Postinstallation setupshell> cd /usr/local/mysqlshell> chown -R mysql .shell> chgrp -R mysql .shell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql data

3、配置 my.conf

安全模式启动mysql前,先注意看一下my.cnf几个关键配置是否正常

port=3306basedir="/www/server/mysql/"datadir="/www/server/mysql/data"character-set-server=utf8
关于my.cnf,优先找安装目录下的/www/server/mysql/my.cnf,如果没有再找/etc/my.cnf

4、设置 启动
shell> cp support-files/mysql.server /etc/init.d/mysql.server

5、启动

shell> bin/mysqld_safe --user=mysql &


行百里路,半九十,到这还没有完。把下面几件事给办了才行。

改root密码

SET PASSWORD FOR 'root'@'机器名' = PASSWORD('new-password-here');

UPDATE mysql.user SET Password=PASSWORD('new-password-here') WHERE User='root' AND Host='机器名';

给远程终端帐号授权

mysql>grant all privileges on *.* to 'root'@'%';

如果要设置密码的话:IDENTIFIED BY  'new password' WITH GRANT OPTION的效果和update set password=PASSWORD(new password)一样,但是记得要加WITH GRANT OPTION
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY  'new password' WITH GRANT OPTION;   

但是正确的做法是给运维部门的机器IP段分配权限,创建新的帐号,且不要轻易用ALL PRIVILEGES和%;
CREATE USER 'soonfly'@'192.168.0.%' IDENTIFIED BY '12345';
grant select,insert,update,delete,create,index,trigger,create temporary tables on sky.* to 'soonfly'@'192.168.0.%';
FLUSH PRIVILEGES;

show grants for 'root'@'localhost';//GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION


OK,收工!

1 0
原创粉丝点击